How to Calculate Network Throughput in NS2
To calculate network throughput in ns2, we need to define the rate at which data is effectively distributed from a source to a destination. It is usually expressed in bits per second (bps). Throughput calculation has contained to evaluate the trace file created by NS2 to evaluate the amount of data received at the destination and the time over that this data was routed. The given below are the procedures to implement the network throughput in ns2:
Steps to Calculate Network Throughput in NS2
- Set Up the NS2 Simulation
Initially, we required to configure a basic simulation with nodes and links and create traffic using agents such as UDP with CBR (Constant Bit Rate) traffic.
Example TCL Script for Data Transmission:
# Create the simulator
set ns [new Simulator]
# Define the topology with 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 (1 Mbps)
# Start and stop the traffic
$ns at 1.0 “$cbr0 start”
$ns at 10.0 “$cbr0 stop”
# Set the simulation time
$ns run
- Run the Simulation
To execute the simulation, run the TCL script using NS2:
ns your_simulation_script.tcl
This will create a trace file that encompasses data about packet transmissions, receptions, and other network events.
- Analyze the Trace File
NS2 created a trace file that records events such as packet transmission (s for sent) and packet reception (r for received). We can utilize this trace file to compute throughput by evaluating the number of bytes received at the destination and dividing it by the time over which these bytes were received.
Sample Trace File Entries:
s 1.000000000 _0_ AGT — 512 cbr 0 0.0 1.0 1.0
r 1.010000000 _1_ AGT — 512 cbr 0 0.0 1.0 1.0
s 2.000000000 _0_ AGT — 512 cbr 1 0.0 2.0 2.0
r 2.010000000 _1_ AGT — 512 cbr 1 0.0 2.0 2.0
In this trace:
- s: Packet sent event.
- r: Packet received event.
- 512: Packet size in bytes.
- 0: Source node.
- 1: Destination node.
- Calculate Throughput
To estimate throughput, use the formula:
Throughput=Total Data Received (in bits)Total Time (in seconds)\text{Throughput} = \frac{\text{Total Data Received (in bits)}}{\text{Total Time (in seconds)}}Throughput=Total Time (in seconds)Total Data Received (in bits)
Where:
- Total Data Received is the sum of the sizes of the packets received (in bits).
- Total Time is the time interval over that the data was received.
AWK Script to Calculate Throughput
The following AWK script can be used to compute throughput from the trace file:
BEGIN {
total_bytes = 0
start_time = -1
end_time = -1
}
# Capture packet reception at the destination (_1_)
$1 == “r” && $4 == “_1_” && $7 == “cbr” {
if (start_time == -1) {
start_time = $2 # Record the start time of the first packet received
}
end_time = $2 # Continuously update the end time with the latest packet reception time
total_bytes += $8 # Add the size of the packet (8th field) to total_bytes
}
END {
if (start_time != -1 && end_time != -1 && end_time > start_time) {
duration = end_time – start_time # Calculate the time duration over which packets were received
throughput = (total_bytes * 8) / duration # Convert total_bytes to bits and calculate throughput
print “Throughput: ” throughput ” bps”
} else {
print “No data received”
}
}
- Run the AWK Script
Save the AWK script as throughput.awk and executed it on the trace file created by NS2:
awk -f throughput.awk out.tr
This script computes the total amount of data received and divides it by the total time that delivers the throughput in bps (bits per second).
Example Output
Throughput: 512000 bps
This means that the network throughput is 512 kbps according to the total data received and the time duration over which the data was routed.
- Conclusion
To compute network throughput in NS2:
- Set up a simulation with data transmission among nodes.
- Run the simulation to create a trace file.
- Analyse the trace file using an AWK script to calculate the total amount of data received and the time period over which it was received.
- Calculate throughput by dividing the total data (in bits) by the total time (in seconds).
In this simulation, we clearly demonstrated the step by procedures to completely compute the network throughput in ns2 simulation environment. We plan to deliver more information regarding the network throughput.
Approach ns2project.com for help in calculating network throughput in NS2. It’s challenging, but we are here to guide you to the best results.