How to Calculate Download rate in ns3

To calculate the download rate in ns3, we have to measure the amount of data received for a period of time. We can achieve this by tracking the number of bytes received at the application layer and dividing it by the total time taken. Additionally, we offer comprehensive project details regarding Download rate.

Below is the example to set up a basic point-to-point network in ns3 and to calculate the download rate.

Example implementation

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

uint64_t totalBytesReceived = 0;

Time startTime, endTime;

void PacketReceivedCallback(Ptr<const Packet> packet, const Address &address)

{

totalBytesReceived += packet->GetSize();

if (startTime == Seconds(0))

{

startTime = Simulator::Now();

}

endTime = Simulator::Now();

}

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

{

Time::SetResolution(Time::NS);

LogComponentEnable(“DownloadRateExample”, 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 download rate

double duration = (endTime – startTime).GetSeconds();

double downloadRate = (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 << “Download Rate: ” << downloadRate << ” bps” << std::endl;

Simulator::Destroy();

return 0;

}

Explanation:

  1. Global Variables:
    • To keep track of the total number of bytes, a counter receivedtotalBytesReceived is used.
    • To store the start and end times of the download, startTime and endTime: Variables are used.
  2. Simulation Setup:
    • Created two nodes and they are connected using a point-to-point link.
    • The link is configured with a specific data rate and delay.
  3. Application Setup:
    • On the destination node (Node 1), a UDP server is installed.
    • On the source node (Node 0), A UDP client is installed to send packets to the server.
  4. Packet Received Callback:
    • A callback function named PacketReceivedCallback is used to trigger each time a packet is received by the server. It increments the totalBytesReceived counter and updates the startTime and endTime variables.
  5. Connecting the Callback:
    • To connect the packet received callback to the UDP server application on Node 1, use Config::ConnectWithoutContext.
  6. Running the Simulation:
    • For a specified period, the simulation runs during which packets are sent and received.
    • After the simulation ends, calculate the download rate by dividing the total number of bits received by the duration of the download period.
  7. Calculating the Download Rate:
    • Calculate the duration of the download period.
    • Calculate the download rate in bits per second and print the result.

On the whole, we had an analysis on download rate by calculating it using ns3. Throughout the process we had the amount of data received over a period of time. Also, we offer a detailed explanation on Download rate.

We provide you with advice on how your project will proceed following the analysis of its performance. Therefore, we will assist you in determining the download rate for your specific parameters, ensuring you receive accurate results.