How to Calculate Network Data Rate in NS2

To calculate the Network Data Rate (or throughput) in NS2 means that the count of data successfully transferred through the network per unit of time, usually computed in bits per second (bps), kilobits per second (kbps) or megabits per second (Mbps). It encompasses assessing the packet transmission and reception events in the simulation and defining how much data is being successfully delivered over a particular period.

The following step-by-step will get you started with the calculation of data rate using ns2:

Steps to Calculate Network Data Rate (Throughput) in NS2

  1. Generate a Trace File

Make sure your NS2 simulation is creating a trace file, which will log packet transmission (+), reception (r), and other related events. This file will be used to estimate the total number of data received over a certain time.

Include the below lines to your NS2 script to allow trace generation:

set tracefile [open out.tr w]

$ns trace-all $tracefile

The generated trace file will log events with details like timestamps, packet types, packet sizes, and more.

  1. Identify Relevant Events in the Trace File

Measure the data rate by analysing packet reception (r) events, which represent successful delivery of packets.

Here’s an example of a typical line from a trace file:

r 0.45678 1 0 tcp 1040 ——- [0 1 0 0] ——- [0:0 1:0 32 0] [0] 0 0

In this line:

  • r: Packet received successfully.
  • 0.45678: Timestamp (when the packet was received).
  • 1: Destination node.
  • 0: Source node.
  • tcp: Protocol (e.g., TCP, UDP, etc.).
  • 1040: Packet size in bytes.
  1. Calculate Network Data Rate Using an AWK Script

To measure the network data rate, you can use an AWK script to:

  • Extract the size of received packets.
  • Gather the total number of data obtained.
  • Divide by the total simulation time to acquire the data rate (throughput).

AWK Script to Calculate Throughput:

awk ‘{

if ($1 == “r” && $4 == “tcp”) {

total_bytes += $6;  # $6 is the packet size in bytes

}

} END {

simulation_time = 100.0;  # Replace with your actual simulation time in seconds

throughput = total_bytes * 8 / simulation_time;  # Convert bytes to bits

throughput_kbps = throughput / 1000;  # Convert to kilobits per second (Kbps)

throughput_mbps = throughput / 1000000;  # Convert to megabits per second (Mbps)

print “Throughput: ” throughput ” bps (” throughput_kbps ” kbps, ” throughput_mbps ” Mbps)”;

}’ out.tr

This script:

  • Summing up the total number of bytes received (successful packet deliveries).
  • Multiplies by 8 to convert bytes to bits.
  • Divides by the simulation time to calculate the throughput in bits per second (bps).
  • Converts the result into Kbps and Mbps for convenience.
  1. Interpret the Results
  • Throughput (bps): The count of bits transmitted successfully per second.
  • Throughput (Kbps): Divide the throughput in bps by 1000 to get kilobits per second.
  • Throughput (Mbps): Divide the throughput in bps by 1,000,000 to get megabits per second.

Example Output

If the total amongst of bytes acquired over a 100-second simulation is 5,000,000 bytes, the AWK script will calculate:

  • Throughput in bps = 5,000,000 * 8 / 100 = 400,000 bps.
  • Throughput in Kbps = 400,000 / 1000 = 400 Kbps.
  • Throughput in Mbps = 400,000 / 1,000,000 = 0.4 Mbps.
  1. Calculate Data Rate for Specific Flows

If you want to quantify the data rate for specific flows (such as amongst certain nodes or using a particular protocol), you can alter the AWK script to filter for specific source-destination pairs or protocols.

For example, to calculate the data rate between node 0 and node 1 using TCP packets:

awk ‘{

if ($1 == “r” && $4 == “tcp” && $3 == 1 && $2 == 0) {

total_bytes += $6;

}

} END {

simulation_time = 100.0;  # Replace with your actual simulation time in seconds

throughput = total_bytes * 8 / simulation_time;

throughput_kbps = throughput / 1000;

throughput_mbps = throughput / 1000000;

print “Throughput for flow 0 -> 1: ” throughput ” bps (” throughput_kbps ” kbps, ” throughput_mbps ” Mbps)”;

}’ out.tr

This will give you the throughput for traffic amongst node 0 and node 1 specifically.

  1. Calculate Time-Windowed Data Rate

You can also calculate the data rate over particular time intervals to assess how the throughput varies over time. For instance, if you want to measure the throughput in 1-second intervals, you can adjust the AWK script:

awk ‘{

if ($1 == “r” && $4 == “tcp”) {

interval = int($2);  # Group by time intervals (1 second)

packet_count[interval] += $6;

}

} END {

for (interval in packet_count) {

throughput = packet_count[interval] * 8;  # Convert bytes to bits

print “Time Interval: ” interval “, Throughput: ” throughput ” bps”;

}

}’ out.tr

This script estimates the total amount of bytes obtained in each 1-second interval, then converts the result to bps. It gives you the throughputs per second, which assist you to evaluate throughput changes over time.

  1. Data Rate for Wireless Networks

For wireless networks, the data rate can differ more significantly because of factors like intrusive, mobility, and signal strength. You can use the same process to calculate throughput in wireless networks, however it’s necessary to consider the influence of node mobility and signal quality.

When setting up wireless simulations, the throughput can be affected by parameters like:

  • Transmission range.
  • Propagation models (e.g., Two-Ray Ground, Free-Space).
  • Node mobility (if using a mobility model like Random Waypoint).

You can build wireless network parameters in NS2 as follows:

# Setup wireless simulation

set val(chan)           Channel/WirelessChannel    ;# Wireless channel

set val(prop)           Propagation/TwoRayGround   ;# Propagation model

set val(netif)          Phy/WirelessPhy            ;# Wireless PHY layer

set val(mac)            Mac/802_11                 ;# MAC layer

set val(ifq)            Queue/DropTail/PriQueue    ;# Queue type

set val(ll)             LL                         ;# Link layer

set val(ant)            Antenna/OmniAntenna        ;# Antenna model

# Set data rate (packet size)

$tcp set packetSize_ 1024  ;# Packet size in bytes

Then, follow the same technique of calculating throughput from the trace file.

Make use of the delivered steps and instructions which make it easier for you to focus on the calculation of Network Data Rate by setting up the simulation using ns2 tool. You can also estimate the Time-Windowed Data Rate and for wireless networks. For any type of Network Data Rate project in NS2, please do not hesitate to reach out to us; we are committed to delivering the best results.