Skip to content

Commit 3e7f1f0

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

File tree

3 files changed

+203
-58
lines changed

3 files changed

+203
-58
lines changed

src/write.ts

Lines changed: 83 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,18 @@ 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 || ratio > threshold) {
111112
core.warning(
112113
`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}`,
114+
` It is ${ratio}x worse than previous, exceeding ratio threshold ${threshold}`,
114115
);
115-
alerts.push({ current, prev, ratio });
116+
losses.push({ current, prev, ratio });
117+
} else if (ratio < 1 / threshold) {
118+
gains.push({ current, prev, ratio });
116119
}
117120
}
118121

119-
return alerts;
122+
return [losses, gains];
120123
}
121124

122125
function getCurrentRepoMetadata() {
@@ -175,7 +178,10 @@ export function buildComment(
175178
'',
176179
expandableDetails ? '<details>' : '',
177180
'',
178-
`| Benchmark suite | Current: ${curSuite.commit.id} | Previous: ${prevSuite.commit.id} | Ratio |`,
181+
`Previous: ${prevSuite.commit.id}`,
182+
`Current: ${curSuite.commit.id}`,
183+
'',
184+
`| Benchmark suite | Current | Previous | Ratio |`,
179185
'|-|-|-|-|',
180186
];
181187

@@ -200,32 +206,77 @@ export function buildComment(
200206
return lines.join('\n');
201207
}
202208

209+
function pushResultLines(results: Alert[], output: string[]) {
210+
results.sort((a, b) => a.ratio - b.ratio);
211+
for (const alert of results) {
212+
const { current, prev, ratio } = alert;
213+
const line = `| \`${current.name}\` | ${strVal(current)} | ${strVal(prev)} | \`${floatStr(ratio)}\` |`;
214+
output.push(line);
215+
}
216+
}
217+
218+
const RESULT_TABLE_HEADER = ['', `| Benchmark suite | Current | Previous | Ratio |`, '|-|-|-|-|'];
219+
220+
function buildReportComment(
221+
results: Alert[],
222+
benchName: string,
223+
curSuite: Benchmark,
224+
prevSuite: Benchmark,
225+
cc: string[],
226+
): string {
227+
// Do not show benchmark name if it is the default value 'Benchmark'.
228+
const benchmarkText = benchName === 'Benchmark' ? '' : ` **'${benchName}'**`;
229+
const lines = [
230+
'# Performance Report',
231+
'',
232+
`For benchmark${benchmarkText}.`,
233+
'',
234+
`Previous commit: ${prevSuite.commit.id}`,
235+
`Current commit: ${curSuite.commit.id}`,
236+
];
237+
238+
lines.push(...RESULT_TABLE_HEADER);
239+
pushResultLines(results, lines);
240+
lines.push('', commentFooter());
241+
242+
if (cc.length > 0) {
243+
lines.push('', `CC: ${cc.join(' ')}`);
244+
}
245+
246+
return lines.join('\n');
247+
}
248+
203249
function buildAlertComment(
204-
alerts: Alert[],
250+
losses: Alert[],
251+
gains: Alert[],
205252
benchName: string,
206253
curSuite: Benchmark,
207254
prevSuite: Benchmark,
208255
threshold: number,
209256
cc: string[],
210257
): string {
211258
// 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:';
259+
const benchmarkText = benchName === 'Benchmark' ? '' : ` for **'${benchName}'**`;
214260
const thresholdString = floatStr(threshold);
215261
const lines = [
216-
title,
262+
`# Performance Report${benchmarkText}`,
217263
'',
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}\`.`,
264+
`Benchmark result(s) exceed ratio of \`${thresholdString}\`.`,
220265
'',
221-
`| Benchmark suite | Current: ${curSuite.commit.id} | Previous: ${prevSuite.commit.id} | Ratio |`,
222-
'|-|-|-|-|',
266+
`Previous commit: ${prevSuite.commit.id}`,
267+
`Current commit: ${curSuite.commit.id}`,
223268
];
224269

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);
270+
if (losses.length > 0) {
271+
lines.push(...['', '### :snail: The following benchmarks show regressions:']);
272+
lines.push(...RESULT_TABLE_HEADER);
273+
pushResultLines(losses, lines);
274+
}
275+
276+
if (gains.length > 0) {
277+
lines.push(...['', '### :rocket: The following benchmarks show improvements:']);
278+
lines.push(...RESULT_TABLE_HEADER);
279+
pushResultLines(gains, lines);
229280
}
230281

231282
// Footer
@@ -276,14 +327,22 @@ async function handleAlert(benchName: string, curSuite: Benchmark, prevSuite: Be
276327
return;
277328
}
278329

279-
const alerts = findAlerts(curSuite, prevSuite, alertThreshold);
330+
const [losses, gains] = findAlerts(curSuite, prevSuite, alertThreshold);
331+
const alerts = [...losses, ...gains];
280332
if (alerts.length === 0) {
281333
core.debug('No performance alert found happily');
282334
return;
283335
}
284336

285-
core.debug(`Found ${alerts.length} alerts`);
286-
const body = buildAlertComment(alerts, benchName, curSuite, prevSuite, alertThreshold, alertCommentCcUsers);
337+
let body = '';
338+
if (alertThreshold === 0) {
339+
core.debug(`Alert threshold is 0. Leaving report with ${alerts.length} alerts`);
340+
body = buildReportComment(alerts, benchName, curSuite, prevSuite, alertCommentCcUsers);
341+
} else {
342+
core.debug(`Found ${alerts.length} alerts`);
343+
body = buildAlertComment(losses, gains, benchName, curSuite, prevSuite, alertThreshold, alertCommentCcUsers);
344+
}
345+
287346
let message = body;
288347

289348
if (commentOnAlert) {
@@ -297,9 +356,9 @@ async function handleAlert(benchName: string, curSuite: Benchmark, prevSuite: Be
297356

298357
if (failOnAlert) {
299358
// Note: alertThreshold is smaller than failThreshold. It was checked in config.ts
300-
const len = alerts.length;
359+
const len = losses.length;
301360
const threshold = floatStr(failThreshold);
302-
const failures = alerts.filter((a) => a.ratio > failThreshold);
361+
const failures = losses.filter((a) => a.ratio > failThreshold);
303362
if (failures.length > 0) {
304363
core.debug('Mark this workflow as fail since one or more fatal alerts found');
305364
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)