UDP Throughput Measurement

Topology

N0(UDP Sender)----P2Plink(5Mbps,2ms)---N1(UDP Sink)

 

throughput-udp.cc(put this file under scratch)

#include <fstream>

#include <string.h>

 

#include "ns3/core-module.h"

#include "ns3/internet-module.h"

#include "ns3/point-to-point-module.h"

#include "ns3/packet-sink.h"

#include "ns3/packet-sink-helper.h"

#include "ns3/on-off-helper.h"

 

 

using namespace ns3;

 

NS_LOG_COMPONENT_DEFINE ("ThroughputUDP");

 

Ptr<PacketSink> sink;                         /* Pointer to the packet sink application */

uint64_t lastTotalRx = 0;                     /* The value of the last total received bytes */

 

void

CalculateThroughput ()

{

  Time now = Simulator::Now ();                                         /* Return the simulator's virtual time. */

  double cur = (sink->GetTotalRx () - lastTotalRx) * (double) 8 / 1e3;     /* Convert Application RX Packets to kBits. */

  std::cout << now.GetSeconds () << "\t" << cur << std::endl;

  lastTotalRx = sink->GetTotalRx ();

  Simulator::Schedule (MilliSeconds (1000), &CalculateThroughput);

}

 

 

int

main (int argc, char *argv[])

{

  NS_LOG_INFO ("Create nodes.");

  NodeContainer n;

  n.Create (2);

 

  InternetStackHelper internet;

  internet.Install (n);

 

  NS_LOG_INFO ("Create channels.");

 

  PointToPointHelper pointToPoint;

  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));

  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));

  pointToPoint.SetDeviceAttribute("Mtu", UintegerValue(1400));  

  NetDeviceContainer p2pDevices;

  p2pDevices = pointToPoint.Install (n);    

 

  Ipv4AddressHelper ipv4;

 

  NS_LOG_INFO ("Assign IP Addresses.");

  ipv4.SetBase ("10.1.1.0", "255.255.255.0");

  Ipv4InterfaceContainer i = ipv4.Assign (p2pDevices);

 

  NS_LOG_INFO ("Create Applications.");

 

  PacketSinkHelper sinkHelper ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), 9));

  ApplicationContainer sinkApp = sinkHelper.Install (n.Get(1));

  sink = StaticCast<PacketSink> (sinkApp.Get (0));

 

  OnOffHelper server ("ns3::UdpSocketFactory", (InetSocketAddress (i.GetAddress (1), 9)));

  server.SetAttribute ("PacketSize", UintegerValue (172)); // G.711 encoding, 20ms delay, 160bytes +12bytes(rtp)

  server.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));

  server.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));

  server.SetAttribute ("DataRate", DataRateValue (DataRate ("64Kbps")));

  ApplicationContainer serverApp = server.Install (n.Get(0));

 

  sinkApp.Start (Seconds (0.0));

  serverApp.Start (Seconds (1.0));

  Simulator::Schedule (Seconds (1.0), &CalculateThroughput);

 

  Simulator::Stop (Seconds (12.0));

  pointToPoint.EnablePcapAll("throughput-udp");

  NS_LOG_INFO ("Run Simulation.");

  Simulator::Run ();

  Simulator::Destroy ();

  NS_LOG_INFO ("Done.");

 

}

 

Execution

 

Back to NS3 Learning Guide

Last Modified:2022/2/4

 

Dr. Chih-Heng Ke

Department of Computer Science and Information Engineering, National Quemoy University, Kinmen, Taiwan

Email: smallko@gmail.com