NS3中跟踪发送速率或接收速率

在NS3中有很多方式跟踪发送速率或接收速率。

修改数据包处理函数

如果方便修改数据包处理函数的话,可以在函数内部做一丢丢修改。一般是HandleRead()函数。这个就不过多介绍了,统计一定时间内的接收数据量就好。

使用FlowMonitor模块

FlowMonitor模块可以检测每一条流的丢包、接收数据包数量、接收数据包大小。但注意是每一条流的。
比如说我想检测最后一条流的接收速率。

#include "ns3/flow-monitor-module.h"
Time prevTime = Seconds (0);
uint32_t prev = 0;
static void
TraceThroughput (Ptr monitor)
{
  	FlowMonitor::FlowStatsContainer stats = monitor->GetFlowStats ();
 	std::map::const_iterator itr = stats.begin();  //检测第一条流
 	//下边是最后一条流最后一条流
 	// std::map::const_iterator itr = stats.end(); itr--;

   	Time curTime = Now ();
    std::cout <<  curTime << " " << 8 * (itr->second.rxBytes - prev) / (1000 * 1000 * (curTime.GetSeconds () - prevTime.GetSeconds ())) << std::endl;
    prevTime = curTime;
   	prev = itr->second.rxBytes;
   	Simulator::Schedule (Seconds (0.1), &TraceThroughput, monitor);
}
//主函数中
int
main() {
  //...
  FlowMonitorHelper flowmon;
  Ptr monitor = flowmon.InstallAll ();
  Simulator::Schedule (Seconds (0 + 0.002), &TraceThroughput, monitor);
  
  Simulator::Stop(stopTime);    //一定要加这句话,否则会出现仿真不会停止的情况
  Simulator::Run ();
}

如果要共计所有流的接收速率。把TraceThroughput的函数改改就行了,改成下边:

static void
TraceThroughput (Ptr monitor)
{
	FlowMonitor::FlowStatsContainer stats = monitor->GetFlowStats ();
	uint64_t rxbyte = 0;
    for (std::map::const_iterator i = stats.begin (); i != stats.end (); ++i)
    {
        rxbyte += i->second.rxBytes;
    }

    Time curTime = Now ();
      std::cout <<  curTime << " " << 8 * (rxbyte - lastrxByte) / (1000 * 1000 * (curTime.GetSeconds () - prevTime.GetSeconds ())) << std::endl;
      prevTime = curTime;
      lastrxByte = rxbyte;
      Simulator::Schedule (Seconds (0.1), &TraceThroughput, monitor);
}

以上是统计的接收速率,统计发送速率的话,把所有的"rx"改成“tx”就好,
举个栗子,统计第一条流的发送速率:

static void
TraceThroughput (Ptr monitor)
{
  	FlowMonitor::FlowStatsContainer stats = monitor->GetFlowStats ();
 	std::map::const_iterator itr = stats.begin();  检测第一条流
 	//下边是最后一条流最后一条流
 	// std::map::const_iterator itr = stats.end(); itr--;

   	Time curTime = Now ();
    std::cout <<  curTime << " " << 8 * (itr->second.txBytes - prev) / (1000 * 1000 * (curTime.GetSeconds () - prevTime.GetSeconds ())) << std::endl;
    prevTime = curTime;
   	prev = itr->second.txBytes;
   	Simulator::Schedule (Seconds (0.1), &TraceThroughput, monitor);
}

你可能感兴趣的:(ns3网络仿真,网络通信,c++,网络)