utils/bench-packets.cc
changeset 832 3d1243d770d9
parent 150 663120712cd9
child 833 224bfad58818
equal deleted inserted replaced
831:be2e028727b2 832:3d1243d770d9
    16  * along with this program; if not, write to the Free Software
    16  * along with this program; if not, write to the Free Software
    17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18  *
    18  *
    19  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
    19  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
    20  */
    20  */
    21 #include "ns3/wall-clock-ms.h"
    21 #include "ns3/system-wall-clock-ms.h"
    22 #include "ns3/packet.h"
    22 #include "ns3/packet.h"
    23 #include "ns3/chunk-constant-data.h"
       
    24 #include "ns3/chunk-udp.h"
       
    25 #include "ns3/chunk-ipv4.h"
       
    26 #include <iostream>
    23 #include <iostream>
       
    24 #include <sstream>
    27 
    25 
    28 using namespace ns3;
    26 using namespace ns3;
       
    27 
       
    28 template <int N>
       
    29 class BenchHeader : public Header
       
    30 {
       
    31 public:
       
    32   BenchHeader ();
       
    33   bool IsOk (void) const;
       
    34 private:
       
    35   virtual std::string DoGetName (void) const;
       
    36   virtual void PrintTo (std::ostream &os) const;
       
    37   virtual uint32_t GetSerializedSize (void) const;
       
    38   virtual void SerializeTo (Buffer::Iterator start) const;
       
    39   virtual uint32_t DeserializeFrom (Buffer::Iterator start);
       
    40   bool m_ok;
       
    41 };
       
    42 
       
    43 template <int N>
       
    44 BenchHeader<N>::BenchHeader ()
       
    45   : m_ok (false)
       
    46 {}
       
    47 
       
    48 template <int N>
       
    49 bool 
       
    50 BenchHeader<N>::IsOk (void) const
       
    51 {
       
    52   return m_ok;
       
    53 }
       
    54 
       
    55 template <int N>
       
    56 std::string 
       
    57 BenchHeader<N>::DoGetName (void) const
       
    58 {
       
    59   std::ostringstream oss;
       
    60   oss << N;
       
    61   return oss.str ();
       
    62 }
       
    63 
       
    64 template <int N>
       
    65 void 
       
    66 BenchHeader<N>::PrintTo (std::ostream &os) const
       
    67 {
       
    68   NS_ASSERT (false);
       
    69 }
       
    70 template <int N>
       
    71 uint32_t 
       
    72 BenchHeader<N>::GetSerializedSize (void) const
       
    73 {
       
    74   return N;
       
    75 }
       
    76 template <int N>
       
    77 void 
       
    78 BenchHeader<N>::SerializeTo (Buffer::Iterator start) const
       
    79 {
       
    80   start.WriteU8 (N, N);
       
    81 }
       
    82 template <int N>
       
    83 uint32_t
       
    84 BenchHeader<N>::DeserializeFrom (Buffer::Iterator start)
       
    85 {
       
    86   m_ok = true;
       
    87   for (int i = 0; i < N; i++)
       
    88     {
       
    89       if (start.ReadU8 () != N)
       
    90         {
       
    91           m_ok = false;
       
    92         }
       
    93     }
       
    94   return N;
       
    95 }
       
    96 
       
    97 
    29 
    98 
    30 static void 
    99 static void 
    31 benchPtrA (uint32_t n)
   100 benchPtrA (uint32_t n)
    32 {
   101 {
    33   ChunkConstantData data = ChunkConstantData (2000, 1);
   102   BenchHeader<25> ipv4;
    34   ChunkUdp udp;
   103   BenchHeader<8> udp;
    35   ChunkIpv4 ipv4;
       
    36 
   104 
    37   for (uint32_t i = 0; i < n; i++) {
   105   for (uint32_t i = 0; i < n; i++) {
    38       Packet p;
   106       Packet p (2000);
    39       p.add (&data);
   107       p.AddHeader (udp);
    40       p.add (&udp);
   108       p.AddHeader (ipv4);
    41       p.add (&ipv4);
       
    42       Packet o = p;
   109       Packet o = p;
    43       o.peek (&ipv4);
   110       o.RemoveHeader (ipv4);
    44       o.remove (&ipv4);
   111       o.RemoveHeader (udp);
    45       o.peek (&udp);
       
    46       o.remove (&udp);
       
    47       o.peek (&data);
       
    48       o.remove (&data);
       
    49   }
   112   }
    50 }
   113 }
    51 
   114 
    52 static void 
   115 static void 
    53 benchPtrB (uint32_t n)
   116 benchPtrB (uint32_t n)
    54 {
   117 {
    55   ChunkConstantData data = ChunkConstantData (2000, 1);
   118   BenchHeader<25> ipv4;
    56   ChunkUdp udp;
   119   BenchHeader<8> udp;
    57   ChunkIpv4 ipv4;
       
    58 
   120 
    59   for (uint32_t i = 0; i < n; i++) {
   121   for (uint32_t i = 0; i < n; i++) {
    60       Packet p;
   122       Packet p (2000);
    61       p.add (&data);
   123       p.AddHeader (udp);
    62       p.add (&udp);
   124       p.AddHeader (ipv4);
    63       p.add (&ipv4);
       
    64   }
   125   }
    65 }
   126 }
    66 
   127 
    67 static void
   128 static void
    68 ptrC2 (Packet p)
   129 ptrC2 (Packet p)
    69 {
   130 {
    70   ChunkConstantData data = ChunkConstantData (2000, 1);
   131   BenchHeader<8> udp;
    71   ChunkUdp udp;
       
    72 
   132 
    73   p.peek (&udp);
   133   p.RemoveHeader (udp);
    74   p.remove (&udp);
       
    75   p.peek (&data);
       
    76   p.remove (&data);
       
    77 }
   134 }
    78 
   135 
    79 static void 
   136 static void 
    80 ptrC1 (Packet p)
   137 ptrC1 (Packet p)
    81 {
   138 {
    82   ChunkIpv4 ipv4;
   139   BenchHeader<25> ipv4;
    83   p.peek (&ipv4);
   140   p.RemoveHeader (ipv4);
    84   p.remove (&ipv4);
       
    85   ptrC2 (p);
   141   ptrC2 (p);
    86 }
   142 }
    87 
   143 
    88 static void
   144 static void
    89 benchPtrC (uint32_t n)
   145 benchPtrC (uint32_t n)
    90 {
   146 {
    91   ChunkConstantData data = ChunkConstantData (2000, 1);
   147   BenchHeader<25> ipv4;
    92   ChunkUdp udp;
   148   BenchHeader<8> udp;
    93   ChunkIpv4 ipv4;
       
    94 
   149 
    95   for (uint32_t i = 0; i < n; i++) {
   150   for (uint32_t i = 0; i < n; i++) {
    96       Packet p;
   151       Packet p (2000);
    97       p.add (&data);
   152       p.AddHeader (udp);
    98       p.add (&udp);
   153       p.AddHeader (ipv4);
    99       p.add (&ipv4);
       
   100       ptrC1 (p);
   154       ptrC1 (p);
   101   }
   155   }
   102 }
   156 }
   103 
   157 
   104 
   158 
   105 static void
   159 static void
   106 runBench (void (*bench) (uint32_t), uint32_t n, char const *name)
   160 runBench (void (*bench) (uint32_t), uint32_t n, char const *name)
   107 {
   161 {
   108   WallClockMs time;
   162   SystemWallClockMs time;
   109   time.start ();
   163   time.Start ();
   110   (*bench) (n);
   164   (*bench) (n);
   111   unsigned long long deltaMs = time.end ();
   165   unsigned long long deltaMs = time.End ();
   112   double ps = n;
   166   double ps = n;
   113   ps *= 1000;
   167   ps *= 1000;
   114   ps /= deltaMs;
   168   ps /= deltaMs;
   115   std::cout << name<<"=" << ps << " packets/s" << std::endl;
   169   std::cout << name<<"=" << ps << " packets/s" << std::endl;
   116 }
   170 }