Repository navigation
Expand file tree
/
Copy pathdemo_benchmark_suite.cpp
More file actions
411 lines (344 loc) · 13.3 KB
/
Copy pathdemo_benchmark_suite.cpp
File metadata and controls
411 lines (344 loc) · 13.3 KB
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#include <chrono>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <random>
#include <string>
#include <thread>
#include <unordered_map>
#include <vector>
#include "include/bolt/core/logging.hpp"
#include "include/bolt/core/performance_profiler.hpp"
using namespace bolt; // NOLINT(build/namespaces)
// Simple benchmark structure
struct SimpleBenchmark {
std::string name;
std::string category;
std::string description;
int iterations;
std::function<void()> function;
};
struct BenchmarkResult {
std::string name;
std::string category;
double averageDurationMs;
double minDurationMs;
double maxDurationMs;
double standardDeviationMs;
int successfulRuns;
int failedRuns;
bool isValid() const { return successfulRuns > 0; }
double getSuccessRate() const {
return successfulRuns > 0 ? static_cast<double>(successfulRuns) / (successfulRuns + failedRuns) : 0.0;
}
};
class SimpleBenchmarkSuite {
private:
std::vector<SimpleBenchmark> benchmarks_;
bool verbose_;
double calculateStandardDeviation(const std::vector<double>& values, double mean) {
if (values.empty())
return 0.0;
double variance = 0.0;
for (double value : values) {
variance += (value - mean) * (value - mean);
}
variance /= values.size();
return std::sqrt(variance);
}
public:
SimpleBenchmarkSuite() : verbose_(false) {}
void setVerbose(bool verbose) { verbose_ = verbose; }
void registerBenchmark(const std::string& name, const std::string& category, const std::string& description,
int iterations, std::function<void()> function) {
benchmarks_.push_back({name, category, description, iterations, function});
if (verbose_) {
std::cout << "Registered benchmark: " << name << " (" << category << ")\n";
}
}
BenchmarkResult runBenchmark(const SimpleBenchmark& benchmark) {
BenchmarkResult result;
result.name = benchmark.name;
result.category = benchmark.category;
result.successfulRuns = 0;
result.failedRuns = 0;
std::vector<double> durations;
auto& profiler = PerformanceProfiler::getInstance();
profiler.enable();
if (verbose_) {
std::cout << "Running " << benchmark.name << " (" << benchmark.iterations << " iterations)...\n";
}
for (int i = 0; i < benchmark.iterations; ++i) {
try {
auto start = std::chrono::high_resolution_clock::now();
// Use profiler for this run
auto metric = profiler.startMetric(benchmark.name + "_run_" + std::to_string(i), benchmark.category);
benchmark.function();
profiler.endMetric(metric);
auto end = std::chrono::high_resolution_clock::now();
double duration = std::chrono::duration<double, std::milli>(end - start).count();
durations.push_back(duration);
result.successfulRuns++;
} catch (const std::exception& e) {
result.failedRuns++;
if (verbose_) {
std::cout << " Run " << i << " failed: " << e.what() << "\n";
}
}
}
if (!durations.empty()) {
result.averageDurationMs = std::accumulate(durations.begin(), durations.end(), 0.0) / durations.size();
result.minDurationMs = *std::min_element(durations.begin(), durations.end());
result.maxDurationMs = *std::max_element(durations.begin(), durations.end());
result.standardDeviationMs = calculateStandardDeviation(durations, result.averageDurationMs);
}
return result;
}
std::vector<BenchmarkResult> runAllBenchmarks() {
std::vector<BenchmarkResult> results;
std::cout << "Running " << benchmarks_.size() << " benchmarks...\n\n";
for (const auto& benchmark : benchmarks_) {
auto result = runBenchmark(benchmark);
results.push_back(result);
}
return results;
}
std::vector<BenchmarkResult> runCategory(const std::string& category) {
std::vector<BenchmarkResult> results;
for (const auto& benchmark : benchmarks_) {
if (benchmark.category == category) {
auto result = runBenchmark(benchmark);
results.push_back(result);
}
}
return results;
}
void generateReport(const std::vector<BenchmarkResult>& results, const std::string& filename) {
std::ofstream file(filename);
if (!file.is_open()) {
std::cerr << "Failed to open file: " << filename << std::endl;
return;
}
file << "{\n";
file << " \"benchmark_suite_version\": \"1.0.0\",\n";
file << " \"timestamp\": \""
<< std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch())
.count()
<< "\",\n";
file << " \"results\": [\n";
for (size_t i = 0; i < results.size(); ++i) {
const auto& result = results[i];
file << " {\n";
file << " \"name\": \"" << result.name << "\",\n";
file << " \"category\": \"" << result.category << "\",\n";
file << " \"average_duration_ms\": " << result.averageDurationMs << ",\n";
file << " \"min_duration_ms\": " << result.minDurationMs << ",\n";
file << " \"max_duration_ms\": " << result.maxDurationMs << ",\n";
file << " \"standard_deviation_ms\": " << result.standardDeviationMs << ",\n";
file << " \"successful_runs\": " << result.successfulRuns << ",\n";
file << " \"failed_runs\": " << result.failedRuns << ",\n";
file << " \"success_rate\": " << result.getSuccessRate() << "\n";
file << " }";
if (i < results.size() - 1)
file << ",";
file << "\n";
}
file << " ]\n";
file << "}\n";
file.close();
std::cout << "Report generated: " << filename << "\n";
}
void printSummary(const std::vector<BenchmarkResult>& results) {
std::cout << "\n=== Benchmark Summary ===\n";
std::cout << std::left << std::setw(25) << "Benchmark" << std::setw(10) << "Category" << std::setw(15)
<< "Avg Time (ms)" << std::setw(15) << "Min Time (ms)" << std::setw(15) << "Max Time (ms)"
<< std::setw(12) << "Success Rate"
<< "Status\n";
std::cout << std::string(95, '-') << "\n";
for (const auto& result : results) {
std::string status = result.isValid() ? "SUCCESS" : "FAILED";
std::cout << std::left << std::setw(25) << result.name.substr(0, 24) << std::setw(10) << result.category
<< std::setw(15) << std::fixed << std::setprecision(3) << result.averageDurationMs
<< std::setw(15) << std::fixed << std::setprecision(3) << result.minDurationMs << std::setw(15)
<< std::fixed << std::setprecision(3) << result.maxDurationMs << std::setw(12) << std::fixed
<< std::setprecision(1) << (result.getSuccessRate() * 100) << "%" << status << "\n";
}
// Summary statistics
int totalSuccessful = 0, totalFailed = 0;
double totalTime = 0.0;
for (const auto& result : results) {
if (result.isValid()) {
totalSuccessful++;
totalTime += result.averageDurationMs;
} else {
totalFailed++;
}
}
std::cout << "\n";
std::cout << "Total Benchmarks: " << results.size() << "\n";
std::cout << "Successful: " << totalSuccessful << "\n";
std::cout << "Failed: " << totalFailed << "\n";
if (totalSuccessful > 0) {
std::cout << "Average Time: " << std::fixed << std::setprecision(3) << (totalTime / totalSuccessful)
<< " ms\n";
}
}
};
// Benchmark functions
void memoryAllocationBenchmark() {
std::vector<void*> allocations;
const size_t numAllocations = 1000;
const size_t allocationSize = 1024;
// Allocate
for (size_t i = 0; i < numAllocations; ++i) {
void* ptr = malloc(allocationSize);
if (ptr) {
allocations.push_back(ptr);
}
}
// Deallocate
for (void* ptr : allocations) {
if (ptr) {
free(ptr);
}
}
}
void stringOperationsBenchmark() {
const int numStrings = 500;
std::vector<std::string> strings;
// String creation
for (int i = 0; i < numStrings; ++i) {
strings.emplace_back("Demo string " + std::to_string(i) + " with content");
}
// String manipulation
for (auto& str : strings) {
str += " modified";
str = str.substr(0, str.length() / 2);
std::transform(str.begin(), str.end(), str.begin(), ::toupper);
}
// String searching
int found = 0;
for (const auto& str : strings) {
if (str.find("DEMO") != std::string::npos) {
found++;
}
}
}
void vectorOperationsBenchmark() {
std::vector<int> vec;
const int numElements = 5000;
// Push back
for (int i = 0; i < numElements; ++i) {
vec.push_back(i);
}
// Random access
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, numElements - 1);
int sum = 0;
for (int i = 0; i < 1000; ++i) {
sum += vec[dis(gen)];
}
// Erase elements
while (!vec.empty() && vec.size() > numElements / 2) {
vec.erase(vec.begin() + vec.size() / 2);
}
}
void threadingBenchmark() {
const int numThreads = 4;
const int operationsPerThread = 500;
std::mutex testMutex;
int sharedCounter = 0;
std::vector<std::thread> threads;
for (int i = 0; i < numThreads; ++i) {
threads.emplace_back([&]() {
for (int j = 0; j < operationsPerThread; ++j) {
std::lock_guard<std::mutex> lock(testMutex);
sharedCounter++;
}
});
}
for (auto& thread : threads) {
if (thread.joinable()) {
thread.join();
}
}
}
void fileIoBenchmark() {
const std::string filename = "/tmp/benchmark_demo_file.txt";
const std::string content = "Demo content for file I/O benchmark. ";
const int numLines = 500;
// Write file
{
std::ofstream file(filename);
for (int i = 0; i < numLines; ++i) {
file << content << "Line " << i << std::endl;
}
}
// Read file
{
std::ifstream file(filename);
std::string line;
int lineCount = 0;
while (std::getline(file, line)) {
lineCount++;
}
}
// Clean up
std::remove(filename.c_str());
}
int main(int argc, char* argv[]) {
// Initialize logging
LogManager::configureConsoleLogging(LogLevel::INFO, false);
SimpleBenchmarkSuite suite;
// Parse command line arguments
bool verbose = false;
std::string outputFile;
std::string category;
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg == "--verbose" || arg == "-v") {
verbose = true;
} else if (arg == "--output" && i + 1 < argc) {
outputFile = argv[++i];
} else if (arg == "--category" && i + 1 < argc) {
category = argv[++i];
} else if (arg == "--help" || arg == "-h") {
std::cout << "Usage: " << argv[0] << " [options]\n";
std::cout << "Options:\n";
std::cout << " --verbose, -v Enable verbose output\n";
std::cout << " --output <file> Generate JSON report\n";
std::cout << " --category <name> Run specific category\n";
std::cout << " --help, -h Show this help\n";
return 0;
}
}
suite.setVerbose(verbose);
// Register benchmarks
suite.registerBenchmark("memory_allocation", "CORE", "Basic memory allocation and deallocation", 50,
memoryAllocationBenchmark);
suite.registerBenchmark("string_operations", "CORE", "String creation, manipulation, and searching", 30,
stringOperationsBenchmark);
suite.registerBenchmark("vector_operations", "CORE", "Vector operations performance", 20,
vectorOperationsBenchmark);
suite.registerBenchmark("threading", "CORE", "Thread creation and synchronization", 10, threadingBenchmark);
suite.registerBenchmark("file_io", "CORE", "File I/O operations", 5, fileIoBenchmark);
std::cout << "Bolt Performance Benchmark Suite\n";
std::cout << "================================\n";
// Run benchmarks
std::vector<BenchmarkResult> results;
if (!category.empty()) {
std::cout << "Running benchmarks in category: " << category << "\n";
results = suite.runCategory(category);
} else {
results = suite.runAllBenchmarks();
}
// Print summary
suite.printSummary(results);
// Generate report if requested
if (!outputFile.empty()) {
suite.generateReport(results, outputFile);
}
std::cout << "\nBenchmark execution completed.\n";
return 0;
}