utils/bench-packets.cc
changeset 12 917ba023c576
child 14 6dd7d31c6fc3
equal deleted inserted replaced
11:5bb7bce13924 12:917ba023c576
       
     1 /* -*-	Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
       
     2 /*
       
     3  * Copyright (c) 2006 INRIA
       
     4  * All rights reserved.
       
     5  *
       
     6  * This program is free software; you can redistribute it and/or modify
       
     7  * it under the terms of the GNU General Public License version 2 as
       
     8  * published by the Free Software Foundation;
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License
       
    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
       
    18  *
       
    19  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
       
    20  */
       
    21 #include "yans/wall-clock-ms.h"
       
    22 #include "yans/packet.h"
       
    23 #include "yans/chunk-constant-data.h"
       
    24 #include "yans/chunk-udp.h"
       
    25 #include "yans/chunk-ipv4.h"
       
    26 #include <iostream>
       
    27 
       
    28 using namespace yans;
       
    29 
       
    30 static void 
       
    31 bench_ptr_a (uint32_t n)
       
    32 {
       
    33 	ChunkConstantData data = ChunkConstantData (2000, 1);
       
    34 	ChunkUdp udp;
       
    35 	ChunkIpv4 ipv4;
       
    36 
       
    37 	for (uint32_t i = 0; i < n; i++) {
       
    38 		Packet p;
       
    39 		p.add (&data);
       
    40 		p.add (&udp);
       
    41 		p.add (&ipv4);
       
    42 		Packet o = p;
       
    43 		o.peek (&ipv4);
       
    44 		o.remove (&ipv4);
       
    45 		o.peek (&udp);
       
    46 		o.remove (&udp);
       
    47 		o.peek (&data);
       
    48 		o.remove (&data);
       
    49 	}
       
    50 }
       
    51 
       
    52 static void 
       
    53 bench_ptr_b (uint32_t n)
       
    54 {
       
    55 	ChunkConstantData data = ChunkConstantData (2000, 1);
       
    56 	ChunkUdp udp;
       
    57 	ChunkIpv4 ipv4;
       
    58 
       
    59 	for (uint32_t i = 0; i < n; i++) {
       
    60 		Packet p;
       
    61 		p.add (&data);
       
    62 		p.add (&udp);
       
    63 		p.add (&ipv4);
       
    64 	}
       
    65 }
       
    66 
       
    67 static void
       
    68 ptr_c2 (Packet p)
       
    69 {
       
    70 	ChunkConstantData data = ChunkConstantData (2000, 1);
       
    71 	ChunkUdp udp;
       
    72 
       
    73 	p.peek (&udp);
       
    74 	p.remove (&udp);
       
    75 	p.peek (&data);
       
    76 	p.remove (&data);
       
    77 }
       
    78 
       
    79 static void 
       
    80 ptr_c1 (Packet p)
       
    81 {
       
    82 	ChunkIpv4 ipv4;
       
    83 	p.peek (&ipv4);
       
    84 	p.remove (&ipv4);
       
    85 	ptr_c2 (p);
       
    86 }
       
    87 
       
    88 static void
       
    89 bench_ptr_c (uint32_t n)
       
    90 {
       
    91 	ChunkConstantData data = ChunkConstantData (2000, 1);
       
    92 	ChunkUdp udp;
       
    93 	ChunkIpv4 ipv4;
       
    94 
       
    95 	for (uint32_t i = 0; i < n; i++) {
       
    96 		Packet p;
       
    97 		p.add (&data);
       
    98 		p.add (&udp);
       
    99 		p.add (&ipv4);
       
   100 		ptr_c1 (p);
       
   101 	}
       
   102 }
       
   103 
       
   104 
       
   105 static void
       
   106 run_bench (void (*bench) (uint32_t), uint32_t n, char const *name)
       
   107 {
       
   108 	WallClockMs time;
       
   109 	time.start ();
       
   110 	(*bench) (n);
       
   111 	unsigned long long delta_ms = time.end ();
       
   112 	double ps = n;
       
   113 	ps *= 1000;
       
   114 	ps /= delta_ms;
       
   115 	std::cout << name<<"=" << ps << " packets/s" << std::endl;
       
   116 }
       
   117 
       
   118 int main (int argc, char *argv[])
       
   119 {
       
   120 	uint32_t n = 0;
       
   121 	while (argc > 0) {
       
   122 		if (strncmp ("--n=", argv[0],strlen ("--n=")) == 0) {
       
   123 			char const *n_ascii = argv[0] + strlen ("--n=");
       
   124 			n = atoi (n_ascii);
       
   125 		}
       
   126 		argc--;
       
   127 		argv++;
       
   128 	}
       
   129 
       
   130 	run_bench (&bench_ptr_a, n, "a");
       
   131 	run_bench (&bench_ptr_b, n, "b");
       
   132 	run_bench (&bench_ptr_c, n, "c");
       
   133 
       
   134 	return 0;
       
   135 }