Skip to content

Commit

Permalink
Include stats calculator to ease performance evaluation
Browse files Browse the repository at this point in the history
* Add stats calculator + example
* Update CMakeLists.txt
  • Loading branch information
MatteoDrago authored Jan 25, 2023
1 parent 8bd43fe commit 68ee148
Show file tree
Hide file tree
Showing 5 changed files with 796 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ build_lib(
model/vr-burst-generator.cc
helper/bursty-helper.cc
helper/burst-sink-helper.cc
helper/bursty-app-stats-calculator.cc
HEADER_FILES model/burst-generator.h
model/burst-sink.h
model/bursty-application.h
Expand All @@ -20,6 +21,7 @@ build_lib(
model/vr-burst-generator.h
helper/bursty-helper.h
helper/burst-sink-helper.h
helper/bursty-app-stats-calculator.h
LIBRARIES_TO_LINK ${libcore}
${libapplications}
${libpoint-to-point}
Expand Down
9 changes: 9 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,13 @@ build_lib_example(
${libapplications}
${libcore}
${libwifi}
)

build_lib_example(
NAME stats-calculator-example
SOURCE_FILES stats-calculator-example.cc
LIBRARIES_TO_LINK ${libvr-app}
${libapplications}
${libcore}
${libwifi}
)
161 changes: 161 additions & 0 deletions examples/stats-calculator-example.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2021 SIGNET Lab, Department of Information Engineering,
* University of Padova
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/

#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 "ns3/seq-ts-size-frag-header.h"
#include "ns3/bursty-helper.h"
#include "ns3/burst-sink-helper.h"
#include "ns3/bursty-app-stats-calculator.h"

/**
* An example on how to use the BurstyAppStatsCalculator.
* Example based on bursty-application-example.
*/

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("BurstyApplicationExample");

std::string
AddressToString (const Address &addr)
{
std::stringstream addressStr;
addressStr << InetSocketAddress::ConvertFrom (addr).GetIpv4 () << ":"
<< InetSocketAddress::ConvertFrom (addr).GetPort ();
return addressStr.str ();
}

void
BurstTx(uint32_t nodeId,
Ptr<BurstyAppStatsCalculator> statsCalculator,
Ptr<const Packet> burst,
const Address& from,
const Address& to,
const SeqTsSizeFragHeader& header)
{
NS_LOG_INFO ("Sent burst seq=" << header.GetSeq () << " of header.GetSize ()="
<< header.GetSize () << " (burst->GetSize ()=" << burst->GetSize ()
<< ") bytes from " << AddressToString (from) << " to "
<< AddressToString (to) << " at " << header.GetTs ().As (Time::S));
statsCalculator->TxBurst(nodeId, burst, from, to, header);
}

void
BurstRx(uint32_t nodeId,
Ptr<BurstyAppStatsCalculator> statsCalculator,
Ptr<const Packet> burst,
const Address& from,
const Address& to,
const SeqTsSizeFragHeader& header)
{
NS_LOG_INFO ("Received burst seq="
<< header.GetSeq () << " of header.GetSize ()=" << header.GetSize ()
<< " (burst->GetSize ()=" << burst->GetSize () << ") bytes from "
<< AddressToString (from) << " to " << AddressToString (to) << " at "
<< header.GetTs ().As (Time::S));

statsCalculator->RxBurst(nodeId, burst, from, to, header);
}

int
main (int argc, char *argv[])
{
double simTimeSec = 10;
CommandLine cmd (__FILE__);
cmd.AddValue ("SimulationTime", "Length of simulation in seconds.", simTimeSec);
cmd.Parse (argc, argv);

Time::SetResolution (Time::NS);
LogComponentEnableAll (LOG_PREFIX_TIME);
LogComponentEnable ("BurstyApplicationExample", LOG_INFO);

Config::SetDefault("ns3::BurstyAppStatsCalculator::EpochDuration", TimeValue(Seconds(0.1)));
Config::SetDefault("ns3::BurstyAppStatsCalculator::WriteToFile", BooleanValue(true));

// Setup two nodes
NodeContainer nodes;
nodes.Create (2);

PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));

NetDeviceContainer devices;
devices = pointToPoint.Install (nodes);

InternetStackHelper stack;
stack.Install (nodes);

uint16_t portNumber = 50000;

Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer interfaces = address.Assign (devices);

Ipv4Address serverAddress = interfaces.GetAddress (0);
Ipv4Address sinkAddress = Ipv4Address::GetAny (); // 0.0.0.0

// Create bursty application helper
BurstyHelper burstyHelper ("ns3::UdpSocketFactory", InetSocketAddress (serverAddress, portNumber));
burstyHelper.SetAttribute ("FragmentSize", UintegerValue (1200));
burstyHelper.SetBurstGenerator ("ns3::SimpleBurstGenerator",
"PeriodRv", StringValue ("ns3::ConstantRandomVariable[Constant=100e-3]"),
"BurstSizeRv", StringValue ("ns3::ConstantRandomVariable[Constant=10e3]"));

// Install bursty application
ApplicationContainer serverApps = burstyHelper.Install (nodes.Get (1));
Ptr<BurstyApplication> burstyApp = serverApps.Get (0)->GetObject<BurstyApplication> ();
Ptr<BurstyAppStatsCalculator> statsCalculator = CreateObject<BurstyAppStatsCalculator>();

// Example of connecting to the trace sources
burstyApp->TraceConnectWithoutContext ("BurstTx", MakeBoundCallback (&BurstTx, nodes.Get (1)->GetId (), statsCalculator));

// Create burst sink helper
BurstSinkHelper burstSinkHelper ("ns3::UdpSocketFactory",
InetSocketAddress (sinkAddress, portNumber));

// Install burst sink
ApplicationContainer clientApps = burstSinkHelper.Install (nodes.Get (0));
Ptr<BurstSink> burstSink = clientApps.Get (0)->GetObject<BurstSink> ();

// Example of connecting to the trace sources
burstSink->TraceConnectWithoutContext("BurstRx", MakeBoundCallback(&BurstRx, nodes.Get (0)->GetId (), statsCalculator));

// Stop bursty app after simTimeSec
serverApps.Stop (Seconds (simTimeSec));
Simulator::Stop (Seconds(simTimeSec));
Simulator::Run ();
Simulator::Destroy ();

// Stats
std::cout << "Total RX bursts: " << burstyApp->GetTotalTxBursts () << "/"
<< burstSink->GetTotalRxBursts () << std::endl;
std::cout << "Total RX fragments: " << burstyApp->GetTotalTxFragments () << "/"
<< burstSink->GetTotalRxFragments () << std::endl;
std::cout << "Total RX bytes: " << burstyApp->GetTotalTxBytes () << "/"
<< burstSink->GetTotalRxBytes () << std::endl;

return 0;
}
Loading

0 comments on commit 68ee148

Please sign in to comment.