Download all the source code here:
https://drive.google.com/drive/folders/14B7QtvAgaiDvBtf-cn8R_mseUYorWkRN?usp=sharing
NS-3 FlowMonitor Tutorial: Measure Throughput, Delay, Packet Loss and Jitter
What is FlowMonitor in NS-3?
FlowMonitor is one of the most useful tools in NS-3 for evaluating the performance of network simulations. It can collect statistics for IP flows and export the results to an XML file for further analysis.
In this tutorial, we learn how to use NS-3 FlowMonitor to collect and analyse transmitted packets, received packets, lost packets, transmitted bytes, received bytes, packet delay, jitter and flow statistics. We will also see how FlowMonitor can be combined with NetAnim, Wireshark and Python-based analysis.
What can FlowMonitor measure?
Parameter | Description |
txPackets | Number of packets transmitted |
rxPackets | Number of packets received |
lostPackets | Number of packets lost |
txBytes | Number of bytes transmitted |
rxBytes | Number of bytes received |
delaySum | Accumulated packet delay |
jitterSum | Accumulated jitter |
timeFirstTxPacket | Time of first packet transmission |
timeLastTxPacket | Time of last packet transmission |
timeFirstRxPacket | Time of first packet reception |
timeLastRxPacket | Time of last packet reception |
timesForwarded | Number of forwarding events |
FlowMonitor architecture
NS-3 Network
|
+-----------+-----------+
| | |
Node 1 Router Node 2
| | |
+-----------+-----------+
|
FlowMonitor
|
Flow Statistics
|
XML Output File
|
+-----------+-----------+
| | |
Python MATLAB Other tools
Step 1: Include the FlowMonitor headers
#include "ns3/flow-monitor.h"
#include "ns3/flow-monitor-helper.h"
Step 2: Create the FlowMonitor
FlowMonitorHelper flowHelper;
Ptr<FlowMonitor> flowMonitor = flowHelper.InstallAll();
Create one FlowMonitorHelper and install the monitor on the nodes. For current ns-3 releases, keep the helper as a single instance in main().
Step 3: Run the simulation
FlowMonitorHelper flowHelper;
Ptr<FlowMonitor> flowMonitor = flowHelper.InstallAll();
Simulator::Run();
Do not immediately stop the simulation when applications stop. Packets may still be travelling through the network. Allow a cleanup period before Simulator::Stop().
application.Stop(Seconds(10.0));
Simulator::Stop(Seconds(11.0));
Simulator::Run();
Step 4: Save FlowMonitor results
flowMonitor->SerializeToXmlFile(
"flowmon-results.xml",
true,
true
);
The generated XML file contains the flow statistics for later analysis.
Complete FlowMonitor example
#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/flow-monitor.h"
#include "ns3/flow-monitor-helper.h"
using namespace ns3;
int main(int argc, char *argv[])
{
NodeContainer nodes;
nodes.Create(2);
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute(
"DataRate",
StringValue("10Mbps")
);
pointToPoint.SetChannelAttribute(
"Delay",
StringValue("2ms")
);
NetDeviceContainer devices =
pointToPoint.Install(nodes);
InternetStackHelper internet;
internet.Install(nodes);
Ipv4AddressHelper address;
address.SetBase(
"10.1.1.0",
"255.255.255.0"
);
Ipv4InterfaceContainer interfaces =
address.Assign(devices);
uint16_t port = 5000;
PacketSinkHelper sinkHelper(
"ns3::UdpSocketFactory",
InetSocketAddress(
Ipv4Address::GetAny(),
port
)
);
ApplicationContainer sink =
sinkHelper.Install(nodes.Get(1));
sink.Start(Seconds(0.0));
sink.Stop(Seconds(10.0));
OnOffHelper source(
"ns3::UdpSocketFactory",
InetSocketAddress(
interfaces.GetAddress(1),
port
)
);
source.SetAttribute(
"DataRate",
DataRateValue(DataRate("2Mbps"))
);
source.SetAttribute(
"PacketSize",
UintegerValue(1024)
);
ApplicationContainer sender =
source.Install(nodes.Get(0));
sender.Start(Seconds(1.0));
sender.Stop(Seconds(10.0));
FlowMonitorHelper flowHelper;
Ptr<FlowMonitor> flowMonitor =
flowHelper.InstallAll();
Simulator::Stop(Seconds(11.0));
Simulator::Run();
flowMonitor->SerializeToXmlFile(
"flowmon-results.xml",
true,
true
);
Simulator::Destroy();
return 0;
}
Running the simulation
For modern ns-3 installations, use the ./ns3 command rather than the old ./waf workflow used by the 2019 article.
./ns3 run flowmonitor-example
Understanding the XML output
<Flow flowId="1"
txBytes="1000000"
rxBytes="990000"
txPackets="1000"
rxPackets="990"
lostPackets="10">
</Flow>
The exact values depend on the simulation configuration.
Calculating packet loss
Packet Loss Ratio =
(TxPackets - RxPackets) / TxPackets × 100
Example: if TxPackets = 1000 and RxPackets = 950, the packet loss ratio is 5%.
Calculating throughput
Throughput = RxBytes × 8 / (TimeLastRxPacket - TimeFirstRxPacket)
Convert bits per second to Mbps by dividing by 1,000,000. State clearly how the measurement interval was defined when reporting research results.
Measuring delay
Average Delay = DelaySum / RxPackets
Use the FlowMonitor measurement scope when interpreting this value.
Measuring jitter
jitterSum
FlowMonitor records accumulated jitter. Define the exact calculation used when reporting jitter in a paper or experiment.
Flow ID and flow classification
FlowMonitor assigns a flow ID to each detected flow. This allows the performance statistics to be associated with a particular communication flow.
FlowMonitor and Wireshark
FlowMonitor | Wireshark |
Throughput | Individual packets |
Delay | Protocol headers |
Jitter | TCP flags |
Packet counts | IP addresses |
Flow-level statistics | Packet timing |
FlowMonitor and NetAnim
NetAnim provides visualisation of the simulated network, while FlowMonitor provides quantitative performance statistics. They complement each other: NetAnim helps answer what is happening visually, while FlowMonitor helps quantify how well the network is performing.
FlowMonitor and Python
NS-3
|
FlowMonitor
|
flowmon-results.xml
|
Python
|
CSV / graphs
|
Performance analysis
This workflow is particularly useful when running many simulations with different parameters, such as TCP Reno, NewReno, Cubic and Vegas.
Important FlowMonitor limitations
FlowMonitor operates at the IP level, so TCP retransmissions and packet fragmentation need careful interpretation.
Packets that remain in the network when the simulation terminates can affect loss statistics.
Allow a cleanup period after applications stop before terminating the simulation.
Always document exactly how metrics were calculated before using them in research publications.
Common FlowMonitor mistakes
Using old ns-3.27 paths: Do not copy version-specific paths such as ns-allinone-3.27 into a current installation.
Using the old waf command: Modern ns-3 tutorials should use the current ./ns3 workflow.
Creating multiple FlowMonitorHelpers: Use a single FlowMonitorHelper instance in main().
Stopping the simulation too early: Allow time for packets already in the network to arrive.
Assuming lostPackets always means application-level loss: FlowMonitor measures at the IP level; interpret the statistic accordingly.
Video Tutorial
The original 2019 article contains an Engineering Clinic YouTube tutorial. Keep the existing video for continuity. If a newer FlowMonitor video is available, place the newer video first and retain the older video under an 'Older FlowMonitor Tutorial' section.
Source Code
The original article points readers to Google Drive. For the updated version, move the maintained source code to GitHub and link the repository from this section. A useful repository structure is:
ns3-flowmonitor/
├── flowmonitor-example.cc
├── README.md
├── flowmon-results.xml
└── analysis/
└── flowmonitor-analysis.py
Frequently Asked Questions
What is FlowMonitor in NS-3?
FlowMonitor is an NS-3 framework for collecting performance statistics for network flows.
What can FlowMonitor measure?
It can provide transmitted/received packets and bytes, lost packets, delay and jitter statistics.
Does FlowMonitor work with TCP?
Yes, but IP-level monitoring means TCP retransmissions must be interpreted carefully.
Can FlowMonitor calculate throughput?
Yes. Received bytes and timing statistics can be used to calculate throughput.
Can FlowMonitor calculate packet loss?
Yes. Transmitted and received packet counts can be used to calculate packet loss.
Can FlowMonitor be used with Wireshark?
Yes. FlowMonitor and Wireshark provide complementary information.
Can FlowMonitor be used with NetAnim?
Yes. NetAnim provides visualisation while FlowMonitor provides quantitative statistics.
Where are FlowMonitor results stored?
They can be exported to an XML file using SerializeToXmlFile().
Conclusion
NS-3 FlowMonitor is an essential tool for analysing network simulation performance. It allows researchers and students to quantify throughput, packet loss, delay, jitter and other flow statistics. For advanced experiments, FlowMonitor can be combined with NetAnim, Wireshark, Python and Gnuplot to build a complete simulation-analysis workflow.
More NS-3 Tutorials from Engineering Clinic
NS-3 Installation
NS-3 TCP Tutorials
NS-3 UDP Tutorials
NS-3 Wi-Fi Tutorials
NS-3 NetAnim Tutorial
NS-3 Wireshark Tutorial
NS-3 FlowMonitor Tutorial
NS-3 Queue Management
NS3-Gym and Reinforcement Learning
NS-3 IoT and IoV Tutorials
About Engineering Clinic
Engineering Clinic provides practical tutorials on NS-3, computer networks, Linux, IoT, AI, deep learning, cybersecurity and open-source technologies. Subscribe to the Engineering Clinic YouTube channel for practical demonstrations and hands-on tutorials.
Comments
Post a Comment