-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathanalyze.js
More file actions
147 lines (125 loc) · 3.49 KB
/
analyze.js
File metadata and controls
147 lines (125 loc) · 3.49 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const { timer } = require("../clock");
const { compareBenchmarks } = require("./ttest");
function analyze(results, sorted = true, options = {}) {
const { ttest = false, alpha = 0.05 } = options;
const baselineResult = results.find((result) => result.baseline);
const output = [...results];
if (baselineResult?.opsSec === undefined) {
return output;
}
const baselineHz = baselineResult.opsSec;
const baselineSamples = baselineResult.histogram?.sampleData;
let min = Number.MAX_SAFE_INTEGER;
let max = Number.MIN_SAFE_INTEGER;
let fastest;
let slowest;
for (const result of output) {
const benchmarkHz = result.opsSec;
if (benchmarkHz === undefined) {
continue;
}
if (benchmarkHz > max) {
max = benchmarkHz;
fastest = result;
}
if (benchmarkHz < min) {
min = benchmarkHz;
slowest = result;
}
if (!result.baseline) {
if (benchmarkHz > baselineHz) {
result.comparison = (benchmarkHz / baselineHz).toFixed(2);
} else {
result.comparison = `-${(baselineHz / benchmarkHz).toFixed(2)}`;
}
if (ttest) {
const resultSamples = result.histogram?.sampleData ?? [];
const baselineSamplesForTest =
baselineResult.histogram?.sampleData ?? [];
if (
baselineSamplesForTest?.length >= 30 &&
resultSamples?.length >= 30
) {
const ttestResult = compareBenchmarks(
resultSamples,
baselineSamplesForTest,
alpha,
);
result.significanceTest = {
significant: ttestResult.significant,
pValue: ttestResult.pValue,
confidence: ttestResult.confidence,
stars: ttestResult.stars,
};
} else {
result.significanceTest = {
significant: false
}
}
}
}
}
if (fastest !== undefined) {
fastest.fastest = true;
}
if (slowest !== undefined) {
slowest.slowest = true;
}
if (sorted) {
output.sort((a, b) => {
if (a.baseline) return -1;
if (b.baseline) return 1;
return (a.opsSec || 0) - (b.opsSec || 0);
});
}
return output;
}
const formatter = Intl.NumberFormat(undefined, {
notation: "standard",
maximumFractionDigits: 2,
});
// Helper function to format time in appropriate units
const formatTime = (time) => {
if (time < 0.000001) {
// Less than 1 microsecond, show in nanoseconds
return `${(time * 1000000000).toFixed(2)} ns`;
} else if (time < 0.001) {
// Less than 1 millisecond, show in microseconds
return `${(time * 1000000).toFixed(2)} µs`;
} else if (time < 1) {
// Less than 1 second, show in milliseconds
return `${(time * 1000).toFixed(2)} ms`;
} else {
// 1 second or more, show in seconds
return `${time.toFixed(2)} s`;
}
};
function summarize(results) {
return results.map((result) => {
const baseResult = {
name: result.name,
runsSampled: result.histogram.samples,
min: result.histogram.min,
max: result.histogram.max,
minFormatted: timer.format(result.histogram.min), // Use timer for ns format
maxFormatted: timer.format(result.histogram.max),
// Report anything the plugins returned
plugins: result.plugins.map((p) => p.report).filter(Boolean),
};
if (result.opsSec !== undefined) {
const opsSecReported =
result.opsSec < 100
? result.opsSec.toFixed(2)
: result.opsSec.toFixed(0);
baseResult.opsSec = Number(opsSecReported);
} else if (result.totalTime !== undefined) {
baseResult.totalTime = result.totalTime; // Total time in seconds
baseResult.totalTimeFormatted = formatTime(result.totalTime);
}
return baseResult;
});
}
module.exports = {
analyze,
summarize,
};