-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathbench_utils.h
More file actions
289 lines (253 loc) · 9.56 KB
/
Copy pathbench_utils.h
File metadata and controls
289 lines (253 loc) · 9.56 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
// Copyright(C) 2025-2026 Advanced Micro Devices, Inc. All rights reserved.
// SPDX-License-Identifier: MIT
//
// Benchmark utilities: timer, memory tracker, result I/O, and comparison.
#pragma once
#include <algorithm>
#include <chrono>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
// Platform-specific memory headers
#if defined(_WIN32)
// WIN32_LEAN_AND_MEAN prevents <windows.h> from pulling in <winsock.h>,
// avoiding redefinition conflicts when <httplib.h> later includes <winsock2.h>.
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# ifndef NOMINMAX
# define NOMINMAX
# endif
# include <windows.h>
# include <psapi.h>
#elif defined(__APPLE__)
# include <mach/mach.h>
#else
# include <fstream>
#endif
#include <nlohmann/json.hpp>
using json = nlohmann::json;
namespace bench {
// ---------------------------------------------------------------------------
// Timer
// ---------------------------------------------------------------------------
class Timer {
public:
void start() { start_ = std::chrono::high_resolution_clock::now(); }
void stop() { end_ = std::chrono::high_resolution_clock::now(); }
double elapsedUs() const {
if (end_ < start_) return 0.0;
return static_cast<double>(
std::chrono::duration_cast<std::chrono::microseconds>(end_ - start_).count());
}
double elapsedMs() const { return elapsedUs() / 1000.0; }
private:
std::chrono::high_resolution_clock::time_point start_;
std::chrono::high_resolution_clock::time_point end_;
};
// ---------------------------------------------------------------------------
// MemoryTracker — returns current process RSS in KB
// ---------------------------------------------------------------------------
class MemoryTracker {
public:
static long getCurrentRssKb() {
#if defined(_WIN32)
PROCESS_MEMORY_COUNTERS pmc;
if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc))) {
return static_cast<long>(pmc.WorkingSetSize / 1024);
}
return 0;
#elif defined(__APPLE__)
mach_task_basic_info info;
mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT;
if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO,
reinterpret_cast<task_info_t>(&info), &count) == KERN_SUCCESS) {
return static_cast<long>(info.resident_size / 1024);
}
return 0;
#else
// Linux: parse /proc/self/status for VmRSS
std::ifstream f("/proc/self/status");
std::string line;
while (std::getline(f, line)) {
if (line.rfind("VmRSS:", 0) == 0) {
std::istringstream iss(line);
std::string key;
long val = 0;
iss >> key >> val;
return val; // already in KB
}
}
return 0;
#endif
}
};
// ---------------------------------------------------------------------------
// BenchmarkResult
// ---------------------------------------------------------------------------
struct BenchmarkResult {
std::string name;
double value;
std::string unit;
};
// ---------------------------------------------------------------------------
// JSON I/O
// ---------------------------------------------------------------------------
inline void writeBenchmarkResults(const std::string& path,
const std::vector<BenchmarkResult>& results) {
// Timestamp (thread-safe via gmtime_r / gmtime_s)
auto now = std::chrono::system_clock::now();
std::time_t t = std::chrono::system_clock::to_time_t(now);
std::tm tm_buf{};
#if defined(_WIN32)
gmtime_s(&tm_buf, &t);
#else
gmtime_r(&t, &tm_buf);
#endif
std::ostringstream ts;
ts << std::put_time(&tm_buf, "%Y-%m-%dT%H:%M:%SZ");
// Platform string
std::string platform;
#if defined(_WIN32)
platform = "windows";
#elif defined(__APPLE__)
platform = "macos";
#else
platform = "linux";
#endif
json root;
root["timestamp"] = ts.str();
root["platform"] = platform;
json arr = json::array();
for (const auto& r : results) {
arr.push_back({{"name", r.name}, {"value", r.value}, {"unit", r.unit}});
}
root["results"] = arr;
std::ofstream f(path);
if (!f.is_open()) {
throw std::runtime_error("Cannot write benchmark results to: " + path);
}
f << root.dump(2) << "\n";
}
inline std::vector<BenchmarkResult> readBenchmarkResults(const std::string& path) {
std::ifstream f(path);
if (!f.is_open()) {
throw std::runtime_error("Cannot read benchmark results from: " + path);
}
json root = json::parse(f);
std::vector<BenchmarkResult> out;
for (const auto& r : root.at("results")) {
out.push_back({r.at("name").get<std::string>(), r.at("value").get<double>(),
r.at("unit").get<std::string>()});
}
return out;
}
// ---------------------------------------------------------------------------
// Per-metric thresholds
// ---------------------------------------------------------------------------
inline double thresholdForMetric(const std::string& name) {
// Binary size metrics: 10% threshold (issue: "Fail if size regresses >10%")
if (name.find("binary_size") != std::string::npos) {
return 10.0;
}
// Per-step growth is a single-digit-KB metric, so a single feature addition
// (e.g. VLM image support adding ~4 KB/step for new content-block storage)
// reads as a 50%+ swing. Use a wider band so legitimate feature additions
// don't fail the gate while still catching true 2x-style regressions.
if (name == "memory_per_step_growth_kb") {
return 75.0;
}
// All other metrics: 15% threshold
return 15.0;
}
// ---------------------------------------------------------------------------
// compareAndReport: compare current vs baseline, return 0 if OK, 1 if regression
// ---------------------------------------------------------------------------
inline int compareAndReport(const std::string& baselinePath, const std::string& currentPath) {
std::vector<BenchmarkResult> baseline = readBenchmarkResults(baselinePath);
std::vector<BenchmarkResult> current = readBenchmarkResults(currentPath);
// Index baseline by name
std::map<std::string, double> baseMap;
for (const auto& r : baseline) {
baseMap[r.name] = r.value;
}
std::cout << "\n=== Benchmark Regression Report ===\n";
std::cout << std::left << std::setw(45) << "Metric"
<< std::right << std::setw(12) << "Baseline"
<< std::setw(12) << "Current"
<< std::setw(10) << "Change"
<< std::setw(12) << "Threshold"
<< std::setw(10) << "Status" << "\n";
std::cout << std::string(101, '-') << "\n";
bool anyRegression = false;
for (const auto& r : current) {
auto it = baseMap.find(r.name);
if (it == baseMap.end()) {
std::cout << std::left << std::setw(45) << r.name
<< std::right << std::setw(12) << "N/A"
<< std::setw(12) << r.value
<< std::setw(10) << "N/A"
<< std::setw(12) << "N/A"
<< std::setw(10) << "NEW" << "\n";
continue;
}
double base = it->second;
double threshold = thresholdForMetric(r.name);
double pct = (base == 0.0) ? 0.0 : (r.value - base) / base * 100.0;
std::string status;
if (pct > threshold) {
status = "FAIL";
anyRegression = true;
} else if (pct < -1.0) {
status = "IMPROVED";
} else {
status = "OK";
}
std::cout << std::left << std::setw(45) << r.name << std::right
<< std::setw(12) << std::fixed << std::setprecision(1) << base
<< std::setw(12) << r.value
<< std::setw(9) << std::showpos << pct << "%" << std::noshowpos
<< std::setw(12) << (std::to_string(static_cast<int>(threshold)) + "%")
<< std::setw(10) << status << "\n";
}
// Report baseline metrics absent from the current run (benchmark may have crashed)
for (const auto& b : baseline) {
bool found = false;
for (const auto& r : current) {
if (r.name == b.name) { found = true; break; }
}
if (!found) {
std::cout << std::left << std::setw(45) << b.name
<< std::right << std::setw(12) << std::fixed << std::setprecision(1)
<< b.value
<< std::setw(12) << "N/A"
<< std::setw(10) << "N/A"
<< std::setw(12) << "N/A"
<< std::setw(10) << "MISSING" << "\n";
anyRegression = true;
}
}
std::cout << std::string(101, '-') << "\n";
if (anyRegression) {
std::cout << "\nRESULT: REGRESSION DETECTED — one or more metrics exceed threshold or are missing\n";
return 1;
}
std::cout << "\nRESULT: PASS — no regressions detected\n";
return 0;
}
// ---------------------------------------------------------------------------
// Median helper
// ---------------------------------------------------------------------------
inline double median(std::vector<double> v) {
if (v.empty()) return 0.0;
std::sort(v.begin(), v.end());
size_t n = v.size();
return (n % 2 == 0) ? (v[n / 2 - 1] + v[n / 2]) / 2.0 : v[n / 2];
}
} // namespace bench