Skip to content

Commit 12850ba

Browse files
committed
add opsSecPerRun to have each of the operations per second when repeatSuite is grater than 1
1 parent 81bc38f commit 12850ba

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

lib/lifecycle.js

Lines changed: 12 additions & 1 deletion
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 = repeatSuite > 1 ? [] : null;
168+
167169
for (let i = 0; i < repeatSuite; ++i) {
168170
const { iterations, timeSpent } = await runBenchmarkOnce(
169171
bench,
@@ -175,6 +177,12 @@ async function runBenchmark(
175177
},
176178
benchmarkMode,
177179
);
180+
181+
if (opsSecPerRun) {
182+
const runOpsSec = iterations / (timeSpent / timer.scale);
183+
opsSecPerRun.push(runOpsSec);
184+
}
185+
178186
totalTimeSpent += timeSpent;
179187
totalIterations += iterations;
180188
}
@@ -206,8 +214,11 @@ async function runBenchmark(
206214
);
207215
} else {
208216
result.opsSec = opsSec;
217+
if (opsSecPerRun) {
218+
result.opsSecPerRun = opsSecPerRun;
219+
}
209220
debugBench(
210-
`${bench.name} completed ${sampleData.length} samples with ${opsSec.toFixed(2)} ops/sec`,
221+
`${bench.name} completed ${sampleData.length} samples with ${opsSecPerRun ? opsSecPerRun.map((op) => op.toFixed(2)).join(", ") : opsSec.toFixed(2)} ops/sec`,
211222
);
212223
}
213224

test/time-mode.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,54 @@ describe("Time-based Benchmarking", () => {
146146
code: "ERR_INVALID_ARG_TYPE",
147147
});
148148
});
149+
150+
it("should include opsSecPerRun when repeatSuite is greater than 1", async () => {
151+
const suite = new Suite({ reporter: false });
152+
const repeatCount = 3;
153+
154+
// A very fast operation that should be consistent
155+
suite.add("Repeat ops test", { repeatSuite: repeatCount }, () => {
156+
// Simple operation
157+
const x = 1 + 1;
158+
});
159+
160+
const results = await suite.run();
161+
162+
assert.strictEqual(results.length, 1);
163+
assert.ok(results[0].opsSec !== undefined, "opsSec should be defined");
164+
assert.ok(
165+
Array.isArray(results[0].opsSecPerRun),
166+
"opsSecPerRun should be an array",
167+
);
168+
assert.strictEqual(
169+
results[0].opsSecPerRun.length,
170+
repeatCount,
171+
`opsSecPerRun should have ${repeatCount} elements`,
172+
);
173+
for (const opsSec of results[0].opsSecPerRun) {
174+
assert.ok(
175+
typeof opsSec === "number" && !Number.isNaN(opsSec),
176+
"each element should be a valid number",
177+
);
178+
}
179+
});
180+
181+
it("should not include opsSecPerRun when repeatSuite is 1", async () => {
182+
const suite = new Suite({ reporter: false });
183+
184+
// A very fast operation that should be consistent
185+
suite.add("Single ops test", { repeatSuite: 1 }, () => {
186+
// Simple operation
187+
const x = 1 + 1;
188+
});
189+
190+
const results = await suite.run();
191+
192+
assert.strictEqual(results.length, 1);
193+
assert.ok(results[0].opsSec !== undefined, "opsSec should be defined");
194+
assert.ok(
195+
results[0].opsSecPerRun === undefined,
196+
"opsSecPerRun should not be defined",
197+
);
198+
});
149199
});

0 commit comments

Comments
 (0)