A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
bench-simulator.cc
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2006 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 
21 #include "ns3/core-module.h"
22 #include <iostream>
23 #include <fstream>
24 #include <vector>
25 #include <string.h>
26 
27 using namespace ns3;
28 
29 
30 bool g_debug = false;
31 
32 class Bench
33 {
34 public:
35  Bench ();
36  void ReadDistribution (std::istream &istream);
37  void SetTotal (uint32_t total);
38  void RunBench (void);
39 private:
40  void Cb (void);
41  std::vector<uint64_t> m_distribution;
42  std::vector<uint64_t>::const_iterator m_current;
43  uint32_t m_n;
44  uint32_t m_total;
45 };
46 
47 Bench::Bench ()
48  : m_n (0),
49  m_total (0)
50 {}
51 
52 void
53 Bench::SetTotal (uint32_t total)
54 {
55  m_total = total;
56 }
57 
58 void
59 Bench::ReadDistribution (std::istream &input)
60 {
61  double data;
62  while (!input.eof ())
63  {
64  if (input >> data)
65  {
66  uint64_t ns = (uint64_t) (data * 1000000000);
67  m_distribution.push_back (ns);
68  }
69  else
70  {
71  input.clear ();
72  std::string line;
73  input >> line;
74  }
75  }
76 }
77 
78 void
79 Bench::RunBench (void)
80 {
81  SystemWallClockMs time;
82  double init, simu;
83  time.Start ();
84  for (std::vector<uint64_t>::const_iterator i = m_distribution.begin ();
85  i != m_distribution.end (); i++)
86  {
87  Simulator::Schedule (NanoSeconds (*i), &Bench::Cb, this);
88  }
89  init = time.End ();
90  init /= 1000;
91 
92  m_current = m_distribution.begin ();
93 
94  time.Start ();
95  Simulator::Run ();
96  simu = time.End ();
97  simu /= 1000;
98 
99  std::cout <<
100  "init n=" << m_distribution.size () << ", time=" << init << "s" << std::endl <<
101  "simu n=" << m_n << ", time=" <<simu << "s" << std::endl <<
102  "init " << ((double)m_distribution.size ()) / init << " insert/s, avg insert=" <<
103  init / ((double)m_distribution.size ())<< "s" << std::endl <<
104  "simu " << ((double)m_n) / simu<< " hold/s, avg hold=" <<
105  simu / ((double)m_n) << "s" << std::endl
106  ;
107 }
108 
109 void
110 Bench::Cb (void)
111 {
112  if (m_n > m_total)
113  {
114  return;
115  }
116  if (m_current == m_distribution.end ())
117  {
118  m_current = m_distribution.begin ();
119  }
120  if (g_debug)
121  {
122  std::cerr << "event at " << Simulator::Now ().GetSeconds () << "s" << std::endl;
123  }
124  Simulator::Schedule (NanoSeconds (*m_current), &Bench::Cb, this);
125  m_current++;
126  m_n++;
127 }
128 
129 void
130 PrintHelp (void)
131 {
132  std::cout << "bench-simulator filename [options]"<<std::endl;
133  std::cout << " filename: a string which identifies the input distribution. \"-\" represents stdin." << std::endl;
134  std::cout << " Options:"<<std::endl;
135  std::cout << " --list: use std::list scheduler"<<std::endl;
136  std::cout << " --map: use std::map cheduler"<<std::endl;
137  std::cout << " --heap: use Binary Heap scheduler"<<std::endl;
138  std::cout << " --debug: enable some debugging"<<std::endl;
139 }
140 
141 int main (int argc, char *argv[])
142 {
143  char const *filename = argv[1];
144  std::istream *input;
145  uint32_t n = 1;
146  uint32_t total = 20000;
147  if (argc == 1)
148  {
149  PrintHelp ();
150  return 0;
151  }
152  argc-=2;
153  argv+= 2;
154  if (strcmp (filename, "-") == 0)
155  {
156  input = &std::cin;
157  }
158  else
159  {
160  input = new std::ifstream (filename);
161  }
162  while (argc > 0)
163  {
164  ObjectFactory factory;
165  if (strcmp ("--list", argv[0]) == 0)
166  {
167  factory.SetTypeId ("ns3::ListScheduler");
168  Simulator::SetScheduler (factory);
169  }
170  else if (strcmp ("--heap", argv[0]) == 0)
171  {
172  factory.SetTypeId ("ns3::HeapScheduler");
173  Simulator::SetScheduler (factory);
174  }
175  else if (strcmp ("--map", argv[0]) == 0)
176  {
177  factory.SetTypeId ("ns3::HeapScheduler");
178  Simulator::SetScheduler (factory);
179  }
180  else if (strcmp ("--calendar", argv[0]) == 0)
181  {
182  factory.SetTypeId ("ns3::CalendarScheduler");
183  Simulator::SetScheduler (factory);
184  }
185  else if (strcmp ("--debug", argv[0]) == 0)
186  {
187  g_debug = true;
188  }
189  else if (strncmp ("--total=", argv[0], strlen("--total=")) == 0)
190  {
191  total = atoi (argv[0]+strlen ("--total="));
192  }
193  else if (strncmp ("--n=", argv[0], strlen("--n=")) == 0)
194  {
195  n = atoi (argv[0]+strlen ("--n="));
196  }
197 
198  argc--;
199  argv++;
200  }
201  Bench *bench = new Bench ();
202  bench->ReadDistribution (*input);
203  bench->SetTotal (total);
204  for (uint32_t i = 0; i < n; i++)
205  {
206  bench->RunBench ();
207  }
208 
209  return 0;
210 }
void SetTypeId(TypeId tid)
measure elapsed time in milliseconds
instantiate subclasses of ns3::Object.
int64_t End(void)
Stop measuring the time since Start() was called.