|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow strict-local |
| 8 | + * @format |
| 9 | + */ |
| 10 | + |
| 11 | +type TestTaskTiming = { |
| 12 | + name: string, |
| 13 | + latency: { |
| 14 | + mean: number, |
| 15 | + min: number, |
| 16 | + max: number, |
| 17 | + p50: number, |
| 18 | + p75: number, |
| 19 | + p99: number, |
| 20 | + }, |
| 21 | +}; |
| 22 | + |
| 23 | +export type BenchmarkTestArtifact = { |
| 24 | + type: string, |
| 25 | + timings: $ReadOnlyArray<TestTaskTiming>, |
| 26 | +}; |
| 27 | + |
| 28 | +export const printBenchmarkResultsRanking = ( |
| 29 | + testResults: Array<{ |
| 30 | + title: string, |
| 31 | + testArtifact: mixed, |
| 32 | + }>, |
| 33 | +) => { |
| 34 | + const numberOfTests = testResults.length; |
| 35 | + if (numberOfTests <= 1 || testResults[0].testArtifact === undefined) { |
| 36 | + // No test variations to compare between, or there are no benchmak results present, just don't print anything |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + const testTaskTimings: {[string]: Array<[string, number]>} = {}; |
| 41 | + for (let i = 0; i < numberOfTests; i++) { |
| 42 | + if (testResults[i] == null) { |
| 43 | + continue; |
| 44 | + } |
| 45 | + // $FlowExpectedError[incompatible-cast] |
| 46 | + const testArtifact = testResults[i].testArtifact as ?BenchmarkTestArtifact; |
| 47 | + const testVariationName = testResults[i].title; |
| 48 | + if ( |
| 49 | + testArtifact == null || |
| 50 | + testArtifact.type !== 'benchmark' || |
| 51 | + testVariationName == null |
| 52 | + ) { |
| 53 | + continue; |
| 54 | + } |
| 55 | + for (const taskTiming of testArtifact.timings) { |
| 56 | + const taskName = taskTiming.name; |
| 57 | + if (testTaskTimings[taskName] === undefined) { |
| 58 | + testTaskTimings[taskName] = []; |
| 59 | + } |
| 60 | + testTaskTimings[taskName].push([ |
| 61 | + testVariationName, |
| 62 | + taskTiming.latency.p50, |
| 63 | + ]); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + // Sort each task's executions by latency |
| 68 | + for (const taskName in testTaskTimings) { |
| 69 | + testTaskTimings[taskName].sort((a, b) => a[1] - b[1]); |
| 70 | + } |
| 71 | + |
| 72 | + // Print the rankings |
| 73 | + console.log('### Benchmark Results Ranking ###'); |
| 74 | + for (const taskName in testTaskTimings) { |
| 75 | + console.log(`> ${taskName}:`); |
| 76 | + let lastTiming; |
| 77 | + for (const [i, [testVariationName, latency]] of testTaskTimings[ |
| 78 | + taskName |
| 79 | + ].entries()) { |
| 80 | + console.log( |
| 81 | + ` ${i + 1}. ${testVariationName}: ${latency.toFixed(2)}ms ${getTimingDelta(lastTiming, latency)}`, |
| 82 | + ); |
| 83 | + lastTiming = latency; |
| 84 | + } |
| 85 | + console.log('\n'); |
| 86 | + } |
| 87 | +}; |
| 88 | + |
| 89 | +function getTimingDelta(lastTiming: ?number, currentTiming: ?number): string { |
| 90 | + if (lastTiming != null && currentTiming != null) { |
| 91 | + const deltaPercent = ((currentTiming - lastTiming) / lastTiming) * 100; |
| 92 | + return `(${deltaPercent.toFixed(2)}% ${deltaPercent > 0 ? 'slower' : 'faster'})`; |
| 93 | + } else { |
| 94 | + return ''; |
| 95 | + } |
| 96 | +} |
0 commit comments