-
-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathbenchmark_util.ts
More file actions
60 lines (54 loc) · 2.14 KB
/
benchmark_util.ts
File metadata and controls
60 lines (54 loc) · 2.14 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
import { BenchmarkResult } from "./benchmark_types";
import { calculatePercentageChange, toFixed } from "./util";
export const makeMarkdownTableRow = (cells: string[]) => `| ${cells.join(" | ")} |\n`;
export const makeBold = (input: string) => `**${input}**`;
export function compareNumericBenchmarks<T extends BenchmarkResult>(
newResults: T[],
oldResults: T[],
unit: string,
extractValue: (result: T) => number,
formatValue: (value: number) => string
): string {
let comparisonTable = makeMarkdownTableRow([
"name",
`master (${unit})`,
`commit (${unit})`,
`change (${unit})`,
"change (%)",
]);
comparisonTable += makeMarkdownTableRow(["---", "---", "---", "---", "---"]);
let oldValueSum = 0;
let newValueSum = 0;
newResults.forEach(newResult => {
const oldResult = oldResults.find(r => r.benchmarkName === newResult.benchmarkName);
const newValue = extractValue(newResult);
if (oldResult) {
const oldValue = extractValue(oldResult);
const percentageChange = calculatePercentageChange(oldValue, newValue);
const change = newValue - oldValue;
const row = [
newResult.benchmarkName,
formatValue(oldValue),
formatValue(newValue),
formatValue(change),
toFixed(percentageChange, 2),
];
comparisonTable += makeMarkdownTableRow(row);
oldValueSum += oldValue;
newValueSum += newValue;
} else {
// No master found => new benchmark
const row = [newResult.benchmarkName, formatValue(newValue), "/", "/", "/"];
comparisonTable += makeMarkdownTableRow(row);
}
});
const sumPercentageChange = calculatePercentageChange(oldValueSum, newValueSum);
comparisonTable += makeMarkdownTableRow([
makeBold("sum"),
makeBold(formatValue(oldValueSum)),
makeBold(formatValue(newValueSum)),
makeBold(formatValue(newValueSum - oldValueSum)),
makeBold(toFixed(sumPercentageChange, 2)),
]);
return comparisonTable;
}