Repository navigation
Expand file tree
/
Copy pathbenchmark_tool_standalone.cpp
More file actions
362 lines (310 loc) · 12.3 KB
/
Copy pathbenchmark_tool_standalone.cpp
File metadata and controls
362 lines (310 loc) · 12.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
#include <algorithm>
#include <chrono>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <random>
#include <string>
#include <thread>
#include <unordered_map>
#include <vector>
#include "include/bolt/core/benchmark_suite.hpp"
#include "include/bolt/core/logging.hpp"
#include "include/bolt/core/memory_manager.hpp"
#include "include/bolt/core/memory_pool.hpp"
#include "include/bolt/core/performance_profiler.hpp"
using namespace bolt; // NOLINT(build/namespaces)
// Simple test benchmarks for standalone demo
void benchmark_memory_allocation_demo(const BenchmarkConfig& config) {
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 benchmark_string_operations_demo(const BenchmarkConfig& config) {
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 benchmark_vector_operations_demo(const BenchmarkConfig& config) {
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 benchmark_threading_demo(const BenchmarkConfig& config) {
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 benchmark_file_io_demo(const BenchmarkConfig& config) {
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());
}
void registerDemoBenchmarks() {
auto& suite = BenchmarkSuite::getInstance();
BenchmarkConfig config1("memory_allocation_demo", "Basic memory allocation and deallocation performance");
config1.category = "DEMO";
config1.iterations = 50;
suite.registerBenchmark(config1, benchmark_memory_allocation_demo);
BenchmarkConfig config2("string_operations_demo", "String creation, manipulation, and destruction");
config2.category = "DEMO";
config2.iterations = 30;
suite.registerBenchmark(config2, benchmark_string_operations_demo);
BenchmarkConfig config3("vector_operations_demo", "Vector operations performance");
config3.category = "DEMO";
config3.iterations = 20;
suite.registerBenchmark(config3, benchmark_vector_operations_demo);
BenchmarkConfig config4("threading_demo", "Thread creation and synchronization");
config4.category = "DEMO";
config4.iterations = 10;
suite.registerBenchmark(config4, benchmark_threading_demo);
BenchmarkConfig config5("file_io_demo", "File I/O operations");
config5.category = "DEMO";
config5.iterations = 5;
suite.registerBenchmark(config5, benchmark_file_io_demo);
}
void printUsage(const char* programName) {
std::cout << "Usage: " << programName << " [options]\n\n";
std::cout << "Options:\n";
std::cout << " --help, -h Show this help message\n";
std::cout << " --list, -l List available benchmarks\n";
std::cout << " --categories, -c List available categories\n";
std::cout << " --run-all, -a Run all benchmarks\n";
std::cout << " --category <name> Run benchmarks in specific category\n";
std::cout << " --benchmark <name> Run specific benchmark\n";
std::cout << " --iterations <n> Set number of iterations (default: 10)\n";
std::cout << " --verbose, -v Enable verbose output\n";
std::cout << " --output-json <file> Generate JSON report\n";
std::cout << " --output-csv <file> Generate CSV report\n";
std::cout << " --output-html <file> Generate HTML report\n";
std::cout << "\nExamples:\n";
std::cout << " " << programName << " --run-all --output-html report.html\n";
std::cout << " " << programName << " --category DEMO --verbose\n";
std::cout << "\n";
}
void printBenchmarkSummary(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";
if (!result.errorMessage.empty()) {
std::cout << " Error: " << result.errorMessage << "\n";
}
}
// Calculate 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";
}
}
int main(int argc, char* argv[]) {
// Initialize logging
LogManager::configureConsoleLogging(LogLevel::INFO, false);
auto& suite = BenchmarkSuite::getInstance();
// Command line argument parsing
std::string command;
std::string specificBenchmark;
std::string specificCategory;
std::string jsonOutput;
std::string csvOutput;
std::string htmlOutput;
int iterations = 10;
bool verbose = false;
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg == "--help" || arg == "-h") {
printUsage(argv[0]);
return 0;
} else if (arg == "--list" || arg == "-l") {
command = "list";
} else if (arg == "--categories" || arg == "-c") {
command = "categories";
} else if (arg == "--run-all" || arg == "-a") {
command = "run-all";
} else if (arg == "--verbose" || arg == "-v") {
verbose = true;
} else if (arg == "--category" && i + 1 < argc) {
command = "category";
specificCategory = argv[++i];
} else if (arg == "--benchmark" && i + 1 < argc) {
command = "benchmark";
specificBenchmark = argv[++i];
} else if (arg == "--iterations" && i + 1 < argc) {
iterations = std::stoi(argv[++i]);
} else if (arg == "--output-json" && i + 1 < argc) {
jsonOutput = argv[++i];
} else if (arg == "--output-csv" && i + 1 < argc) {
csvOutput = argv[++i];
} else if (arg == "--output-html" && i + 1 < argc) {
htmlOutput = argv[++i];
} else {
std::cerr << "Unknown argument: " << arg << std::endl;
printUsage(argv[0]);
return 1;
}
}
if (command.empty()) {
command = "run-all"; // Default action
}
// Configure benchmark suite
suite.setDefaultIterations(iterations);
suite.enableVerboseOutput(verbose);
// Register demo benchmarks
registerDemoBenchmarks();
std::cout << "Bolt Performance Benchmark Suite (Standalone)\n";
std::cout << "=============================================\n\n";
try {
if (command == "list") {
auto benchmarks = suite.getAvailableBenchmarks();
std::cout << "Available Benchmarks:\n";
for (const auto& name : benchmarks) {
std::cout << " " << name << "\n";
}
std::cout << "\nTotal: " << benchmarks.size() << " benchmarks\n";
} else if (command == "categories") {
auto categories = suite.getAvailableCategories();
std::cout << "Available Categories:\n";
for (const auto& category : categories) {
std::cout << " " << category << "\n";
}
std::cout << "\nTotal: " << categories.size() << " categories\n";
} else if (command == "run-all") {
std::cout << "Running all benchmarks...\n";
auto results = suite.runAllBenchmarks();
printBenchmarkSummary(results);
// Generate reports
if (!jsonOutput.empty()) {
suite.generateJsonReport(results, jsonOutput);
std::cout << "JSON report saved to: " << jsonOutput << "\n";
}
if (!csvOutput.empty()) {
suite.generateCsvReport(results, csvOutput);
std::cout << "CSV report saved to: " << csvOutput << "\n";
}
if (!htmlOutput.empty()) {
suite.generateHtmlReport(results, htmlOutput);
std::cout << "HTML report saved to: " << htmlOutput << "\n";
}
} else if (command == "category") {
std::cout << "Running benchmarks in category: " << specificCategory << "\n";
auto results = suite.runBenchmarksByCategory(specificCategory);
if (results.empty()) {
std::cout << "No benchmarks found in category: " << specificCategory << "\n";
return 1;
}
printBenchmarkSummary(results);
} else if (command == "benchmark") {
std::cout << "Running benchmark: " << specificBenchmark << "\n";
auto result = suite.runBenchmark(specificBenchmark);
std::vector<BenchmarkResult> results = {result};
printBenchmarkSummary(results);
if (!result.isValid()) {
return 1;
}
}
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
std::cout << "\nBenchmark execution completed.\n";
return 0;
}