Skip to main content

NS-3 FlowMonitor Tutorial: Measure Throughput, Delay, Packet Loss and Jitter


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-monit
or.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",
         Strin
gValue("10Mbps")
     );
 
     pointToPoint.SetChannelAttribute(
         "Delay",
         StringValue("2ms")
     );
 
     NetDeviceContainer devices =
         pointToPoint.Install(nodes);
 
     InternetStackHelper internet;
     internet.Install(nodes);
 
     Ip
v4AddressHelper address;
 
     address.SetBase(
         "10.1.1.0",
         "255.255.255.0"
     );
 
     Ipv4InterfaceContainer interfaces =
         address.Assign(devices);
 
     uint16_t port = 5000;
 
     PacketSinkHelper sinkHelper(
         "ns3::UdpSocket
Factory",
         InetSocketAddress(
             Ipv4Address::GetAny(),
             port
         )
     );
 
     ApplicationContainer sink =
         sinkHelper.Install(nodes.Get(1));
 
     sink.Start(Seconds(0.0));
     sink.Stop(Seconds(10.0));
 
     OnOffHelp
er source(
         "ns3::UdpSocketFactory",
         InetSocketAddress(
             interfaces.GetAddress(1),
             port
         )
     );
 
     source.SetAttribute(
         "DataRate",
         DataRateValue(DataRate("2Mbps"))
     );
 
     source.SetAttr
ibute(
         "PacketSize",
         UintegerValue(1024)
     );
 
     ApplicationContainer sender =
         source.Install(nodes.Get(0));
 
     sender.Start(Seconds(1.0));
     sender.Stop(Seconds(10.0));
 
     FlowMonitorHelper flowHelper;
 
     Ptr<FlowMonito
r> 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

Popular posts from this blog

How to Create Ubuntu 24.04 Bootable USB Using Rufus [Step-by-Step Guide]

How to Create Ubuntu 24.04 Bootable USB Using Rufus [Step-by-Step Guide] Are you planning to install or try Ubuntu 24.04 LTS ? The easiest and most reliable method is to create a bootable USB drive using Rufus on a Windows system. This detailed guide will help you create a Ubuntu 24.04 USB bootloader using Rufus with easy-to-follow steps and screenshots (optional). Here is the complete video of the bootloader creation and OS installation in Windows 11. 🧰 Requirements A USB flash drive (minimum 8GB recommended) A Windows PC Ubuntu 24.04 LTS ISO file Rufus USB creation tool 🧾 Steps to Create a Ubuntu 24.04 Bootable USB Using Rufus ✅ Step 1: Download Ubuntu 24.04 ISO Visit the official Ubuntu website and download the Ubuntu 24.04 LTS ISO file . ✅ Step 2: Download and Run Rufus Head to Rufus official site and download the latest version. Open the executable file (no installation required). ✅ Step 3: Insert USB Drive Plug in your USB drive. Rufus ...

Installing ns3 in Ubuntu 22.04 | Complete Instructions

In this post, we are going to see how to install ns-3.36.1 in Ubuntu 22.04. You can follow the video for complete details Tools used in this simulation: NS3 version ns-3.36.1  OS Used: Ubuntu 22.04 LTS Installation of NS3 (ns-3.36.1) There are some changes in the ns3 installation procedure and the dependencies. So open a terminal and issue the following commands Step 1:  Prerequisites $ sudo apt update In the following packages, all the required dependencies are taken care and you can install all these packages for the complete use of ns3. $ sudo apt install g++ python3 python3-dev pkg-config sqlite3 cmake python3-setuptools git qtbase5-dev qtchooser qt5-qmake qtbase5-dev-tools gir1.2-goocanvas-2.0 python3-gi python3-gi-cairo python3-pygraphviz gir1.2-gtk-3.0 ipython3 openmpi-bin openmpi-common openmpi-doc libopenmpi-dev autoconf cvs bzr unrar gsl-bin libgsl-dev libgslcblas0 wireshark tcpdump sqlite sqlite3 libsqlite3-dev  libxml2 libxml2-dev libc6-dev libc6-dev-i386 libc...

Installation of NS2 in Ubuntu 22.04 | NS2 Tutorial 2

NS-2.35 installation in Ubuntu 22.04 This post shows how to install ns-2.35 in Ubuntu 22.04 Operating System Since ns-2.35 is too old, it needs the following packages gcc-4.8 g++-4.8 gawk and some more libraries Follow the video for more instructions So, here are the steps to install this software: To download and extract the ns2 software Download the software from the following link http://sourceforge.net/projects/nsnam/files/allinone/ns-allinone-2.35/ns-allinone-2.35.tar.gz/download Extract it to home folder and in my case its /home/pradeepkumar (I recommend to install it under your home folder) $ tar zxvf ns-allinone-2.35.tar.gz or Right click over the file and click extract here and select the home folder. $ sudo apt update $ sudo apt install build-essential autoconf automake libxmu-dev gawk To install gcc-4.8 and g++-4.8 $ sudo gedit /etc/apt/sources.list make an entry in the above file deb http://in.archive.ubuntu.com/ubuntu/ bionic main universe $ sudo apt update Since, it...