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 Delay Variance in Ns3

To Calculate delay variance (jitter) in ns3, we need to follow several steps. Because Jitter is one of the most important metric for analysing the performance of real-time applications such as VoIP and video streaming.

In this delay variance we have to measure the variability in packet delay time for calculation process.

Steps to Calculate Delay Variance in ns3

  1. Set Up ns3 Environment:
    • Make sure ns3 is installed on the system.
  2. Create a New ns-3 Script:
    • Create a new script file in the scratch directory of ns3, e.g., delay_variance_calculation.cc.
  3. Include Necessary Headers:
    • Include the necessary ns3 headers in the script.
  4. Define Network Topology:
    • Set up a network topology with multiple nodes.
  5. Simulate Data Transmission:
    • Use the PacketSink application to simulate the receiver and generate traffic from the source.
  6. Measure Delay and Calculate Variance:
    • Track the delay of each packet received and calculate the variance of these delays.

Example Code:

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

std::vector<double> delaySamples;

void PacketReceiveCallback(Ptr<Socket> socket)

{

Ptr<Packet> packet;

Address from;

while ((packet = socket->RecvFrom(from)))

{

Time sendTime;

packet->PeekPacketTag(sendTime);

Time delay = Simulator::Now() – sendTime;

delaySamples.push_back(delay.GetSeconds());

std::cout << “Packet received with delay: ” << delay.GetSeconds() << ” seconds” << std::endl;

}

}

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

{

if (pktCount > 0)

{

Ptr<Packet> packet = Create<Packet>(pktSize);

packet->AddPacketTag(Simulator::Now());

socket->Send(packet);

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

}

}

double CalculateVariance(const std::vector<double>& data)

{

double sum = std::accumulate(data.begin(), data.end(), 0.0);

double mean = sum / data.size();

double variance = 0.0;

for (double value : data)

{

variance += (value – mean) * (value – mean);

}

variance /= data.size();

return variance;

}

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

{

uint32_t nNodes = 2;

double simulationTime = 10.0; // seconds

uint32_t packetSize = 1024; // bytes

uint32_t numPackets = 100; // number of packets

Time interPacketInterval = MilliSeconds(100); // interval between packets

CommandLine cmd;

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

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

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);

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

InetSocketAddress local = InetSocketAddress(Ipv4Address::GetAny(), 9);

recvSocket->Bind(local);

recvSocket->SetRecvCallback(MakeCallback(&PacketReceiveCallback));

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

InetSocketAddress remote = InetSocketAddress(interfaces.GetAddress(1), 9);

sourceSocket->Connect(remote);

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

Simulator::Stop(Seconds(simulationTime));

Simulator::Run();

Simulator::Destroy();

 

double delayVariance = CalculateVariance(delaySamples);

std::cout << “Delay Variance: ” << delayVariance << ” seconds^2″ << std::endl;

return 0;

}

Explanation:

  1. Nodes and Links:
    • Created two nodes and configured a point-to-point network between them.
    •  Using point-to-point links set up the network with nodes.
  2. Applications:
    • Created UDP sockets on both sender and receiver nodes to simulate traffic.
  3. Traffic Generation:
    • Implemented the GenerateTraffic function to send packets from the source node to the sink node at regular intervals.
    • Each packet is tagged with the current simulation time to track when it was sent.
  4. Delay Measurement:
    • Implemented the PacketReceiveCallback function to calculate the delay of each received packet by comparing the current simulation time with the timestamp in the packet tag.
    • Stored the delay of each packet in a delaySamples vector.
  5. Delay Variance Calculation:
    • Implemented the CalculateVariance function to calculate the variance of the delays stored in the delaySamples vector.
  6. Running the Simulation:
    • The simulation runs, sending and receiving packets.
    • After the simulation, the delay variance is calculated and printed.

Finally, we get to know that delay variance is calculated by measuring the packet delay times and simulate data transmission to receive and generate traffic through ns3.

Get performance analysis done by our developers by comparing parameters on  Calculate delay variance (jitter) in ns3.