Repository navigation
Expand file tree
/
Copy pathcompare-results.js
More file actions
319 lines (280 loc) · 8.36 KB
/
Copy pathcompare-results.js
File metadata and controls
319 lines (280 loc) · 8.36 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
#!/usr/bin/env node
/**
* Performance Regression Detection Script
*
* Compares current test results against baseline results stored per branch.
* Baselines are stored in .performance-baselines/ directory.
*
* Usage:
* node compare-results.js [--save-baseline] [--branch <name>] [--threshold <pct>]
*
* Examples:
* node compare-results.js # Compare against baseline
* node compare-results.js --save-baseline # Save current as baseline
* node compare-results.js --branch main # Compare against main baseline
* node compare-results.js --threshold 15 # Use 15% regression threshold
*/
const fs = require("fs");
const path = require("path");
const REPORTS_DIR = path.join(__dirname, "..", "backend", "reports");
const BASELINES_DIR = path.join(__dirname, "..", ".performance-baselines");
// Parse CLI arguments
const args = process.argv.slice(2);
const saveBaseline = args.includes("--save-baseline");
const branchIdx = args.indexOf("--branch");
const branch =
branchIdx !== -1
? args[branchIdx + 1]
: process.env.GITHUB_REF_NAME || "main";
const thresholdIdx = args.indexOf("--threshold");
const regressionThreshold =
thresholdIdx !== -1 ? parseFloat(args[thresholdIdx + 1]) : 10;
// Ensure baselines directory exists
if (!fs.existsSync(BASELINES_DIR)) {
fs.mkdirSync(BASELINES_DIR, { recursive: true });
}
/**
* Load test results from reports directory
*/
function loadResults(filename) {
const filePath = path.join(REPORTS_DIR, filename);
if (!fs.existsSync(filePath)) {
return null;
}
try {
return JSON.parse(fs.readFileSync(filePath, "utf8"));
} catch (e) {
console.error(`Failed to parse ${filename}:`, e.message);
return null;
}
}
/**
* Load baseline results for a specific branch
*/
function loadBaseline(testName, branchName) {
const baselineFile = path.join(
BASELINES_DIR,
`${branchName}-${testName}-baseline.json`,
);
if (!fs.existsSync(baselineFile)) {
return null;
}
try {
return JSON.parse(fs.readFileSync(baselineFile, "utf8"));
} catch (e) {
console.error(`Failed to parse baseline ${baselineFile}:`, e.message);
return null;
}
}
/**
* Save current results as baseline for a branch
*/
function saveAsBaseline(testName, branchName, results) {
const baselineFile = path.join(
BASELINES_DIR,
`${branchName}-${testName}-baseline.json`,
);
fs.writeFileSync(baselineFile, JSON.stringify(results, null, 2));
console.log(`✓ Saved baseline: ${baselineFile}`);
}
/**
* Compare two metric values and calculate regression percentage
*/
function compareMetrics(baseline, current, metricPath) {
const baselineValue = getNestedValue(baseline, metricPath);
const currentValue = getNestedValue(current, metricPath);
if (baselineValue === null || currentValue === null) {
return null;
}
const change = ((currentValue - baselineValue) / baselineValue) * 100;
return {
baseline: baselineValue,
current: currentValue,
change: change,
regression: change > regressionThreshold,
};
}
/**
* Get nested object value by dot-notation path
*/
function getNestedValue(obj, path) {
return path.split(".").reduce((current, key) => {
return current && current[key] !== undefined ? current[key] : null;
}, obj);
}
/**
* Format percentage change with sign
*/
function formatChange(change) {
const sign = change > 0 ? "+" : "";
return `${sign}${change.toFixed(2)}%`;
}
/**
* Define metrics to compare
*/
const METRICS = [
{
name: "Avg Response Time",
path: "metrics.http_req_duration.values.avg",
unit: "ms",
threshold: regressionThreshold,
direction: "lower-is-better",
},
{
name: "P95 Response Time",
path: "metrics.http_req_duration.values.p(95)",
unit: "ms",
threshold: regressionThreshold,
direction: "lower-is-better",
},
{
name: "P99 Response Time",
path: "metrics.http_req_duration.values.p(99)",
unit: "ms",
threshold: regressionThreshold,
direction: "lower-is-better",
},
{
name: "Error Rate",
path: "metrics.http_req_failed.values.rate",
unit: "%",
threshold: regressionThreshold,
multiply: 100,
direction: "lower-is-better",
},
{
name: "Throughput",
path: "metrics.http_reqs.values.rate",
unit: "req/s",
threshold: -regressionThreshold,
direction: "higher-is-better",
},
{
name: "Cache Hit Rate",
path: "cache.hit_rate_pct",
unit: "%",
threshold: -regressionThreshold,
direction: "higher-is-better",
},
];
/**
* Main comparison logic
*/
function compareResults(testName, currentResults) {
const baseline = loadBaseline(testName, branch);
if (!baseline) {
const baselineFile = path.join(
BASELINES_DIR,
`${branch}-${testName}-baseline.json`,
);
console.warn(
`\n⚠️ NO BASELINE TO COMPARE AGAINST for test '${testName}' on branch '${branch}'.`,
);
console.warn(` Expected baseline file: ${baselineFile}`);
console.warn(
" This is NOT a 'no regression' result — regression detection was skipped.",
);
console.warn(
" Generate a baseline by running this script with --save-baseline on the target branch.",
);
return { hasRegression: false, regressions: [], noBaseline: true };
}
console.log(
`\n📊 Comparing ${testName} against baseline (branch: ${branch})`,
);
console.log("=".repeat(90));
let hasRegression = false;
const regressions = [];
console.log(
"Metric".padEnd(25) +
"Baseline".padEnd(15) +
"Current".padEnd(15) +
"Change".padEnd(12) +
"Status",
);
console.log("-".repeat(90));
METRICS.forEach((metric) => {
const comparison = compareMetrics(baseline, currentResults, metric.path);
if (comparison === null) {
console.log(`${metric.name.padEnd(25)} N/A`);
return;
}
let baselineValue = comparison.baseline;
let currentValue = comparison.current;
if (metric.multiply) {
baselineValue *= metric.multiply;
currentValue *= metric.multiply;
}
const baselineStr = `${baselineValue.toFixed(2)}${metric.unit}`.padEnd(15);
const currentStr = `${currentValue.toFixed(2)}${metric.unit}`.padEnd(15);
const changeStr = formatChange(comparison.change).padEnd(12);
let status = "✅ OK";
let isRegression = false;
if (
metric.direction === "lower-is-better" &&
comparison.change > metric.threshold
) {
status = "⚠️ REGRESSION";
isRegression = true;
hasRegression = true;
} else if (
metric.direction === "higher-is-better" &&
comparison.change < metric.threshold
) {
status = "⚠️ REGRESSION";
isRegression = true;
hasRegression = true;
}
if (isRegression) {
regressions.push({
metric: metric.name,
baseline: baselineValue,
current: currentValue,
change: comparison.change,
});
}
console.log(
`${metric.name.padEnd(25)} ${baselineStr} ${currentStr} ${changeStr} ${status}`,
);
});
console.log("=".repeat(90));
return { hasRegression, regressions };
}
/**
* Load and display error budget trend
*/
function displayErrorBudgetTrend() {
try {
const errorBudgetModule = require('./calculate-error-budget.js');
const trends = errorBudgetModule.calculateErrorBudgetTrend(10);
if (trends.message) {
console.log(`\n${trends.message}`);
return;
}
console.log('\n📊 Error Budget Trend (Last 10 Runs):');
console.log('─'.repeat(90));
Object.values(trends).forEach(trend => {
const trendEmoji = {
improving: '📈',
degrading: '📉',
stable: '➡️',
}[trend.trend] || '❓';
console.log(`${trendEmoji} ${trend.slo_name}`);
console.log(` Avg Remaining: ${trend.avg_remaining}% | Min: ${trend.min_remaining.toFixed(2)}% | Max: ${trend.max_remaining.toFixed(2)}%`);
});
} catch (e) {
console.log('\n⚠️ Could not load error budget trend:', e.message);
}
}
/**
* Generate markdown report for PR comment
*/
function generateMarkdownReport(testResults) {
let markdown = "## 📊 Performance Regression Detection\n\n";
let hasAnyRegression = false;
const allRegressions = [];
Object.entries(testResults).forEach(([testName, result]) => {
if (result.hasRegression) {
hasAnyRegression = true;
}
/* … truncated 2799 chars — edit only what you need near the top … */