Skip to content

Commit 9ad4c94

Browse files
authored
feat: add fastest/slowest value for feature parity with benchmark.js
* chore: Rename analysis file from baseline.js -> analyze.js Implements #101 * chore: Formatting fixups.
1 parent a85e15c commit 9ad4c94

File tree

5 files changed

+94
-13
lines changed

5 files changed

+94
-13
lines changed

lib/reporter/chart.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { platform, arch, availableParallelism, totalmem } = require("node:os");
22
const { styleText } = require("../utils/styleText");
3-
const { analyze } = require("../utils/baseline.js");
3+
const { analyze } = require("../utils/analyze.js");
44

55
/**
66
* Draws a bar chart representation of a benchmark result

lib/reporter/pretty.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const { styleText } = require("../utils/styleText");
22
const { timer } = require("../clock");
33
const os = require("node:os");
4-
const { analyze } = require("../utils/baseline");
4+
const { analyze } = require("../utils/analyze.js");
55

66
const formatter = Intl.NumberFormat(undefined, {
77
notation: "standard",

lib/reporter/text.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { styleText } = require("../utils/styleText");
22
const { timer } = require("../clock");
3-
const { analyze } = require("../utils/baseline.js");
3+
const { analyze } = require("../utils/analyze.js");
44

55
const formatter = Intl.NumberFormat(undefined, {
66
notation: "standard",
Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,31 @@ function analyze(results, sorted = true) {
77
return output;
88
}
99

10-
if (sorted) {
11-
output.sort((a, b) => {
12-
if (a.baseline) return -1;
13-
if (b.baseline) return 1;
14-
return (a.opsSec || 0) - (b.opsSec || 0);
15-
});
16-
}
17-
1810
const baselineHz = baselineResult.opsSec;
11+
let min = Number.MAX_SAFE_INTEGER;
12+
let max = Number.MIN_SAFE_INTEGER;
13+
14+
let fastest;
15+
let slowest;
1916

2017
for (const result of output) {
21-
if (!result.baseline && result.opsSec !== undefined) {
22-
const benchmarkHz = result.opsSec;
18+
const benchmarkHz = result.opsSec;
19+
20+
if (benchmarkHz === undefined) {
21+
continue;
22+
}
2323

24+
if (benchmarkHz > max) {
25+
max = benchmarkHz;
26+
fastest = result;
27+
}
28+
29+
if (benchmarkHz < min) {
30+
min = benchmarkHz;
31+
slowest = result;
32+
}
33+
34+
if (!result.baseline) {
2435
if (benchmarkHz > baselineHz) {
2536
result.comparison = (benchmarkHz / baselineHz).toFixed(2);
2637
} else {
@@ -29,6 +40,22 @@ function analyze(results, sorted = true) {
2940
}
3041
}
3142

43+
if (fastest !== undefined) {
44+
fastest.fastest = true;
45+
}
46+
47+
if (slowest !== undefined) {
48+
slowest.slowest = true;
49+
}
50+
51+
if (sorted) {
52+
output.sort((a, b) => {
53+
if (a.baseline) return -1;
54+
if (b.baseline) return 1;
55+
return (a.opsSec || 0) - (b.opsSec || 0);
56+
});
57+
}
58+
3259
return output;
3360
}
3461

test/reporter.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const {
1212
textReport,
1313
} = require("../lib");
1414

15+
const { analyze } = require("../lib/utils/analyze.js");
16+
1517
describe("chartReport outputs benchmark results as a bar chart", async (t) => {
1618
let output = "";
1719

@@ -285,6 +287,42 @@ describe("prettyReport outputs a beautiful report", async (t) => {
285287
});
286288
});
287289

290+
describe("analyse", async (t) => {
291+
let analysis;
292+
293+
before(async () => {
294+
// Create a new Suite with the pretty reporter
295+
const suite = new Suite({});
296+
297+
// Add benchmarks with one being the baseline
298+
suite
299+
.add("baseline-test", { baseline: true }, () => {
300+
// Medium-speed operation
301+
for (let i = 0; i < 10000; i++) {}
302+
})
303+
.add("other-test", () => {
304+
// Faster operation
305+
for (let i = 0; i < 1000; i++) {}
306+
})
307+
.add("faster-test", () => {
308+
// Slower operation
309+
for (let i = 0; i < 100; i++) {}
310+
});
311+
312+
// Run the suite
313+
const results = await suite.run();
314+
analysis = analyze(results, false);
315+
});
316+
317+
it("annotates the fastest result", () => {
318+
assert.equal(analysis[2].fastest, true, "fastest result");
319+
});
320+
321+
it("annotates the slowest result", () => {
322+
assert.equal(analysis[0].slowest, true, "slowest result");
323+
});
324+
});
325+
288326
describe("baseline comparisons", async (t) => {
289327
let results;
290328

@@ -311,6 +349,22 @@ describe("baseline comparisons", async (t) => {
311349
results = await suite.run();
312350
});
313351

352+
describe("analyze", () => {
353+
let analysis;
354+
355+
before(() => {
356+
analysis = analyze(results);
357+
});
358+
359+
it("annotates the fastest result", () => {
360+
assert.equal(results[1].fastest, true, "fastest result");
361+
});
362+
363+
it("annotates the slowest result", () => {
364+
assert.equal(results[2].slowest, true, "slowest result");
365+
});
366+
});
367+
314368
describe("for prettyReport", async (t) => {
315369
let output = "";
316370

0 commit comments

Comments
 (0)