当前位置: 首页 > 工具软件 > NS3 > 使用案例 >

ns3网络仿真的数据分析与统计

郎献
2023-12-01

ns3网络仿真的数据分析与统计

平均时延与包投递率

写在前面:

  • 本文所用到的工具安装

    • 数据处理工具gawk:sudo apt-get install gawk
    • 绘图工具gnuplot:sudo apt-get install Gnuplot
  • 根据仿真数据的来源不同,数据分析脚本(.awk文件)的编写会有区别

  • 数据来源一般有三种:日志组件终端打印信息;ASCII文件(.tr);pcap文件

  • 终端所在目录:ns-allinone-3.32/ns-3.32/

  • 利用应用程序的客户端与服务器端日志组件终端打印信息

    • 生成仿真数据并处理(以ns3自带例程third.cc为例)

      • 仿真源文件(文件third.cc

        /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
        /*
         * 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/point-to-point-module.h"
        #include "ns3/network-module.h"
        #include "ns3/applications-module.h"
        #include "ns3/mobility-module.h"
        #include "ns3/csma-module.h"
        #include "ns3/internet-module.h"
        #include "ns3/yans-wifi-helper.h"
        #include "ns3/ssid.h"
        
        // Default Network Topology
        //
        //   Wifi 10.1.3.0
        //                 AP
        //  *    *    *    *
        //  |    |    |    |    10.1.1.0
        // n5   n6   n7   n0 -------------- n1   n2   n3   n4
        //                   point-to-point  |    |    |    |
        //                                   ================
        //                                     LAN 10.1.2.0
        
        using namespace ns3;
        
        NS_LOG_COMPONENT_DEFINE ("ThirdScriptExample");
        
        int 
        main (int argc, char *argv[])
        {
          bool verbose = true;
          uint32_t nCsma = 3;
          uint32_t nWifi = 3;
          bool tracing = false;
        
          CommandLine cmd (__FILE__);
          cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);
          cmd.AddValue ("nWifi", "Number of wifi STA devices", nWifi);
          cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose);
          cmd.AddValue ("tracing", "Enable pcap tracing", tracing);
        
          cmd.Parse (argc,argv);
        
          // The underlying restriction of 18 is due to the grid position
          // allocator's configuration; the grid layout will exceed the
          // bounding box if more than 18 nodes are provided.
          if (nWifi > 18)
            {
              std::cout << "nWifi should be 18 or less; otherwise grid layout exceeds the bounding box" << std::endl;
              return 1;
            }
        
          if (verbose)
            {
              LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
              LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
            }
        
          NodeContainer p2pNodes;
          p2pNodes.Create (2);
        
          PointToPointHelper pointToPoint;
          pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
          pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
        
          NetDeviceContainer p2pDevices;
          p2pDevices = pointToPoint.Install (p2pNodes);
        
          NodeContainer csmaNodes;
          csmaNodes.Add (p2pNodes.Get (1));
          csmaNodes.Create (nCsma);
        
          CsmaHelper csma;
          csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));
          csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));
        
          NetDeviceContainer csmaDevices;
          csmaDevices = csma.Install (csmaNodes);
        
          NodeContainer wifiStaNodes;
          wifiStaNodes.Create (nWifi);
          NodeContainer wifiApNode = p2pNodes.Get (0);
        
          YansWifiChannelHelper channel = YansWifiChannelHelper::Default ();
          YansWifiPhyHelper phy = YansWifiPhyHelper::Default ();
          phy.SetChannel (channel.Create ());
        
          WifiHelper wifi;
          wifi.SetRemoteStationManager ("ns3::AarfWifiManager");
        
          WifiMacHelper mac;
          Ssid ssid = Ssid ("ns-3-ssid");
          mac.SetType ("ns3::StaWifiMac",
                       "Ssid", SsidValue (ssid),
                       "ActiveProbing", BooleanValue (false));
        
          NetDeviceContainer staDevices;
          staDevices = wifi.Install (phy, mac, wifiStaNodes);
        
          mac.SetType ("ns3::ApWifiMac",
                       "Ssid", SsidValue (ssid));
        
          NetDeviceContainer apDevices;
          apDevices = wifi.Install (phy, mac, wifiApNode);
        
          MobilityHelper mobility;
        
          mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
                                         "MinX", DoubleValue (0.0),
                                         "MinY", DoubleValue (0.0),
                                         "DeltaX", DoubleValue (5.0),
                                         "DeltaY", DoubleValue (10.0),
                                         "GridWidth", UintegerValue (3),
                                         "LayoutType", StringValue ("RowFirst"));
        
          mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
                                     "Bounds", RectangleValue (Rectangle (-50, 50, -50, 50)));
          mobility.Install (wifiStaNodes);
        
          mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
          mobility.Install (wifiApNode);
        
          InternetStackHelper stack;
          stack.Install (csmaNodes);
          stack.Install (wifiApNode);
          stack.Install (wifiStaNodes);
        
          Ipv4AddressHelper address;
        
          address.SetBase ("10.1.1.0", "255.255.255.0");
          Ipv4InterfaceContainer p2pInterfaces;
          p2pInterfaces = address.Assign (p2pDevices);
        
          address.SetBase ("10.1.2.0", "255.255.255.0");
          Ipv4InterfaceContainer csmaInterfaces;
          csmaInterfaces = address.Assign (csmaDevices);
        
          address.SetBase ("10.1.3.0", "255.255.255.0");
          address.Assign (staDevices);
          address.Assign (apDevices);
        
          UdpEchoServerHelper echoServer (9);
        
          ApplicationContainer serverApps = echoServer.Install (csmaNodes.Get (nCsma));
          serverApps.Start (Seconds (1.0));
          serverApps.Stop (Seconds (10.0));
        
          UdpEchoClientHelper echoClient (csmaInterfaces.GetAddress (nCsma), 9);
          echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
          echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
          echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
        
          ApplicationContainer clientApps = 
            echoClient.Install (wifiStaNodes.Get (nWifi - 1));
          clientApps.Start (Seconds (2.0));
          clientApps.Stop (Seconds (10.0));
        
          Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
        
          Simulator::Stop (Seconds (10.0));
        
          if (tracing == true)
            {
              pointToPoint.EnablePcapAll ("third");
              phy.EnablePcap ("third", apDevices.Get (0));
              csma.EnablePcap ("third", csmaDevices.Get (0), true);
            }
        
          Simulator::Run ();
          Simulator::Destroy ();
          return 0;
        }
        
      • 将awk脚本delayloss.awk放入当前目录,并在当前目录创建文件夹third-datamkdir third-data用于存放仿真数据

        • delayloss.awk

          BEGIN {
                
                FS="[ \t]";#field seperator is ' '
                
                rpackets = 0 ;
                clientid = 0 ;
                totaldelay = 0;
                }
                
                {
                
                action = $5;
                
                time = ($3 + 0);
                
                device = $4;
                
                bytes = $6;
                
                #targetid = $9;
                
                portid = $11;
                
                #packetid = substr($9,8,(length($9)-7));
                
                if ((device == "client")&&(action == "sent")) 
                {
                    clientid ++ ;
                     sendtime[clientid] = time;
                    }
                
                if ((device == "server")&&(action == "received"))
                {
                    rpackets ++ ;
                    receivedtime[rpackets] = time;
                    }
                #printf ("%s %s %s %s %s %s\n", action, time, device, bytes, packetid, portid);
                }
                 END {
                    for (packetid=0;packetid<=clientid;packetid++)
                    {
                        start=sendtime[packetid];
                        end=receivedtime[packetid];
                        packetdelay=end-start;
                        if (start < end)
                        {	#printf("%f %f\n",start,packetdelay);
                            totaldelay = packetdelay + totaldelay;
                            }
                        }
                    packetlossrate = rpackets/clientid;
                    delay = totaldelay/rpackets;
                    printf ("%f %f\n", packetlossrate, delay);
                }
          
      • 运行:

        • sudo ./waf --run "third --nWifi=3" >& third-data/third-3.data

        • sudo ./waf --run "third --nWifi=4" >& third-data/third-4.data

        • sudo ./waf --run "third --nWifi=5" >& third-data/third-5.data

        • sudo ./waf --run "third --nWifi=6" >& third-data/third-6.data

        • gawk -f delayloss.awk third-data/third-3.data >> third.out

        • gawk -f delayloss.awk third-data/third-4.data >> third.out

        • gawk -f delayloss.awk third-data/third-5.data >> third.out

        • gawk -f delayloss.awk third-data/third-6.data >> third.out

        • 命令较多时不建议直接复制粘贴于终端,应 放入可执行文件中便于重复使用与修改:

          • gedit shellcommand-third,复制粘贴上面的命令
          • chmod 777 shellcommand-third
          • ./shellcommand-third
        • 说明:

          • 仿真参数未设置的均为默认值;
          • >&:将终端所有输出信息存放在指定文件(每次都会覆盖之前数据)
          • >>:将终端标准输出信息存放在指定文件(不会覆盖之前数据)
          • .data:原始数据
          • .out:处理后的数据
          • 文件名称与后缀名没有命名规定,按自己喜好来
    • 数据分析

      • 数据来源.out文件

        • 上一步所生成的.out文件内容为wifi节点为3、4、5、6时网络的平均时延与包投递率
        • 第一列:平均时延
        • 第二列:包投递率
        • 共4行数据与节点数相对应
      • 数据预处理

        • 在第一列插入节点数作为折线图的x轴数据源
        • third.out
          3 1.000000 0.017990
          4 1.000000 0.015930
          5 1.000000 0.011000
          6 1.000000 0.014150
      • gnuplot绘图

        • 模板moban.conf

          set terminal gif small size 450,390 #指定输出成gif图片,且图片大小为550×25
          set output "包投递率与平均时延.gif" #指定输出gif图片的文件名
          set title " " #图片标题
          set style data lines #
          set xrange [2:7]
          set xtics 2,1,7
          set yrange [0:1.1]
          set ytics 0,0.1,1
          set xlabel "Nodes" #X轴标题
          set ylabel "data" #Y轴标题
          set grid #显示网格
          plot \
          "third.out" using 1:2 w lp pt 1 lc 1 lw 2 title "Packet delivery rate" ,"third.out" using 1:3 w lp pt 2 lc 2 lw 2 title "Average delay"
          #"out.txt":绘图所需数据;using 1:2:第一列为x坐标,第二列为y坐标;
          #w:with;lp:linepoint线类型点线型;pt 1:pointtype风格一;lc:linestyle;lw:linewidth
          
      • 运行命令cat moban.conf |gnuplot

  • 注:代码和命令可能回因为复制格式问题导致报错,手动输入即可

  • gawk网络性能参数分析参考博客:

  • gnuplot绘图参考链接:

  • 下一篇:ns3利用FlowMonitor进行网络性能分析

 类似资料: