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

How to Calculate Network Throughput in ns3

To calculate throughput in ns3, we need to measure the amount of data successfully received at the destination node over a specific period. Throughput is typically measured in bits per second (bps) and for evaluating the performance of network protocols and applications it is an essential metric. Here is the complete guide to calculate throughput in ns3.

Steps for calculation

  1. Set up your ns3 :
  • Make sure that ns3 is installed in the computer. If not, install it.
  1. Create a new ns3 script :
  • In the scratch directory of ns3, create a new script.
  1. Include necessary libraries :
  • In your script, include the necessary libraries.
  1. Define network topology :
  • For your network topology, create multiple nodes.
  1. Simulate data transmission :
  • To simulate the receiver and generate traffic from the source, use PacketSink function.
  1. Measure throughput :
  • To calculate the throughput, track the amount of data received over time.

Example for calculating throughput in ns3

Here is the example for the calculation of throughput:

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

void CalculateThroughput(Ptr<PacketSink> sink, Time interval)

{

static uint64_t lastTotalRx = 0;

uint64_t totalRx = sink->GetTotalRx();

uint64_t rxBytes = totalRx – lastTotalRx;

lastTotalRx = totalRx;

double throughput = (rxBytes * 8) / interval.GetSeconds(); // bits per second

std::cout << Simulator::Now().GetSeconds() << “s: Throughput = ” << throughput / 1e6 << ” Mbps” << std::endl;

Simulator::Schedule(interval, &CalculateThroughput, sink, interval); // Schedule next calculation

}

void GenerateTraffic(Ptr<Socket> socket, uint32_t pktSize, uint32_t pktCount, Time interPacketInterval)

{

if (pktCount > 0)

{

socket->Send(Create<Packet>(pktSize));

Simulator::Schedule(interPacketInterval, &GenerateTraffic, socket, pktSize, pktCount – 1, interPacketInterval);

}

}

int main(int argc, char *argv[])

{

uint32_t nNodes = 2; // Number of nodes

double simulationTime = 10.0; // Total simulation time in seconds

Time calculationInterval = Seconds(1.0); // Interval to calculate throughput

uint32_t packetSize = 1024; // Packet size in bytes

uint32_t numPackets = 1000; // Number of packets to send

Time interPacketInterval = MilliSeconds(10); // Interval between packets

CommandLine cmd;

cmd.AddValue(“nNodes”, “Number of nodes”, nNodes);

cmd.AddValue(“simulationTime”, “Total simulation time”, simulationTime);

cmd.AddValue(“calculationInterval”, “Interval to calculate throughput”, calculationInterval);

cmd.AddValue(“packetSize”, “Size of each packet”, packetSize);

cmd.AddValue(“numPackets”, “Number of packets to send”, numPackets);

cmd.AddValue(“interPacketInterval”, “Interval between packets”, interPacketInterval);

cmd.Parse(argc, argv);

NodeContainer nodes;

nodes.Create(nNodes);

PointToPointHelper pointToPoint;

pointToPoint.SetDeviceAttribute(“DataRate”, StringValue(“5Mbps”));

pointToPoint.SetChannelAttribute(“Delay”, StringValue(“2ms”));

NetDeviceContainer devices;

devices = pointToPoint.Install(nodes);

InternetStackHelper stack;

stack.Install(nodes);

Ipv4AddressHelper address;

address.SetBase(“10.1.1.0”, “255.255.255.0”);

Ipv4InterfaceContainer interfaces = address.Assign(devices);

uint16_t port = 9;

Address sinkAddress(InetSocketAddress(interfaces.GetAddress(1), port));

PacketSinkHelper packetSinkHelper(“ns3::UdpSocketFactory”, sinkAddress);

ApplicationContainer sinkApps = packetSinkHelper.Install(nodes.Get(1));

sinkApps.Start(Seconds(0.0));

sinkApps.Stop(Seconds(simulationTime));

Ptr<Socket> sourceSocket = Socket::CreateSocket(nodes.Get(0), TypeId::LookupByName(“ns3::UdpSocketFactory”));

sourceSocket->Connect(sinkAddress);

Simulator::ScheduleWithContext(sourceSocket->GetNode()->GetId(), Seconds(1.0), &GenerateTraffic, sourceSocket, packetSize, numPackets, interPacketInterval);

Simulator::Schedule(calculationInterval, &CalculateThroughput, DynamicCast<PacketSink>(sinkApps.Get(0)), calculationInterval);

Simulator::Stop(Seconds(simulationTime));

Simulator::Run();

Simulator::Destroy();

return 0;

}

Explanation

  1. Nodes and links :

Two nodes are created. Point-to-point links between nodes are configured. o Set up the network with nodes connected using point-to-point links.

  1. Applications :

On one node, a PacketSink is installed to simulate receiver. and On another node, UDP sockets are created to send packets.

  1. Traffic generation :

To send packets from the source node to the sink node at regular intervals,  GenerateTraffic is implemented.

  1. Throughput calculation :

To calculate the throughput by measuring the amount of data received over time, CalculateThroughput is implemented. Scheduled the throughput calculation at regular intervals using the Simulator::Schedule function.

  1. Running the Simulation :

The simulation runs by sending and receiving the packets. Throughput is calculated and printed at regular intervals.

So now we had performed analysis on calculating throughput in ns3 by measuring the amount of data successfully received at the destination node over a specific period. Also, we provide more related information on throughput.

If you require additional support in performance analysis focused on throughput in ns3, we invite you to share your specifications with us. ns3simulation.com will provide a detailed analysis of the results.