-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchy.cpp
326 lines (280 loc) · 11 KB
/
benchy.cpp
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
#include "benchy.h"
#include <bits/chrono.h>
#include <chrono>
#include <cstdio>
#include <optional>
#include <ostream>
#include <sstream>
#include <string>
#include <vector>
#include <cmath>
benchy::Bencher::Bencher(LogLevel logLevel, std::optional<std::ostream*> logStreamPtr) {
this->mLogLevel = logLevel;
if (logLevel == LogLevel::NONE) {
this->mLogStreamPtr = std::nullopt;
} else {
this->mLogStreamPtr = logStreamPtr;
}
}
void benchy::Bencher::execute(const std::string& benchmarkName, int iterations, std::function<void()> func) {
log("Executing benchmark: " + benchmarkName, LogLevel::INFO);
double overhead = _empty_loop_overhead_ns(1000);
std::vector<double> results;
for (int i = 0; i < iterations; i++) {
auto funcStart = std::chrono::high_resolution_clock::now();
func();
auto funcEnd = std::chrono::high_resolution_clock::now();
results.push_back(std::chrono::duration_cast<std::chrono::nanoseconds>(funcEnd - funcStart).count() - overhead);
}
mResults[benchmarkName] = BenchResult {
_vectorAverage(results),
_vectorMedian(results),
_vectorStdev(results),
_vectorSum(results),
_vectorMin(results),
_vectorMax(results),
iterations
};
log("Done running benchmark: " + benchmarkName + " average is " + _format_ns(mResults[benchmarkName].averageDuration), LogLevel::INFO);
}
void benchy::Bencher::queueExecution(const std::string& benchmarkName, int iterations, std::function<void()> func) {
queue.push(
EnqueuedBenchmark {
benchmarkName,
iterations,
func,
}
);
}
void benchy::Bencher::runAll() {
while (!queue.empty()) {
EnqueuedBenchmark front = queue.front();
execute(front.name, front.iterations, front.func);
queue.pop();
}
}
void benchy::Bencher::print_results() {
std::cout << std::left << std::setw(20) << "Benchmark"
<< std::setw(15) << "Avg Duration"
<< std::setw(15) << "Median Duration"
<< std::setw(15) << "Std Deviation"
<< std::setw(15) << "Total Duration"
<< std::setw(15) << "Min"
<< std::setw(15) << "Max"
<< std::setw(10) << "Iterations" << std::endl;
for (const auto& [name, result] : mResults) {
std::cout << std::left << std::setw(20) << name
<< std::setw(15) << _format_ns(result.averageDuration)
<< std::setw(15) << _format_ns(result.medianDuration)
<< std::setw(15) << _format_ns(result.standardDeviation)
<< std::setw(15) << _format_ns(result.totalDurations)
<< std::setw(15) << _format_ns(result.min)
<< std::setw(15) << _format_ns(result.max)
<< std::setw(10) << result.iterations << std::endl;
}
}
void benchy::Bencher::log_results() {
if (!should_log(LogLevel::RESULT))
return;
std::stringstream ss;
ss << std::left << std::setw(20) << "Benchmark"
<< std::setw(15) << "Avg Duration"
<< std::setw(15) << "Median Duration"
<< std::setw(15) << "Std Deviation"
<< std::setw(15) << "Total Duration"
<< std::setw(15) << "Min"
<< std::setw(15) << "Max"
<< std::setw(10) << "Iterations" << std::endl;
for (const auto& [name, result] : mResults) {
ss << std::left << std::setw(20) << name
<< std::setw(15) << _format_ns(result.averageDuration)
<< std::setw(15) << _format_ns(result.medianDuration)
<< std::setw(15) << _format_ns(result.standardDeviation)
<< std::setw(15) << _format_ns(result.totalDurations)
<< std::setw(15) << _format_ns(result.min)
<< std::setw(15) << _format_ns(result.max)
<< std::setw(10) << result.iterations << std::endl;
}
*mLogStreamPtr.value() << _format_results(ss.str()) << std::endl;
}
bool benchy::Bencher::should_log(LogLevel logLevel) {
return (mLogStreamPtr == std::nullopt || // 1. Did we opt out of logging?
mLogStreamPtr.value() == nullptr || // 2. Is the pointer to the log stream null?
mLogLevel >= logLevel); // 3. Is mLogLevel >= to what we should log?
}
void benchy::Bencher::log(const std::string& message, LogLevel logLevel) {
if (!should_log(logLevel))
return;
*mLogStreamPtr.value() << _format_log(message, logLevel, _is_colorful_terminal(*mLogStreamPtr.value())) << std::endl;
}
std::string benchy::_format_log(const std::string& message, LogLevel logLevel, bool colorful = false) {
std::stringstream ss;
if (colorful) {
ss << _colorful_word("[" + _log_level_to_string(logLevel) + " " +
_datetime_now_string() + "]", _log_level_to_color_code(logLevel)) + " ";
} else {
ss << "[" + _log_level_to_string(logLevel) + " " + _datetime_now_string() + "] ";
}
ss << message;
return ss.str();
}
std::string benchy::_format_results(const std::string &results) {
return _colorful_word("[" + _log_level_to_string(LogLevel::RESULT) + " " +
_datetime_now_string() + "]", _log_level_to_color_code(LogLevel::RESULT))
+ "\n" + results;
}
bool benchy::_is_colorful_terminal(const std::ostream &os) {
if (&os == &std::cout || &os == &std::cerr || &os == &std::clog) {
#ifdef _WIN32
return true;
#else
return isatty(fileno(stdout));
#endif
}
return false;
}
std::string benchy::_format_ns(double ns) {
constexpr double ns_per_ms = 1e6;
constexpr double ns_per_sec = 1e9;
constexpr double ns_per_min = 6e10;
constexpr double ns_per_hour = 3.6e12;
constexpr double ns_per_day = 8.64e13;
std::ostringstream oss;
if (std::abs(ns) < ns_per_ms) {
oss << std::setprecision(2) << std::fixed << ns << "ns";
} else if (std::abs(ns) < ns_per_sec) {
oss << std::setprecision(2) << std::fixed << ns / ns_per_ms << "ms";
} else if (std::abs(ns) < ns_per_min) {
oss << std::setprecision(2) << std::fixed << ns / ns_per_sec << "s";
} else if (std::abs(ns) < ns_per_hour) {
oss << std::setprecision(2) << std::fixed << ns / ns_per_min << "min";
} else if (std::abs(ns) < ns_per_day) {
oss << std::setprecision(2) << std::fixed << ns / ns_per_hour << "h";
} else {
oss << std::setprecision(2) << std::fixed << ns / ns_per_day << "days";
}
return oss.str();
}
std::string benchy::_log_level_to_string(LogLevel logLevel) {
switch (logLevel) {
case LogLevel::NONE:
return "NONE";
case LogLevel::RESULT:
return "RESULT";
case LogLevel::ERROR:
return "ERROR";
case LogLevel::WARNING:
return "WARNING";
case LogLevel::INFO:
return "INFO";
default:
throw std::runtime_error("Unknown LogLevel");
}
}
std::string benchy::_log_level_to_color_code(LogLevel logLevel) {
switch (logLevel) {
case LogLevel::NONE:
return FORE_BLACK;
case LogLevel::RESULT:
return FORE_GREEN;
case LogLevel::ERROR:
return FORE_RED;
case LogLevel::WARNING:
return FORE_YELLOW;
case LogLevel::INFO:
return FORE_WHITE;
default:
throw std::runtime_error("Unknown LogLevel");
}
}
double benchy::_empty_loop_overhead_ns(double iterations) {
auto now = std::chrono::high_resolution_clock::now();
std::vector<std::chrono::nanoseconds> results;
for (int i = 0; i < iterations; i++) {
auto now = std::chrono::high_resolution_clock::now();
auto end = std::chrono::high_resolution_clock::now();
results.push_back(end - now);
}
auto end = std::chrono::high_resolution_clock::now();
return std::chrono::duration_cast<std::chrono::nanoseconds>(end - now).count() / iterations;
}
std::string benchy::_colorful_word(const std::string &word, const std::string& color) {
return color + word + RESET_FORE;
}
std::string benchy::_datetime_now_string() {
// Get current time
auto now = std::chrono::system_clock::now();
// Convert to time_t (seconds since epoch)
std::time_t now_time_t = std::chrono::system_clock::to_time_t(now);
// Convert to local time
std::tm local_tm = *std::localtime(&now_time_t);
// Format as string
std::stringstream ss;
ss << std::put_time(&local_tm, "%Y-%m-%d %H:%M:%S");
return ss.str();
}
double benchy::_vectorAverage(const std::vector<double>& doubleVec) {
return _vectorSum(doubleVec) / doubleVec.size();
}
double benchy::_vectorMedian(const std::vector<double>& doubleVec) {
// Make a copy of the vector and sort it
std::vector<double> sortedVec = doubleVec;
std::sort(sortedVec.begin(), sortedVec.end());
// Calculate the median
if (sortedVec.size() % 2 == 0) {
// If the size of the vector is even, average the middle two elements
int midIndex1 = sortedVec.size() / 2 - 1;
int midIndex2 = sortedVec.size() / 2;
return (sortedVec[midIndex1] + sortedVec[midIndex2]) / 2.0;
} else {
// If the size of the vector is odd, return the middle element
return sortedVec[sortedVec.size() / 2];
}
}
double benchy::_vectorStdev(const std::vector<double>& doubleVec) {
// Calculate the mean
double mean = _vectorSum(doubleVec) / doubleVec.size();
// Calculate the sum of squares of differences from the mean
double sumOfSquares = 0.0;
for (double value : doubleVec) {
double diff = value - mean;
sumOfSquares += diff * diff;
}
// Calculate the variance
double variance = sumOfSquares / doubleVec.size();
// Calculate the standard deviation as the square root of the variance
return std::sqrt(variance);
}
double benchy::_vectorSum(const std::vector<double>& doubleVec) {
double sum = 0;
for (double i : doubleVec) {
sum += i;
}
return sum;
}
double benchy::_vectorMin(const std::vector<double>& doubleVec) {
if (doubleVec.empty()) {
// Handle empty vector case
throw std::invalid_argument("Cannot find minimum of an empty vector");
}
double minVal = doubleVec[0]; // Assume the first element as the minimum
for (double val : doubleVec) {
if (val < minVal) {
minVal = val; // Update minVal if a smaller value is found
}
}
return minVal;
}
double benchy::_vectorMax(const std::vector<double>& doubleVec) {
if (doubleVec.empty()) {
// Handle empty vector case
throw std::invalid_argument("Cannot find maximum of an empty vector");
}
double maxVal = doubleVec[0]; // Assume the first element as the maximum
for (double val : doubleVec) {
if (val > maxVal) {
maxVal = val; // Update maxVal if a larger value is found
}
}
return maxVal;
}