How to Calculate Time Delay in NS2

To compute time delay or end-to-end delay in ns2, has defined to the amount of time it takes for a packet to moves from the source node to the destination node. Time delay is significant parameters for measuring the network performance specifically in real-time applications like video streaming, VoIP, and online gaming.

The end-to-end delay for a packet involves numerous kinds of delays:

  • Transmission delay: Time to push all packet bits onto the transmission medium.
  • Propagation delay: Time for the signal to disseminate across the medium.
  • Queueing delay: Time the packet spends in queues at intermediate routers or switches.
  • Processing delay: Time spent on packet examination and processing at intermediate nodes.

Steps to Calculate Time Delay in NS2

  1. Set Up the NS2 Simulation:

Initially, we need to configure a simulation in which packets are routed among nodes. NS2 creates a trace file that logs the transmission and reception times of each packet that can use to compute the time delay.

Example TCL Script for Data Transmission:

# Create the simulator

set ns [new Simulator]

# Create two nodes: source and destination

set node_(0) [$ns node]

set node_(1) [$ns node]

# Create a duplex link between the nodes (1 Mbps bandwidth, 10ms delay)

$ns duplex-link $node_(0) $node_(1) 1Mb 10ms DropTail

# Attach UDP agents to nodes for sending and receiving data

set udp0 [new Agent/UDP]

set sink [new Agent/Null]

$ns attach-agent $node_(0) $udp0

$ns attach-agent $node_(1) $sink

$ns connect $udp0 $sink

# Set up Constant Bit Rate (CBR) traffic on the UDP agent

set cbr0 [new Application/Traffic/CBR]

$cbr0 attach-agent $udp0

$cbr0 set packetSize_ 512        ;# Packet size in bytes

$cbr0 set rate_ 1Mb              ;# Transmission rate

# Start and stop the traffic

$ns at 1.0 “$cbr0 start”

$ns at 10.0 “$cbr0 stop”

# Enable tracing to capture packet transmission and reception

set tracefile [open out.tr w]

$ns trace-all $tracefile

# Close trace file at the end of the simulation

proc finish {} {

global ns tracefile

$ns flush-trace

close $tracefile

exit 0

}

# End simulation at time 12 seconds

$ns at 12.0 “finish”

  1. Trace File Overview:

After executing the simulation, NS2 creates a trace file (out.tr) that logs packet-level events like transmissions, receptions, and drops. To compute the time delay, we can measure the send (s) and receive (r) events in the trace file.

Example Trace File Entries:

s 2.000000000 _0_ AGT  — 512 cbr 0 0.0 1.0 1.0

r 2.010000000 _1_ AGT  — 512 cbr 0 0.0 1.0 1.0

s 3.000000000 _0_ AGT  — 512 cbr 1 0.0 2.0 2.0

r 3.010000000 _1_ AGT  — 512 cbr 1 0.0 2.0 2.0

In the trace file:

  • s: A packet was sent at a particular time.
  • r: A packet was received at a particular time.
  • _0_ and _1_: Node IDs (source node _0_, destination node _1_).
  • 512: Packet size in bytes.
  • The time for each event is recorded at the beginning of each line (e.g., 2.000000000 for packet transmission and 2.010000000 for packet reception).
  1. Calculating Time Delay:

The end-to-end delay for a packet is the difference among the time a packet is sent and the time it is received at the destination node.

Delay=Time of Reception−Time of Transmission\text{Delay} = \text{Time of Reception} – \text{Time of Transmission}Delay=Time of Reception−Time of Transmission

To estimate the latency for each packet, we need to fits the s (send) and r (receive) events for the same packet, using the packet ID (e.g., 0, 1, 2 in the trace file).

AWK Script to Calculate Average Delay:

We can use an AWK script to compute the average latency for all packets sent and received in course of the simulation.

BEGIN {

total_delay = 0;

received_packets = 0;

}

# Record the time each packet was sent

$1 == “s” && $4 == “_0_” && $7 == “cbr” {

packet_time[$10] = $2;  # Store the transmission time indexed by packet ID

}

# Calculate the delay for each received packet

$1 == “r” && $4 == “_1_” && $7 == “cbr” {

delay = $2 – packet_time[$10];  # Subtract transmission time from reception time

total_delay += delay;

received_packets++;

}

END {

avg_delay = total_delay / received_packets;

print “Average End-to-End Delay: ” avg_delay ” seconds”;

}

  • The script records the send time of each packet and then computes the latency when the packet is received.
  • Overall, the script calculates the average end-to-end delay over all packets.
  1. Running the AWK Script:

Save the above script as delay.awk, and then execute it on the generated trace file out.tr:

awk -f delay.awk out.tr

  1. Other Considerations for Delay:
  • Propagation delay is the time it takes for a signal to travel via the network medium, define by the distance among nodes and the speed of signal propagation.
  • Queueing delay happens when a packet is buffered in a queue despite the fact waiting for transmission.
  • Processing delay occurs when a packet is processed by transmitter or intermediate devices.
  • Transmission delay is the time to push all the bits of the packet onto the transmission medium.

All of these latency contribute to the overall end-to-end delay.

  1. Conclusion:

To compute time delay in NS2:

  1. Set up a simulation in which packets are routed among source and destination nodes.
  2. Enable tracing to log packet transmission and reception events.
  3. Analyse the trace file using an AWK script to compute the time difference among the transmission and reception of each packet.
  4. Calculate the average delay over all received packets to measure the performance of the network in terms of delay.

Through this approach, we understood the procedures to calculate the time delay in the real time application that were executed using ns2 simulation. We also offer the valuable insights regarding how the time delay will perform in other simulation tools. For assistance in calculating time delay using the NS2 tool for your project, please reach out to us for expert guidance and clear explanations. Our focus is on enhancing network performance, particularly in real-time applications such as video streaming, VoIP, and online gaming.