-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathbench-simulator.cc
277 lines (231 loc) · 7.65 KB
/
bench-simulator.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2006 INRIA
*
* 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
*
* Author: Mathieu Lacage <[email protected]>
*/
#include <iomanip>
#include <iostream>
#include <fstream>
#include <vector>
#include <string.h>
#include "ns3/core-module.h"
using namespace ns3;
bool g_debug = false;
std::string g_me;
#define LOG(x) std::cout << x << std::endl
#define LOGME(x) LOG (g_me << x)
#define DEB(x) if (g_debug) { LOGME (x) ; }
// Output field width
int g_fwidth = 6;
class Bench
{
public:
Bench (const uint32_t population, const uint32_t total)
: m_population (population),
m_total (total),
m_count (0)
{ };
void SetRandomStream (Ptr<RandomVariableStream> stream)
{
m_rand = stream;
}
void SetPopulation (const uint32_t population)
{
m_population = population;
}
void SetTotal (const uint32_t total)
{
m_total = total;
}
void RunBench (void);
private:
void Cb (void);
Ptr<RandomVariableStream> m_rand;
uint32_t m_population;
uint32_t m_total;
uint32_t m_count;
};
void
Bench::RunBench (void)
{
SystemWallClockMs time;
double init, simu;
DEB ("initializing");
m_count = 0;
time.Start ();
for (uint32_t i = 0; i < m_population; ++i)
{
Time at = NanoSeconds (m_rand->GetValue ());
Simulator::Schedule (at, &Bench::Cb, this);
}
init = time.End ();
init /= 1000;
DEB ("initialization took " << init << "s");
DEB ("running");
time.Start ();
Simulator::Run ();
simu = time.End ();
simu /= 1000;
DEB ("run took " << simu << "s");
LOG (std::setw (g_fwidth) << init <<
std::setw (g_fwidth) << (m_population / init) <<
std::setw (g_fwidth) << (init / m_population) <<
std::setw (g_fwidth) << simu <<
std::setw (g_fwidth) << (m_count / simu) <<
std::setw (g_fwidth) << (simu / m_count));
}
void
Bench::Cb (void)
{
if (m_count >= m_total)
{
return;
}
DEB ("event at " << Simulator::Now ().GetSeconds () << "s");
Time after = NanoSeconds (m_rand->GetValue ());
Simulator::Schedule (after, &Bench::Cb, this);
++m_count;
}
Ptr<RandomVariableStream>
GetRandomStream (std::string filename)
{
Ptr<RandomVariableStream> stream = 0;
if (filename == "")
{
LOGME ("using default exponential distribution");
Ptr<ExponentialRandomVariable> erv = CreateObject<ExponentialRandomVariable> ();
erv->SetAttribute ("Mean", DoubleValue (100));
stream = erv;
}
else
{
std::istream *input;
if (filename == "-")
{
LOGME ("using event distribution from stdin");
input = &std::cin;
}
else
{
LOGME ("using event distribution from " << filename);
input = new std::ifstream (filename.c_str ());
}
double value;
std::vector<double> nsValues;
while (!input->eof ())
{
if (*input >> value)
{
uint64_t ns = (uint64_t) (value * 1000000000);
nsValues.push_back (ns);
}
else
{
input->clear ();
std::string line;
*input >> line;
}
}
LOGME ("found " << nsValues.size () << " entries");
Ptr<DeterministicRandomVariable> drv = CreateObject<DeterministicRandomVariable> ();
drv->SetValueArray (&nsValues[0], nsValues.size ());
stream = drv;
}
return stream;
}
int main (int argc, char *argv[])
{
bool schedCal = false;
bool schedHeap = false;
bool schedList = false;
bool schedMap = true;
uint32_t pop = 100000;
uint32_t total = 1000000;
uint32_t runs = 1;
std::string filename = "";
CommandLine cmd;
cmd.Usage ("Benchmark the simulator scheduler.\n"
"\n"
"Event intervals are taken from one of:\n"
" an exponential distribution, with mean 100 ns,\n"
" an ascii file, given by the --file=\"<filename>\" argument,\n"
" or standard input, by the argument --file=\"-\"\n"
"In the case of either --file form, the input is expected\n"
"to be ascii, giving the relative event times in ns.");
cmd.AddValue ("cal", "use CalendarSheduler", schedCal);
cmd.AddValue ("heap", "use HeapScheduler", schedHeap);
cmd.AddValue ("list", "use ListSheduler", schedList);
cmd.AddValue ("map", "use MapScheduler (default)", schedMap);
cmd.AddValue ("debug", "enable debugging output", g_debug);
cmd.AddValue ("pop", "event population size (default 1E5)", pop);
cmd.AddValue ("total", "total number of events to run (default 1E6)", total);
cmd.AddValue ("runs", "number of runs (default 1)", runs);
cmd.AddValue ("file", "file of relative event times", filename);
cmd.AddValue ("prec", "printed output precision", g_fwidth);
cmd.Parse (argc, argv);
g_me = cmd.GetName () + ": ";
g_fwidth += 6; // 5 extra chars in '2.000002e+07 ': . e+0 _
ObjectFactory factory ("ns3::MapScheduler");
if (schedCal) { factory.SetTypeId ("ns3::CalendarScheduler"); }
if (schedHeap) { factory.SetTypeId ("ns3::HeapScheduler"); }
if (schedList) { factory.SetTypeId ("ns3::ListScheduler"); }
Simulator::SetScheduler (factory);
LOGME (std::setprecision (g_fwidth - 6));
DEB ("debugging is ON");
LOGME ("scheduler: " << factory.GetTypeId ().GetName ());
LOGME ("population: " << pop);
LOGME ("total events: " << total);
LOGME ("runs: " << runs);
Bench *bench = new Bench (pop, total);
bench->SetRandomStream (GetRandomStream (filename));
// table header
LOG ("");
LOG (std::left << std::setw (g_fwidth) << "Run #" <<
std::left << std::setw (3 * g_fwidth) << "Inititialization:" <<
std::left << std::setw (3 * g_fwidth) << "Simulation:");
LOG (std::left << std::setw (g_fwidth) << "" <<
std::left << std::setw (g_fwidth) << "Time (s)" <<
std::left << std::setw (g_fwidth) << "Rate (ev/s)" <<
std::left << std::setw (g_fwidth) << "Per (s/ev)" <<
std::left << std::setw (g_fwidth) << "Time (s)" <<
std::left << std::setw (g_fwidth) << "Rate (ev/s)" <<
std::left << std::setw (g_fwidth) << "Per (s/ev)" );
LOG (std::setfill ('-') <<
std::right << std::setw (g_fwidth) << " " <<
std::right << std::setw (g_fwidth) << " " <<
std::right << std::setw (g_fwidth) << " " <<
std::right << std::setw (g_fwidth) << " " <<
std::right << std::setw (g_fwidth) << " " <<
std::right << std::setw (g_fwidth) << " " <<
std::right << std::setw (g_fwidth) << " " <<
std::setfill (' ')
);
// prime
DEB ("priming");
std::cout << std::left << std::setw (g_fwidth) << "(prime)";
bench->RunBench ();
bench->SetPopulation (pop);
bench->SetTotal (total);
for (uint32_t i = 0; i < runs; i++)
{
std::cout << std::setw (g_fwidth) << i;
bench->RunBench ();
}
LOG ("");
return 0;
Simulator::Destroy ();
}