Skip to content

Commit 34ad9e9

Browse files
feat: add opsSecPerRun result (#74)
* feat: add opsSecPerRun array This will be useful when comparing runs (statisticallanalysis) * fixup! feat: add opsSecPerRun array --------- Co-authored-by: RafaelGSS <rafael.nunu@hotmail.com>
1 parent 81bc38f commit 34ad9e9

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ A `Suite` manages and executes benchmark functions. It provides two methods: `ad
9595
* `results` {Object[]} Array of benchmark results:
9696
* `name` {string} Benchmark name.
9797
* `opsSec` {string} Operations per second.
98+
* `opsSecPerRun` {Array} Array of operations per second (useful when repeatSuite > 1).
9899
* `iterations` {Number} Number of iterations.
99100
* `histogram` {Histogram} Histogram instance.
100101
* `benchmarkMode` {string} Benchmark mode to use. Can be 'ops' or 'time'. **Default:** `'ops'`.

lib/lifecycle.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ async function runBenchmark(
164164

165165
let totalIterations = 0;
166166
let totalTimeSpent = 0;
167+
const opsSecPerRun = [];
168+
167169
for (let i = 0; i < repeatSuite; ++i) {
168170
const { iterations, timeSpent } = await runBenchmarkOnce(
169171
bench,
@@ -175,6 +177,10 @@ async function runBenchmark(
175177
},
176178
benchmarkMode,
177179
);
180+
181+
const runOpsSec = iterations / (timeSpent / timer.scale);
182+
opsSecPerRun.push(runOpsSec);
183+
178184
totalTimeSpent += timeSpent;
179185
totalIterations += iterations;
180186
}
@@ -206,6 +212,7 @@ async function runBenchmark(
206212
);
207213
} else {
208214
result.opsSec = opsSec;
215+
result.opsSecPerRun = opsSecPerRun;
209216
debugBench(
210217
`${bench.name} completed ${sampleData.length} samples with ${opsSec.toFixed(2)} ops/sec`,
211218
);

test/basic.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,48 @@ describe("simple usage", async () => {
204204
assert.ok(bench1.iterations >= 10);
205205
assert.ok(bench2.iterations >= 10);
206206
});
207+
208+
it("opsSecPerRun should exist when repeatSuite is 1", async () => {
209+
assert.ok(
210+
Array.isArray(bench1.opsSecPerRun),
211+
"opsSecPerRun should be an array",
212+
);
213+
assert.ok(
214+
Array.isArray(bench2.opsSecPerRun),
215+
"opsSecPerRun should be an array",
216+
);
217+
});
218+
});
219+
220+
describe("repeat suite", async () => {
221+
const repeatCount = 3;
222+
const bench = new Suite({ reporter: noop });
223+
bench.add("Repeat ops test", { repeatSuite: repeatCount }, () => {
224+
// Simple operation
225+
const x = 1 + 1;
226+
});
227+
228+
const results = await bench.run();
229+
230+
it("should include opsSecPerRun when repeatSuite is greater than 1", async () => {
231+
assert.strictEqual(results.length, 1);
232+
assert.ok(results[0].opsSec !== undefined, "opsSec should be defined");
233+
assert.ok(
234+
Array.isArray(results[0].opsSecPerRun),
235+
"opsSecPerRun should be an array",
236+
);
237+
assert.strictEqual(
238+
results[0].opsSecPerRun.length,
239+
repeatCount,
240+
`opsSecPerRun should have ${repeatCount} elements`,
241+
);
242+
for (const opsSec of results[0].opsSecPerRun) {
243+
assert.ok(
244+
typeof opsSec === "number" && !Number.isNaN(opsSec),
245+
"each element should be a valid number",
246+
);
247+
}
248+
});
207249
});
208250

209251
describe("throws when a benchmark task throw", async () => {

0 commit comments

Comments
 (0)