-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultiSMPL.h
375 lines (308 loc) · 15.8 KB
/
MultiSMPL.h
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
//
// Created by Nikolay on 17.12.2019.
//
#ifndef PROJECT_MULTISMPL_H
#define PROJECT_MULTISMPL_H
#include <ostream>
#include <iostream>
#include <map>
#include <cmath>
#include "smpl.h"
namespace multiSMPL {
enum SystemEvents {
SystemEventMonitor = 1000000000LL,
SystemEventEnd
};
struct Meta {
std::vector<std::string> devices;
std::vector<std::string> queues;
};
class MultiSMPL {
private:
struct QueueInformation {
std::vector<double> avgWaitTime;
std::vector<double> avgLength;
std::vector<double> length;
};
struct DeviceInformation {
std::vector<double> avgReserveTime;
std::vector<double> avgPercentTime;
};
class DecPoint : public std::numpunct<char>
{
public:
DecPoint ( char ch_ ) : ch(ch_) {}
char do_decimal_point() const { return ch ; }
private:
char ch ;
};
std::ostream *fileOutputStream, *csvOutputStream;
std::map<smpl::u64, void (*)(std::pair<smpl::u64, smpl::transact_t>, int, smpl::Engine *)> handlers;
Meta meta;
void updateDevicesInformation(std::vector<DeviceInformation> &devicesInformation,
std::vector<smpl::Device *> &devices, time_t time, int number);
void updateQueuesInformation(std::vector<QueueInformation> &queuesInformation,
std::vector<smpl::Queue *> &queues, time_t time, int number);
void updateDeviceInformation(DeviceInformation &deviceInformation, smpl::Device *device, time_t time,
int number);
void updateQueueInformation(QueueInformation &queueInformation, smpl::Queue * queue, time_t time, int number);
static double avgSum(const std::vector<double> &values, int l, int r);
public:
MultiSMPL(const std::map<smpl::u64, void (*)(std::pair<smpl::u64, smpl::transact_t>, int, smpl::Engine *)> &handlers,
Meta &DevicesAndQueuesNames, std::ostream *fileOutputStream, std::ostream *csvOutputStream);
void run(int testsCount, std::pair<smpl::u64, smpl::transact_t> startEvent, time_t startTime,
time_t (*monitorTime)(smpl::transact_t));
static std::string printCSVTable(const std::vector<std::vector<std::string>> &table);
template<typename T>
static std::string toCSVString(T x);
template <typename T>
static void addToVector(std::vector<T> &vector, T value, int index);
template <typename T>
static std::vector<std::vector<T>> makeTable(const std::vector<T> &value, T step);
template <typename T>
static std::vector<std::vector<T>> makeTable(const std::vector<T> &first, const std::vector<T> &second);
template <typename T>
static std::vector<std::vector<std::string>> toStringTable(const std::vector<std::vector<T>> &table,
std::string (*toString)(T));
template <typename T>
static std::vector<std::string> toStringVector(const std::vector<T> &vector, std::string (*toString)(T));
template <typename T>
static std::vector<T> division(const std::vector<T> &vec, T value);
static std::vector<double> welch(const std::vector<double> &values, int w);
};
MultiSMPL::MultiSMPL(
const std::map<smpl::u64, void (*)(std::pair<smpl::u64, smpl::transact_t>, int, smpl::Engine *)> &handlers,
Meta &DevicesAndQueuesNames, std::ostream *fileOutputStream = nullptr, std::ostream *csvOutputStream = nullptr) {
this->handlers = handlers;
this->fileOutputStream = fileOutputStream;
this->csvOutputStream = csvOutputStream;
this->meta = DevicesAndQueuesNames;
}
void MultiSMPL::run(int testsCount, std::pair<smpl::u64, smpl::transact_t> startEvent, time_t startTime,
time_t (*monitorTime)(smpl::transact_t)) {
assert(startTime >= 0);
std::vector<QueueInformation> queuesInformation((int)meta.queues.size());
std::vector<DeviceInformation> devicesInformation((int)meta.devices.size());
std::vector<double> monitoringTimes;
for (int i = 0; i < testsCount; i++) {
if (fileOutputStream != nullptr)
*fileOutputStream << std::endl << "Прогон номер " << i + 1 << std::endl << std::endl;
smpl::Engine * e = new smpl::Engine(this->fileOutputStream);
for (int i = 0; i < meta.queues.size(); i++) {
e->createQueue(meta.queues[i]);
}
for (int i = 0; i < meta.devices.size(); i++) {
e->createDevice(meta.devices[i]);
}
e->schedule(startEvent.first, startTime, startEvent.second);
if (monitorTime != nullptr)
e->schedule(SystemEventMonitor, monitorTime(0), 0);
int event = SystemEventEnd;
do {
std::pair<smpl::u64, smpl::transact_t> top = e->cause();
event = top.first;
smpl::transact_t transact = top.second;
if (event == SystemEventMonitor) {
updateDevicesInformation(devicesInformation, e->getDevices(), e->getTime(), (int)transact);
updateQueuesInformation(queuesInformation, e->getQueues(), e->getTime(), (int)transact);
if (monitoringTimes.size() <= (int)transact)
monitoringTimes.push_back(e->getTime());
e->schedule(SystemEventMonitor, monitorTime(transact + 1), transact + 1);
}
handlers[event](top, i, e);
} while (event != SystemEventEnd);
if (fileOutputStream != nullptr) e->monitor();
if (fileOutputStream != nullptr) e->report();
if (fileOutputStream != nullptr) {
for (int i = 0; i < 1000; i++)
*fileOutputStream << "-";
*fileOutputStream << std::endl;
}
delete e;
}
for (int i = 0; i < meta.devices.size(); i++) {
if (fileOutputStream != nullptr)
*fileOutputStream << "Усредненные срезы параметров устройства " << meta.devices[i] << std::endl;
if (csvOutputStream != nullptr)
*csvOutputStream << "Усредненные срезы параметров устройства " << meta.devices[i] << std::endl;
if (fileOutputStream != nullptr) {
*fileOutputStream << "Среднее время работы: " << std::endl;
*fileOutputStream << smpl::Engine::printTable(toStringTable(
makeTable(monitoringTimes, division(devicesInformation[i].avgReserveTime,(double)testsCount) ),
smpl::Engine::toString)) << std::endl;
}
if (csvOutputStream != nullptr) {
*csvOutputStream << "Среднее время работы: " << std::endl;
*csvOutputStream << printCSVTable(toStringTable(
makeTable(monitoringTimes, division(devicesInformation[i].avgReserveTime, (double)testsCount)),
toCSVString)) << std::endl;
}
if (fileOutputStream != nullptr) {
*fileOutputStream << "Средний процент работы: " << std::endl;
*fileOutputStream << smpl::Engine::printTable(toStringTable(
makeTable(monitoringTimes, division(devicesInformation[i].avgPercentTime, (double)testsCount)),
smpl::Engine::toString)) << std::endl;
}
if (csvOutputStream != nullptr) {
*csvOutputStream << "Средний процент работы: " << std::endl;
*csvOutputStream << printCSVTable(toStringTable(
makeTable(monitoringTimes, division(devicesInformation[i].avgPercentTime, (double)testsCount)),
toCSVString)) << std::endl;
}
}
for (int i = 0; i < meta.queues.size(); i++) {
if (fileOutputStream != nullptr)
*fileOutputStream << "Усредненные срезы параметров очереди " << meta.queues[i] << std::endl;
if (csvOutputStream != nullptr)
*csvOutputStream << "Усредненные срезы параметров очереди " << meta.queues[i] << std::endl;
if (fileOutputStream != nullptr) {
*fileOutputStream << "Средняя длина очереди: " << std::endl;
*fileOutputStream << smpl::Engine::printTable(toStringTable(
makeTable(monitoringTimes, division(queuesInformation[i].avgLength, (double)testsCount)),
smpl::Engine::toString)) << std::endl;
}
if (csvOutputStream != nullptr) {
*csvOutputStream << "Средняя длина очереди: " << std::endl;
*csvOutputStream << printCSVTable(
toStringTable(
makeTable(monitoringTimes, division(queuesInformation[i].avgLength, (double)testsCount)),
toCSVString)) << std::endl;
}
if (fileOutputStream != nullptr) {
*fileOutputStream << "Время ожидания: " << std::endl;
*fileOutputStream << smpl::Engine::printTable(toStringTable(
makeTable(monitoringTimes, division(queuesInformation[i].avgWaitTime, (double)testsCount)),
smpl::Engine::toString)) << std::endl;
}
if (csvOutputStream != nullptr) {
*csvOutputStream << "Время ожидания: " << std::endl;
*csvOutputStream << printCSVTable(
toStringTable(makeTable(
monitoringTimes,division(queuesInformation[i].avgWaitTime, (double)testsCount)),
toCSVString)) << std::endl;
}
if (fileOutputStream != nullptr) {
*fileOutputStream << "Длина очереди: " << std::endl;
*fileOutputStream << smpl::Engine::printTable(toStringTable(
makeTable(monitoringTimes, division(queuesInformation[i].length, (double)testsCount)),
smpl::Engine::toString)) << std::endl;
}
if (csvOutputStream != nullptr) {
*csvOutputStream << "Длина очереди: " << std::endl;
*csvOutputStream << printCSVTable(
toStringTable(makeTable(monitoringTimes, division(queuesInformation[i].length,
(double)testsCount)), toCSVString)) << std::endl;
}
}
}
std::string MultiSMPL::printCSVTable(const std::vector<std::vector<std::string>> &table) {
std::string result = "";
for (int i = 0; i < table.size(); i++) {
for (int j = 0; j < table[i].size(); j++) {
result += table[i][j] + ";";
}
result += "\n";
}
return result;
}
template<typename T>
std::string MultiSMPL::toCSVString(T x) {
std::stringstream ss;
ss.imbue(std::locale(ss.getloc(), new DecPoint(',')));
ss << std::fixed << std::setprecision(5) << x;
return ss.str();
}
void MultiSMPL::updateDevicesInformation(std::vector<DeviceInformation> &devicesInformation,
std::vector<smpl::Device *> &devices, time_t time, int number) {
for (int i = 0; i < devices.size(); i++) {
updateDeviceInformation(devicesInformation[i], devices[i], time, number);
}
}
void MultiSMPL::updateQueuesInformation(std::vector<QueueInformation> &queuesInformation,
std::vector<smpl::Queue *> &queues, time_t time, int number) {
for (int i = 0; i < queues.size(); i++) {
updateQueueInformation(queuesInformation[i], queues[i], time, number);
}
}
void MultiSMPL::updateDeviceInformation(MultiSMPL::DeviceInformation &deviceInformation, smpl::Device *device,
time_t time, int number) {
addToVector(deviceInformation.avgReserveTime, device->transactCount?
device->timeUsedSum * 1.0 / device->transactCount:0, number);
addToVector(deviceInformation.avgPercentTime, device->timeUsedSum * 1.0 / time * 100, number);
}
void
MultiSMPL::updateQueueInformation(MultiSMPL::QueueInformation &queueInformation, smpl::Queue *queue, time_t time,
int number) {
addToVector(queueInformation.avgLength, queue->timeQueueSum*1.0 / time, number);
addToVector(queueInformation.avgWaitTime, queue->count?
queue->waitTimeSum * 1.0 / queue->count:0, number);
addToVector(queueInformation.length, (double)queue->length(), number);
}
template <typename T>
void MultiSMPL::addToVector(std::vector<T> &vector, T value, int index) {
assert(index >= 0);
while (vector.size() <= index)
vector.push_back(0.0);
vector[index] += value;
}
template <typename T>
std::vector<T> MultiSMPL::division(const std::vector<T> &vec, T value) {
std::vector<double> result = vec;
for (int i = 0; i < vec.size(); i++) {
result[i] /= value;
}
return result;
}
template <typename T>
std::vector<std::vector<T>> MultiSMPL::makeTable(const std::vector<T> &value, T step) {
std::vector<std::vector<T>> result(2, std::vector<T>(value.size()));
for (int i = 0; i < value.size(); i++) {
result[0][i] = (i + 1) * step;
result[1][i] = value[i];
}
return result;
}
template <typename T>
std::vector<std::vector<std::string>>
MultiSMPL::toStringTable(const std::vector<std::vector<T>> &table, std::string (*toString)(T)) {
if (table.size() == 0) return std::vector<std::vector<std::string>>(0);
std::vector<std::vector<std::string>> result(table.size());
for (int i = 0; i < table.size(); i++) {
result[i] = toStringVector(table[i], toString);
}
return result;
}
template<typename T>
std::vector<std::string> MultiSMPL::toStringVector(const std::vector<T> &vector, std::string (*toString)(T)) {
std::vector<std::string> result(vector.size());
for (int i = 0; i < vector.size(); i++) {
result[i] = !__isnan(vector[i]) ? toString(vector[i]) : "-";
}
return result;
}
template<typename T>
std::vector<std::vector<T>> MultiSMPL::makeTable(const std::vector<T> &first, const std::vector<T> &second) {
if (first.size() != second.size()) return std::vector<std::vector<T>>(0);
std::vector<std::vector<T>> result(2, std::vector<T>(first.size()));
for (int i = 0; i < first.size(); i++) {
result[0][i] = first[i];
result[1][i] = second[i];
}
return result;
}
std::vector<double> MultiSMPL::welch(const std::vector<double> &values, int w) {
std::vector<double> result(values.size() - w);
for (int i = 0; i < (int)values.size() - w; i++) {
result[i] = avgSum(values, i - std::min(w, i), i + std::min(w, i));
}
return result;
}
double MultiSMPL::avgSum(const std::vector<double> &values, int l, int r) {
double res = 0;
for (int i = l; i <= r; i++) {
res += values[i];
}
return res / (r - l + 1);
}
}
#endif //PROJECT_MULTISMPL_H