To calculate intermediate delay in ns3, we have to measure the delay at every hop in a multi-hop network. Intermediate delay is defined as the time taken for a packet to travel from one node to the next node. Below are the steps to calculate intermediate delay.
Steps for calculating network burstiness
- Set up the simulation :
- To simulate the network, create a network topology with nodes, protocols and links configured.
- Install applications :
- On the nodes, setup applications to generate and receive traffic.
- Trace the packets :
- To record the send and receive times of packets at each node, use ns3 tracing capabilities.
- Calculate intermediate delay :
- Subtract the send time from the receive time for each packet at each intermediate node.
Example of a simple Multi-Hop Network
Create a basic three-node linear topology where Node 0 sends packets to Node 2 via Node 1.
set up the simulation
#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 (“IntermediateDelayExample”);
void RxTrace (Ptr<const Packet> packet, const Address &address, std::string nodeName, std::string context)
{
NS_LOG_UNCOND (“Packet received at ” << nodeName << ” at time ” << Simulator::Now ().GetSeconds () << ” seconds”);
}
int main (int argc, char *argv[])
{
// Create three nodes
NodeContainer nodes;
nodes.Create (3);
// Set up point-to-point links
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute (“DataRate”, StringValue (“5Mbps”));
pointToPoint.SetChannelAttribute (“Delay”, StringValue (“2ms”));
// Install link devices on nodes
NetDeviceContainer devices01, devices12;
devices01 = pointToPoint.Install (nodes.Get (0), nodes.Get (1));
devices12 = pointToPoint.Install (nodes.Get (1), nodes.Get (2));
// 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 interfaces01 = address.Assign (devices01);
address.SetBase (“10.1.2.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces12 = address.Assign (devices12);
// Set up the UDP echo server on Node 2
uint16_t port = 9; // well-known echo port number
UdpEchoServerHelper echoServer (port);
ApplicationContainer serverApps = echoServer.Install (nodes.Get (2));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
// Set up the UDP echo client on Node 0
UdpEchoClientHelper echoClient (interfaces12.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 packet receive tracing on Node 1 and Node 2
Config::Connect (“/NodeList/1/DeviceList/*/MacRx”, MakeBoundCallback (&RxTrace, “Node 1”));
Config::Connect (“/NodeList/2/DeviceList/*/MacRx”, MakeBoundCallback (&RxTrace, “Node 2”));
// Enable tracing
AsciiTraceHelper ascii;
pointToPoint.EnableAsciiAll (ascii.CreateFileStream (“intermediate-delay.tr”));
// Run the simulation
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
- Install applications
In the above example, on Node 2 a UDP echo server is installed. and on Node 2, 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. In our example, packet receive tracing is enabled on Node 1 and Node 2 using the Config::Connect method.
- Calculate Intermediate Delay
Parse the trace file to extract the times of packets at each node and calculate the delay between hops after the simulation.
Example Python Script to Calculate Intermediate Delay
Below is an example python scrip to calculate intermediate delay.
import re
# Initialize dictionaries to store send and receive times
node1_receive_times = {}
node2_receive_times = {}
# Regular expression to match the trace output
pattern = re.compile(r”Packet received at Node (\d) at time (\d+\.\d+) seconds”)
# Parse the trace file
with open(“intermediate-delay.tr”, “r”) as trace_file:
for line in trace_file:
match = pattern.search(line)
if match:
node = match.group(1)
time = float(match.group(2))
if node == “1”:
node1_receive_times[time] = time
elif node == “2”:
node2_receive_times[time] = time
# Calculate intermediate delays
intermediate_delays = []
for time in node1_receive_times:
if time in node2_receive_times:
delay = node2_receive_times[time] – node1_receive_times[time]
intermediate_delays.append(delay)
# Calculate average intermediate delay
average_intermediate_delay = sum(intermediate_delays) / len(intermediate_delays) if intermediate_delays else 0
print(f”Average Intermediate Delay: {average_intermediate_delay} seconds”)
Explanation
- Simulation setup :
Three-node linear topology is created. Those nodes are connected using a point-to-point link.
- Application setup :
On Node 2, a UDP echo server is installed. and On Node 0, a UDP echo client is installed. Configure the client to send a specified number of packets at a specified interval.
- Packet tracing :
To record the receive times of packets, enable packet receive tracing on Node 1 and Node 2.
- Calculate Intermediate Delay :
Parse the trace file to extract the receive times of packets at each node. Calculate the delay between Node 0 and Node 1, and between Node 1 and Node 2, then compute the average intermediate delay.
We have conducted an analysis of the intermediate delay in ns3 by measuring the delay at each hop in a multi-hop network. Additionally, we provide additional information on intermediate delay.
Our developers are pleased to provide you with advice on project performance for your current project. Moreover, we continue with a thorough comparative analysis. Furthermore, we offer comprehensive details on calculating Intermediate Delay in ns3simulation. For ideal outcomes, please call ns3simulation.com.