Skip to content

Commit b24c614

Browse files
committed
Add performance improvements to alert comment
1 parent 5378675 commit b24c614

2 files changed

Lines changed: 105 additions & 26 deletions

File tree

src/write.ts

Lines changed: 97 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,11 @@ interface Alert {
9494
ratio: number;
9595
}
9696

97-
function findAlerts(curSuite: Benchmark, prevSuite: Benchmark, threshold: number): Alert[] {
97+
function findAlerts(curSuite: Benchmark, prevSuite: Benchmark, threshold: number): [Alert[], Alert[]] {
9898
core.debug(`Comparing current:${curSuite.commit.id} and prev:${prevSuite.commit.id} for alert`);
9999

100-
const alerts = [];
100+
const losses = [];
101+
const gains = [];
101102
for (const current of curSuite.benches) {
102103
const prev = prevSuite.benches.find((b) => b.name === current.name);
103104
if (prev === undefined) {
@@ -107,16 +108,20 @@ function findAlerts(curSuite: Benchmark, prevSuite: Benchmark, threshold: number
107108

108109
const ratio = getRatio(curSuite.tool, prev, current);
109110

110-
if (ratio > threshold) {
111+
if (threshold === 0) {
112+
gains.push({current, prev, ratio});
113+
} else if (ratio < 1/threshold) {
114+
gains.push({ current, prev, ratio });
115+
} else if (ratio > threshold) {
111116
core.warning(
112117
`Performance alert! Previous value was ${prev.value} and current value is ${current.value}.` +
113-
` It is ${ratio}x worse than previous exceeding a ratio threshold ${threshold}`,
118+
` It is ${ratio}x worse than previous, exceeding ratio threshold ${threshold}`,
114119
);
115-
alerts.push({ current, prev, ratio });
120+
losses.push({ current, prev, ratio });
116121
}
117122
}
118123

119-
return alerts;
124+
return [losses, gains];
120125
}
121126

122127
function getCurrentRepoMetadata() {
@@ -175,7 +180,10 @@ export function buildComment(
175180
'',
176181
expandableDetails ? '<details>' : '',
177182
'',
178-
`| Benchmark suite | Current: ${curSuite.commit.id} | Previous: ${prevSuite.commit.id} | Ratio |`,
183+
`Previous: ${prevSuite.commit.id}`,
184+
`Current: ${curSuite.commit.id}`,
185+
'',
186+
`| Benchmark suite | Current | Previous | Ratio |`,
179187
'|-|-|-|-|',
180188
];
181189

@@ -200,32 +208,89 @@ export function buildComment(
200208
return lines.join('\n');
201209
}
202210

211+
function pushResultLines(results: Alert[], output: string[]) {
212+
results.sort((a, b) => a.ratio - b.ratio);
213+
for (const alert of results) {
214+
const { current, prev, ratio } = alert;
215+
const line = `| \`${current.name}\` | ${strVal(current)} | ${strVal(prev)} | \`${floatStr(ratio)}\` |`;
216+
output.push(line);
217+
}
218+
}
219+
220+
const RESULT_TABLE_HEADER = [
221+
'',
222+
`| Benchmark suite | Current | Previous | Ratio |`,
223+
'|-|-|-|-|',
224+
];
225+
226+
227+
function buildReportComment(
228+
results: Alert[],
229+
benchName: string,
230+
curSuite: Benchmark,
231+
prevSuite: Benchmark,
232+
cc: string[],
233+
): string {
234+
// Do not show benchmark name if it is the default value 'Benchmark'.
235+
const benchmarkText = benchName === 'Benchmark' ? '' : ` **'${benchName}'**`;
236+
const lines = [
237+
'# Performance Report',
238+
'',
239+
`For benchmark${benchmarkText}.`,
240+
'',
241+
`Previous commit: ${prevSuite.commit.id}`,
242+
`Current commit: ${curSuite.commit.id}`,
243+
];
244+
245+
lines.push(...RESULT_TABLE_HEADER);
246+
pushResultLines(results, lines);
247+
lines.push('', commentFooter());
248+
249+
if (cc.length > 0) {
250+
lines.push('', `CC: ${cc.join(' ')}`);
251+
}
252+
253+
return lines.join('\n');
254+
}
255+
203256
function buildAlertComment(
204-
alerts: Alert[],
257+
losses: Alert[],
258+
gains: Alert[],
205259
benchName: string,
206260
curSuite: Benchmark,
207261
prevSuite: Benchmark,
208262
threshold: number,
209263
cc: string[],
210264
): string {
211265
// Do not show benchmark name if it is the default value 'Benchmark'.
212-
const benchmarkText = benchName === 'Benchmark' ? '' : ` **'${benchName}'**`;
213-
const title = threshold === 0 ? '# Performance Report' : '# :warning: **Performance Alert** :warning:';
266+
const benchmarkText = benchName === 'Benchmark' ? '' : ` for **'${benchName}'**`;
214267
const thresholdString = floatStr(threshold);
215268
const lines = [
216-
title,
269+
`# Performance Report${benchmarkText}`,
217270
'',
218-
`Possible performance regression was detected for benchmark${benchmarkText}.`,
219-
`Benchmark result of this commit is worse than the previous benchmark result exceeding threshold \`${thresholdString}\`.`,
271+
`Benchmark result(s) exceed ratio of \`${thresholdString}\`.`,
220272
'',
221-
`| Benchmark suite | Current: ${curSuite.commit.id} | Previous: ${prevSuite.commit.id} | Ratio |`,
222-
'|-|-|-|-|',
273+
`Previous commit: ${prevSuite.commit.id}`,
274+
`Current commit: ${curSuite.commit.id}`,
223275
];
224276

225-
for (const alert of alerts) {
226-
const { current, prev, ratio } = alert;
227-
const line = `| \`${current.name}\` | ${strVal(current)} | ${strVal(prev)} | \`${floatStr(ratio)}\` |`;
228-
lines.push(line);
277+
if (gains.length > 0) {
278+
lines.push(...[
279+
'',
280+
'### :rocket: The following benchmarks show improvements:',
281+
]);
282+
lines.push(...RESULT_TABLE_HEADER);
283+
pushResultLines(gains, lines);
284+
285+
}
286+
287+
if (losses.length > 0) {
288+
lines.push(...[
289+
'',
290+
'### :snail: The following benchmarks show regressions:',
291+
]);
292+
lines.push(...RESULT_TABLE_HEADER);
293+
pushResultLines(losses, lines);
229294
}
230295

231296
// Footer
@@ -276,14 +341,22 @@ async function handleAlert(benchName: string, curSuite: Benchmark, prevSuite: Be
276341
return;
277342
}
278343

279-
const alerts = findAlerts(curSuite, prevSuite, alertThreshold);
344+
const [losses, gains] = findAlerts(curSuite, prevSuite, alertThreshold);
345+
const alerts = [...losses, ...gains];
280346
if (alerts.length === 0) {
281347
core.debug('No performance alert found happily');
282348
return;
283349
}
284350

285-
core.debug(`Found ${alerts.length} alerts`);
286-
const body = buildAlertComment(alerts, benchName, curSuite, prevSuite, alertThreshold, alertCommentCcUsers);
351+
let body = '';
352+
if (alertThreshold === 0) {
353+
core.debug(`Alert threshold is 0. Leaving report with ${alerts.length} alerts`);
354+
body = buildReportComment(alerts, benchName, curSuite, prevSuite, alertCommentCcUsers);
355+
} else {
356+
core.debug(`Found ${alerts.length} alerts`);
357+
body = buildAlertComment(losses, gains, benchName, curSuite, prevSuite, alertThreshold, alertCommentCcUsers);
358+
}
359+
287360
let message = body;
288361

289362
if (commentOnAlert) {
@@ -297,9 +370,9 @@ async function handleAlert(benchName: string, curSuite: Benchmark, prevSuite: Be
297370

298371
if (failOnAlert) {
299372
// Note: alertThreshold is smaller than failThreshold. It was checked in config.ts
300-
const len = alerts.length;
373+
const len = losses.length;
301374
const threshold = floatStr(failThreshold);
302-
const failures = alerts.filter((a) => a.ratio > failThreshold);
375+
const failures = losses.filter((a) => a.ratio > failThreshold);
303376
if (failures.length > 0) {
304377
core.debug('Mark this workflow as fail since one or more fatal alerts found');
305378
if (failThreshold !== alertThreshold) {

test/buildComment.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,11 @@ describe('buildComment', () => {
108108
# TestSuite
109109
110110
<details>
111+
112+
Previous: testCommitIdPrevious
113+
Current: testCommitIdCurrent
111114
112-
| Benchmark suite | Current: testCommitIdCurrent | Previous: testCommitIdPrevious | Ratio |
115+
| Benchmark suite | Current | Previous | Ratio |
113116
|-|-|-|-|
114117
| \`TestBench<1>\` | \`0\` testUnit | \`0\` testUnit | \`1\` |
115118
| \`TestBench<2>\` | \`1\` testUnit | \`0\` testUnit | \`+∞\` |
@@ -195,7 +198,10 @@ describe('buildComment', () => {
195198
196199
<details>
197200
198-
| Benchmark suite | Current: testCommitIdCurrent | Previous: testCommitIdPrevious | Ratio |
201+
Previous: testCommitIdPrevious
202+
Current: testCommitIdCurrent
203+
204+
| Benchmark suite | Current | Previous | Ratio |
199205
|-|-|-|-|
200206
| \`TestBench<1>\` | \`0\` testUnit | \`0\` testUnit | \`1\` |
201207
| \`TestBench<2>\` | \`0\` testUnit | \`1\` testUnit | \`+∞\` |

0 commit comments

Comments
 (0)