Calculating end-to-end delay in ns3 involves measuring the time taken for a packet to traverse from the source to the destination. End-to-end delay can be calculated by subtracting the send time of the packet from its receive time. Below is the guide on calculating end-to-end delay in ns3.
Steps for calculating end-to-end delay
- Set up the simulation :
- To simulate the network, create a network topology with nodes, protocols and links configured.
- Install applications :
- To generate and receive traffic, set up application.
- Trace the packets :
- To record the send and receive times of packets, use ns3 tracing capabilities.
- Calculate end-to-end delay :
- Subtract the send time from the receive time for each packet.
Below is an example to demonstrate the process:
- set up the simulation
Create a simple network topology by using a point-to-point link between two nodes.
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/point-to-point-module.h”
#include “ns3/applications-module.h”
using namespace ns3;
NS_LOG_COMPONENT_DEFINE (“EndToEndDelayExample”);
int main (int argc, char *argv[])
{
// Create two nodes
NodeContainer nodes;
nodes.Create (2);
// Set up the point-to-point link
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute (“DataRate”, StringValue (“5Mbps”));
pointToPoint.SetChannelAttribute (“Delay”, StringValue (“2ms”));
// Install the link devices on the nodes
NetDeviceContainer devices;
devices = pointToPoint.Install (nodes);
// Install the internet stack
InternetStackHelper stack;
stack.Install (nodes);
// Assign IP addresses
Ipv4AddressHelper address;
address.SetBase (“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = address.Assign (devices);
// Set up the UDP echo server and client
uint16_t port = 9; // well-known echo port number
UdpEchoServerHelper echoServer (port);
ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
UdpEchoClientHelper echoClient (interfaces.GetAddress (1), port);
echoClient.SetAttribute (“MaxPackets”, UintegerValue (100));
echoClient.SetAttribute (“Interval”, TimeValue (Seconds (0.1))); // 10 packets per second
echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
// Enable tracing
AsciiTraceHelper ascii;
pointToPoint.EnableAsciiAll (ascii.CreateFileStream (“end-to-end-delay.tr”));
// Run the simulation
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
- Install applications
In the above example, on one node a UDP echo server is installed. and on another node, a UDP echo client is installed. The client sends 1000 packets, each 1024 bytes in size, at an interval of 0.01 seconds.
- Trace the packets
There are several ways to trace packets in ns3. We can use either ASCII or PCAP tracing. But in our example, we enabled ASCII tracing.
- Calculate end-to-end delay
Parse the trace file to extract the send and receive times of packets after the simulation. Here is a basic way to process the trace file :
Example Python Script to Calculate end-to-end delay
send_times = {}
recv_times = {}
with open(“end-to-end-delay.tr”, “r”) as trace_file:
for line in trace_file:
fields = line.split()
time = float(fields[1]) # Assuming the time is the second field
node_id = fields[2]
event = fields[3]
packet_id = fields[5] # Assuming packet ID is the sixth field
if “UdpEchoClient” in line and “Sent” in event:
send_times[packet_id] = time
elif “UdpEchoServer” in line and “Received” in event:
recv_times[packet_id] = time
delays = []
for packet_id in send_times:
if packet_id in recv_times:
delays.append(recv_times[packet_id] – send_times[packet_id])
average_delay = sum(delays) / len(delays) if delays else 0
print(f”Average End-to-End Delay: {average_delay} seconds”)
Explanation
- Simulation setup :
Two nodes are created. Those nodes are connected using a point-to-point link.
- Application setup :
On one node, a UDP echo server is installed. and On another node, a UDP echo client is installed. Configure the client to send a specified number of packets at a specified interval.
- Packet tracing :
To record packet events, enable ASCII.
- Calculating end-to-end delay :
to extract the send and receive times of packets, parse the trace files. Subtract the send time from the receive time for each packet to get the end-to-end delay.
Overall, we went through the guide on calculating end-to-end delay in ns3 by measuring the time taken for a packet to travel from the source to the destination. Also, we offer more topics related to end-to-end delay.
ns3simulation.com provide with advice on network performance for your project. Additionally, we continue to work on performance analysis after it. We also offer in-depth details on the calculation from end-to-end delay in ns3simulation.