How to Calculate Throughput in ns3
To calculate throughput in ns3, we need to simulate the parameter in network simulator that denotes the rate at which data is completely transferred from one point to another. In ns3, we need to compute throughput by measuring amount of data that achieves concluded period of time.
Example: Calculating Throughput
The given below is the instance to configure the point to point network in ns3 and evaluates the throughput:
Setting Up the Network Simulation
#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”
#include <iostream>
using namespace ns3;
NS_LOG_COMPONENT_DEFINE(“ThroughputExample”);
uint64_t totalBytesReceived = 0;
void PacketReceivedCallback(Ptr<const Packet> packet, const Address &address)
{
totalBytesReceived += packet->GetSize();
}
int main(int argc, char *argv[])
{
Time::SetResolution(Time::NS);
LogComponentEnable(“ThroughputExample”, LOG_LEVEL_INFO);
// Create nodes
NodeContainer nodes;
nodes.Create(2);
// Set up point-to-point link
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute(“DataRate”, StringValue(“5Mbps”));
pointToPoint.SetChannelAttribute(“Delay”, StringValue(“2ms”));
NetDeviceContainer devices;
devices = pointToPoint.Install(nodes);
// Install the Internet stack
InternetStackHelper stack;
stack.Install(nodes);
Ipv4AddressHelper address;
address.SetBase(“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = address.Assign(devices);
// Set up UDP server on Node 1
uint16_t port = 9;
UdpServerHelper server(port);
ApplicationContainer serverApp = server.Install(nodes.Get(1));
serverApp.Start(Seconds(1.0));
serverApp.Stop(Seconds(10.0));
// Set up UDP client on Node 0
UdpClientHelper client(interfaces.GetAddress(1), port);
client.SetAttribute(“MaxPackets”, UintegerValue(320));
client.SetAttribute(“Interval”, TimeValue(MilliSeconds(50)));
client.SetAttribute(“PacketSize”, UintegerValue(1024));
ApplicationContainer clientApp = client.Install(nodes.Get(0));
clientApp.Start(Seconds(2.0));
clientApp.Stop(Seconds(10.0));
// Connect packet received callback
Config::ConnectWithoutContext(“/NodeList/1/ApplicationList/0/$ns3::UdpServer/Rx”, MakeCallback(&PacketReceivedCallback));
Simulator::Run();
// Calculate throughput
double duration = 10.0 – 1.0; // Duration in seconds (end time – start time)
double throughput = (totalBytesReceived * 8) / duration; // in bits per second
std::cout << “Total Bytes Received: ” << totalBytesReceived << ” bytes” << std::endl;
std::cout << “Duration: ” << duration << ” seconds” << std::endl;
std::cout << “Throughput: ” << throughput << ” bps” << std::endl;
Simulator::Destroy();
return 0;
}
Explanation:
- Global Variable:
-
- totalBytesReceived: A counter to keep track of the total number of bytes received.
- Simulation Setup:
-
- Two nodes are created and connected using a point-to-point link.
- The link is configured with a specific data rate and delay.
- Application Setup:
-
- A UDP server is installed on the destination node (Node 1).
- A UDP client is installed on the source node (Node 0) to send packets to the server.
- Packet Received Callback:
-
- PacketReceivedCallback: A callback function that is triggered each time a packet is received by the server. It increments the totalBytesReceived counter by the size of the received packet.
- Connecting the Callback:
-
- Use Config::ConnectWithoutContext to connect the packet received callback to the UDP server application on Node 1.
- Running the Simulation:
-
- The simulation runs for a specified period, during which packets are sent and received.
- After the simulation ends, the throughput is calculated by dividing the total number of bits received by the duration of the simulation.
- Calculating Throughput:
-
- Calculate the duration of the data transfer period.
- Calculate the throughput in bits per second and print the result.
Finally, we discussed about how the throughput is computed in a particular period of time by using ns3 tool. We also provide more information about the evaluation of throughput when it simulated using other tools. Connect with ns3simulation.com for your networking comparison analysis from our developers. We can help you with calculating Throughput in the ns3 tool. We offer support for any ns3simulation projects needing Throughput calculations.