Open History to see the last commit
1const critical95 = [
2 12.7062, 4.3027, 3.1824, 2.7764, 2.5706, 2.4469, 2.3646, 2.306, 2.2622,
3 2.2281,
4]
5β
6export function mean(values: ReadonlyArray<number>) {
7 if (!values.length || values.some((value) => !Number.isFinite(value))) {
8 throw new Error('Expected finite, nonempty samples')
9 }
10 return values.reduce((sum, value) => sum + value, 0) / values.length
11}
12β
13export function summarizeRatios(ratios: ReadonlyArray<number>) {
14 if (
15 ratios.length < 2 ||
16 ratios.length > 11 ||
17 ratios.some((value) => value <= 0)
18 ) {
19 throw new Error('Expected 2-11 independent positive process-replica ratios')
20 }
21 const logs = ratios.map(Math.log)
22 const center = mean(logs)
23 const variance =
24 logs.reduce((sum, value) => sum + (value - center) ** 2, 0) /
25 (logs.length - 1)
26 // Replicas, not the many correlated batches inside a worker, are the samples.
27 const margin =
28 critical95[logs.length - 2]! * Math.sqrt(variance / logs.length)
29 const percent = (value: number) => (Math.exp(value) - 1) * 100
30 return {
31 changePercent: percent(center),
32 low95: percent(center - margin),
33 high95: percent(center + margin),
34 replicas: ratios.length,
35 }
36}
37β
38export function classify(
39 cpu: ReturnType<typeof summarizeRatios>,
40 wall: ReturnType<typeof summarizeRatios>,
41) {
42 if (cpu.low95 > 0 && wall.low95 > 0) {
43 return 'slower'
44 }
45 if (cpu.high95 < 0 && wall.high95 < 0) {
46 return 'faster'
47 }
48 if (
49 cpu.low95 >= -2 &&
50 cpu.high95 <= 2 &&
51 wall.low95 >= -2 &&
52 wall.high95 <= 2
53 ) {
54 return 'within-2%'
55 }
56 return 'inconclusive'
57}