(
newResults: T[],
oldResults: T[],
unit: string,
extractValue: (result: T) => number,
formatValue: (value: number) => string
)
| 5 | export const makeBold = (input: string) => `**${input}**`; |
| 6 | |
| 7 | export function compareNumericBenchmarks<T extends BenchmarkResult>( |
| 8 | newResults: T[], |
| 9 | oldResults: T[], |
| 10 | unit: string, |
| 11 | extractValue: (result: T) => number, |
| 12 | formatValue: (value: number) => string |
| 13 | ): string { |
| 14 | let comparisonTable = makeMarkdownTableRow([ |
| 15 | "name", |
| 16 | `master (${unit})`, |
| 17 | `commit (${unit})`, |
| 18 | `change (${unit})`, |
| 19 | "change (%)", |
| 20 | ]); |
| 21 | comparisonTable += makeMarkdownTableRow(["---", "---", "---", "---", "---"]); |
| 22 | |
| 23 | let oldValueSum = 0; |
| 24 | let newValueSum = 0; |
| 25 | |
| 26 | newResults.forEach(newResult => { |
| 27 | const oldResult = oldResults.find(r => r.benchmarkName === newResult.benchmarkName); |
| 28 | const newValue = extractValue(newResult); |
| 29 | if (oldResult) { |
| 30 | const oldValue = extractValue(oldResult); |
| 31 | const percentageChange = calculatePercentageChange(oldValue, newValue); |
| 32 | const change = newValue - oldValue; |
| 33 | const row = [ |
| 34 | newResult.benchmarkName, |
| 35 | formatValue(oldValue), |
| 36 | formatValue(newValue), |
| 37 | formatValue(change), |
| 38 | toFixed(percentageChange, 2), |
| 39 | ]; |
| 40 | comparisonTable += makeMarkdownTableRow(row); |
| 41 | oldValueSum += oldValue; |
| 42 | newValueSum += newValue; |
| 43 | } else { |
| 44 | // No master found => new benchmark |
| 45 | const row = [newResult.benchmarkName, formatValue(newValue), "/", "/", "/"]; |
| 46 | comparisonTable += makeMarkdownTableRow(row); |
| 47 | } |
| 48 | }); |
| 49 | |
| 50 | const sumPercentageChange = calculatePercentageChange(oldValueSum, newValueSum); |
| 51 | comparisonTable += makeMarkdownTableRow([ |
| 52 | makeBold("sum"), |
| 53 | makeBold(formatValue(oldValueSum)), |
| 54 | makeBold(formatValue(newValueSum)), |
| 55 | makeBold(formatValue(newValueSum - oldValueSum)), |
| 56 | makeBold(toFixed(sumPercentageChange, 2)), |
| 57 | ]); |
| 58 | |
| 59 | return comparisonTable; |
| 60 | } |
no test coverage detected