Ns3 Projects for B.E/B.Tech M.E/M.Tech PhD Scholars.  Phone-Number:9790238391   E-mail: ns3simulation@gmail.com

How to Calculate Path Latency in ns3

To calculate the path latency in ns3, we need to compute the time taken for packet to monitor from source node to destination node over the network path. At each hop path latency contains transmission delay, Propagation delay, and processing delay.

Here, are the procedures to calculate the path latency in ns3:

  1. Set up Your Simulation Environment: Generate a network topology; setup nodes, links, and protocols.
  2. Install Applications: Set up applications on the nodes to create and receive traffic.
  3. Trace the Packets: Use ns3 tracing capabilities to record the send and receive times of packets.
  4. Calculate Path Latency: Subtract the send time from the receive time for each packet.

Example: Multi-Hop Network

Here, we generate the basic multi-hop network topology with three nodes and evaluate the latency path.

Step 1: Set Up Your Simulation Environment

#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 (“PathLatencyExample”);

void PacketSent (Ptr<const Packet> packet)

{

NS_LOG_UNCOND (“Packet sent at time ” << Simulator::Now ().GetSeconds ());

}

void PacketReceived (Ptr<const Packet> packet)

{

NS_LOG_UNCOND (“Packet received at time ” << Simulator::Now ().GetSeconds ());

}

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 (10));

echoClient.SetAttribute (“Interval”, TimeValue (Seconds (1.0))); // 1 packet 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

devices.Get (0)->TraceConnectWithoutContext (“MacTx”, MakeCallback (&PacketSent));

devices.Get (2)->TraceConnectWithoutContext (“MacRx”, MakeCallback (&PacketReceived));

// Run the simulation

Simulator::Run ();

Simulator::Destroy ();

return 0;

}

Step 2: Install Applications

In this example, a UDP echo server is installed on Node 2, and a UDP echo client is installed on Node 0. The client sends 10 packets, each 1024 bytes in size, at an interval of 1 second.

Step 3: Trace the Packets

Packet transmission and reception are traced using callbacks. The PacketSent and PacketReceived functions are used to log the event times.

Step 4: Calculate Path Latency

After running the simulation, parse the log to extract the send and receive times of packets, and calculate the path latency.

Example Python Script to Calculate Path Latency

import re

send_times = []

receive_times = []

# Regular expressions to match the log output

send_pattern = re.compile(r”Packet sent at time (\d+\.\d+)”)

receive_pattern = re.compile(r”Packet received at time (\d+\.\d+)”)

# Parse the log file

with open(“ns3-output.log”, “r”) as log_file:

for line in log_file:

send_match = send_pattern.search(line)

receive_match = receive_pattern.search(line)

if send_match:

send_times.append(float(send_match.group(1)))

if receive_match:

receive_times.append(float(receive_match.group(1)))

# Calculate Path Latency

latencies = [recv – send for send, recv in zip(send_times, receive_times)]

average_latency = sum(latencies) / len(latencies) if latencies else 0

print(f”Average Path Latency: {average_latency} seconds”)

Explanation

In this place, we have explained the process for path latency in ns3:

  1. Set up Your Simulation Environment: Create a multi-hop network topology with three nodes and configure point-to-point links between them.
  2. Install Applications: Install a UDP echo server on Node 2 and a UDP echo client on Node 0. Configure the client to send a specified number of packets at a specified interval.
  3. Trace the Packets: Use callbacks to log the times when packets are sent from Node 0 and received at Node 2.
  4. Calculate Path Latency: Parse the log file to extract send and receive times, then calculate the path latency by subtracting the send time from the receive time for each packet.

Eventually, in this place we suggested the path latency will compute the delay among the send and received packets in ns3. Also, we elaborate how the path latency computes the latency in other simulated tools.  Struggling hard to get networking comparison analysis then connect with ns3simulation.com we will help you in calculating Path Latency in ns3tool.