How to Calculate Network Efficiency in NS2

To calculate the Network efficiency using NS2 (Network Simulator 2), which refers to the capability of a network to send the data to its intended end including minimal overhead, delay, and energy consumption. It reflects how effectively the network uses its resources, like bandwidth, energy, and time. There are numerous metrics we can be used to compute the network efficiency according to the network objectives and network type. These contain:

  1. Throughput Efficiency: The ratio of the valuable data transmitted to the total data transmitted (including overhead).
  2. Energy Efficiency: The ratio of the total amount of data effectively transmitted to the total energy consumed.
  3. Delay Efficiency: How successfully the network maintains the low delays, particularly for time-sensitive data.
  4. Bandwidth Utilization: The amount of bandwidth used well for transmitting valuable data.

General Formula for Network Efficiency:

For throughput efficiency, the formula can be stated as:

Network Efficiency=Useful Data TransmittedTotal Data Transmitted\text{Network Efficiency} = \frac{\text{Useful Data Transmitted}}{\text{Total Data Transmitted}}Network Efficiency=Total Data TransmittedUseful Data Transmitted​

Above formula measures how much of the total sent data is really valuable data (i.e., data that attains its end without being dropped or retransmitted).

Steps to Calculate Network Efficiency in NS2:

  1. Set Up the Network Simulation:
    Describe a network topology within NS2 with traffic sources, routing protocols, and wireless/wired links. Replicate a situation that generates traffic and logs performance metrics like throughput, packet drops, delays, and energy consumption.
  2. Generate the Trace File:
    NS2 makes a trace file in the course of the simulation that includes the data regarding all the packets sent, received, dropped, and forwarded. The trace file is vital for computing the network performance metrics.
  3. Analyze the Trace File:
    Extort related details from the trace file, like the total data sent (including control overhead) and the useful data effectively received at the end.
  4. Calculate Network Efficiency:
    We can be used the proper metrics, like throughput or energy efficiency, to estimate the network efficiency.

Example Tcl Script to Set Up the Network in NS2:

# Create a new simulator instance

set ns [new Simulator]

# Define network nodes

set node0 [$ns node]

set node1 [$ns node]

set node2 [$ns node]

# Create duplex links between the nodes (with defined bandwidth and delay)

$ns duplex-link $node0 $node1 1Mb 10ms DropTail

$ns duplex-link $node1 $node2 500Kb 20ms DropTail

# Attach UDP agents to nodes

set udp0 [new Agent/UDP]

set null0 [new Agent/Null]

$ns attach-agent $node0 $udp0

$ns attach-agent $node2 $null0

$ns connect $udp0 $null0

# Create traffic source (CBR) to generate packets

set cbr0 [new Application/Traffic/CBR]

$cbr0 attach-agent $udp0

$cbr0 set packetSize_ 512

$cbr0 set rate_ 1Mb   ;# High rate to potentially induce congestion

# Schedule traffic start and stop times

$ns at 1.0 “$cbr0 start”

$ns at 5.0 “$cbr0 stop”

# Open trace file to log events

set tracefile [open trace.tr w]

$ns trace-all $tracefile

# Define the finish procedure to close the trace file and stop the simulation

proc finish {} {

global ns tracefile

$ns flush-trace

close $tracefile

exit 0

}

# Schedule the end of the simulation

$ns at 6.0 “finish”

# Run the simulation

$ns run

  1. Trace File Analysis for Throughput and Total Data:

To assess the network efficiency, we require to calculate:

  • Total Data Sent (Including Overhead): It contains data packets, control packets (like routing packets), and any retransmitted packets because of the network issues.
  • Useful Data Received: It is the actual amount of data effectively sent to the end.

Example Trace File Output:

s 1.0 _0_ AGT — 512 [0 0 0 0] ——- [1:0 2:0 32 0]

r 1.2 _2_ AGT — 512 [0 0 0 0] ——- [1:0 2:0 32 0]

d 1.3 _1_ AGT — 512 [0 0 0 0] ——- [1:0 2:0 32 0]

  • s: Packet sent.
  • r: Packet received.
  • d: Packet dropped.
  1. Calculate Network Efficiency (Throughput-Based):

To estimate the throughput efficiency, compare the total data received (useful data) to the total data transferred (including control messages and retransmitted packets).

Bash Script to Calculate Network Efficiency:

# Count the total number of data packets sent

sent_packets=$(grep “^s” trace.tr | grep “_0_” | wc -l)

# Count the total number of data packets received

received_packets=$(grep “^r” trace.tr | grep “_2_” | wc -l)

# Define packet size in bytes (512 bytes)

packet_size=512

# Convert packet size to bits (1 byte = 8 bits)

packet_size_bits=$(echo “$packet_size * 8” | bc)

# Calculate total data sent and received in bits

total_data_sent=$(echo “$sent_packets * $packet_size_bits” | bc)

total_data_received=$(echo “$received_packets * $packet_size_bits” | bc)

# Calculate network efficiency as the ratio of received data to sent data

if [ $total_data_sent -gt 0 ]; then

network_efficiency=$(echo “scale=2; $total_data_received / $total_data_sent” | bc)

echo “Network Efficiency: $network_efficiency”

else

echo “No data was sent.”

fi

  1. Calculating Energy Efficiency:

If we are replicating the wireless network in which energy consumption is significant then we can be computed the energy efficiency as the amount of data effectively sent per unit of energy consumed. It is specifically useful in scenarios like wireless sensor networks.

We could require to:

  • Describe an energy model for the nodes.
  • Track the total energy consumed by the nodes in the course of the simulation.
  • Estimate the energy efficiency as:

Energy Efficiency (bits/Joule)=Total Data Transmitted (bits)Total Energy Consumed (Joules)\text{Energy Efficiency (bits/Joule)} = \frac{\text{Total Data Transmitted (bits)}}{\text{Total Energy Consumed (Joules)}}Energy Efficiency (bits/Joule)=Total Energy Consumed (Joules)Total Data Transmitted (bits)​

Example for Energy Efficiency:

We can extorted the remaining energy of the nodes from the trace file at the destination of the simulation. Deduct the remaining energy from the starting energy to assess the total energy consumed.

# Define initial energy and transmission power for each node

set val(initialEnergy) 100.0   ;# Joules

set val(txPower) 0.5    ;# Transmission power (Watts)

set val(rxPower) 0.3    ;# Reception power (Watts)

set val(idlePower) 0.01 ;# Idle power (Watts)

# Assign energy model to nodes

for {set i 0} {$i < 3} {incr i} {

$node($i) energy-model EnergyModel

$node($i) add-energy $val(initialEnergy)

}

After the simulation, we extract the remaining energy also compute the energy efficiency likewise to how we estimate the throughput efficiency.

  1. Average End-to-End Delay Efficiency:

If the focus is on delay efficiency then we can be computed the average delay for packets delivered and measure how efficient the network is in reducing delay.

# Extract the sending and receiving times from the trace file

grep “^s” trace.tr > sent_packets.txt

grep “^r” trace.tr > received_packets.txt

# Calculate the delay for each packet

awk ‘FNR==NR{sent[$6]=$2; next} {if ($6 in sent) print $2 – sent[$6]}’ sent_packets.txt received_packets.txt > delays.txt

# Calculate average delay

awk ‘{sum+=$1; count+=1} END {if (count > 0) print “Average Delay: “, sum/count, ” seconds”; else print “No packets received.”}’ delays.txt

  1. Overall Network Efficiency:

The overall network efficiency can estimate as a weighted average of the various metrics (throughput, energy, and delay) based on the significance of each aspect in the particular network.

Summary:

  1. Run the simulation and generate a trace file within NS2.
  2. Estimate the trace file to extract useful metrics like total data transmitted, total data received, packet drop rate, and energy consumed.
  3. Compute the network efficiency using the suitable formula rely on the focus (throughput, energy, delay).
  4. We can be used the bash scripts or physical calculations to automate the extraction and computation of these metrics from the trace file.

Here, we had calculated the Network Efficiency utilising the computation process and some general formula within NS2 simulation environment. Likewise, we will be shared further concepts on this topic in upcoming manual.

For any types of Network Efficiency project in NS2  feel free to contact us we provide you with best results. Drop us all your parameter details we will guide you with performance analysis results.