Skip to content

Commit 38d10d1

Browse files
authored
feat: Add fixed-unit time format to JSON min/max output for use in (#157)
code. minFormatted and maxFormatted add a bit of unnecessary overhead for a consumer to do any logic or tracking of the min/max range (eg, for telemetry).
1 parent 946bbdc commit 38d10d1

File tree

2 files changed

+71
-21
lines changed

2 files changed

+71
-21
lines changed

lib/reporter/json.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ function toJSON(results) {
1919
name: result.name,
2020
runsSampled: result.runsSampled,
2121
min: result.minFormatted,
22+
minNS: result.min,
2223
max: result.maxFormatted,
24+
maxNS: result.max,
2325
plugins: result.plugins.map((p) => p.report).filter(Boolean),
2426
opsSec: result.opsSec,
2527
totalTime: result.totalTime,

test/reporter.js

Lines changed: 69 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -678,19 +678,11 @@ describe("custom reporter should have access to histogram data", async () => {
678678
});
679679
});
680680

681-
describe("jsonReport should produce valid JSON output", async () => {
682-
let output = "";
681+
describe("toJSON", () => {
682+
let results;
683683

684684
before(async () => {
685-
const originalStdoutWrite = process.stdout.write;
686-
process.stdout.write = (data) => {
687-
output += data;
688-
};
689-
690-
// Create a new Suite with the JSON reporter
691-
const suite = new Suite({
692-
reporter: jsonReport,
693-
});
685+
const suite = new Suite({});
694686

695687
suite
696688
.add("single with matcher", () => {
@@ -710,25 +702,22 @@ describe("jsonReport should produce valid JSON output", async () => {
710702
});
711703

712704
// Run the suite
713-
await suite.run();
705+
results = await suite.run();
706+
});
714707

715-
// Restore stdout
716-
process.stdout.write = originalStdoutWrite;
708+
it("should run", () => {
709+
toJSON(results);
717710
});
718711

719712
it("should print valid JSON", () => {
720-
// Verify if the output can be parsed as JSON
721-
let data;
722-
try {
723-
data = JSON.parse(output);
724-
} catch (err) {
725-
assert.fail(`Output is not valid JSON: ${err.message}`);
726-
}
713+
const output = toJSON(results);
714+
const data = JSON.parse(output);
727715

728716
assert.ok(Array.isArray(data), "Output should be an array of results");
729717
});
730718

731719
it("should contain the required benchmark fields", () => {
720+
const output = toJSON(results);
732721
const data = JSON.parse(output);
733722

734723
// We expect the two benchmarks we added: 'single with matcher' and 'Multiple replaces'
@@ -746,15 +735,74 @@ describe("jsonReport should produce valid JSON output", async () => {
746735
typeof entry.min === "string",
747736
"min should be a string (formatted time)",
748737
);
738+
assert.ok(
739+
typeof entry.minNS === "number",
740+
"minNS should be a number (time in NS)",
741+
);
749742
assert.ok(
750743
typeof entry.max === "string",
751744
"max should be a string (formatted time)",
752745
);
746+
assert.ok(
747+
typeof entry.maxNS === "number",
748+
"maxNS should be a number (time in NS)",
749+
);
753750
assert.ok(Array.isArray(entry.plugins), "plugins should be an array");
754751
}
755752
});
756753
});
757754

755+
describe("jsonReport", () => {
756+
let output = "";
757+
758+
before(async () => {
759+
const originalStdoutWrite = process.stdout.write;
760+
process.stdout.write = (data) => {
761+
output += data;
762+
};
763+
764+
// Create a new Suite with the JSON reporter
765+
const suite = new Suite({
766+
reporter: jsonReport,
767+
});
768+
769+
suite
770+
.add("single with matcher", () => {
771+
const pattern = /[123]/g;
772+
const replacements = { 1: "a", 2: "b", 3: "c" };
773+
const subject = "123123123123123123123123123123123123123123123123";
774+
const r = subject.replace(pattern, (m) => replacements[m]);
775+
assert.ok(r);
776+
})
777+
.add("Multiple replaces", () => {
778+
const subject = "123123123123123123123123123123123123123123123123";
779+
const r = subject
780+
.replace(/1/g, "a")
781+
.replace(/2/g, "b")
782+
.replace(/3/g, "c");
783+
assert.ok(r);
784+
});
785+
786+
// Run the suite
787+
await suite.run();
788+
789+
// Restore stdout
790+
process.stdout.write = originalStdoutWrite;
791+
});
792+
793+
it("should print valid JSON", () => {
794+
// Verify if the output can be parsed as JSON
795+
let data;
796+
try {
797+
data = JSON.parse(output);
798+
} catch (err) {
799+
assert.fail(`Output is not valid JSON: ${err.message}`);
800+
}
801+
802+
assert.ok(Array.isArray(data), "Output should be an array of results");
803+
});
804+
});
805+
758806
describe("toCSV", () => {
759807
it("should generate valid CSV output", async (t) => {
760808
const result = toCSV([

0 commit comments

Comments
 (0)