Skip to content

Commit 9c6812e

Browse files
authored
feat: add min samples as param (#65)
* feat: add minSamples as param to each benchmark * test: add to minSamples param * docs: add minSamples in the README * feat: update comment of minSamples * chore: fix lint
1 parent b5e1e8b commit 9c6812e

6 files changed

Lines changed: 33 additions & 6 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ const suite = new Suite({ reporter: false });
116116
* `minTime` {number} Minimum duration for the benchmark to run. **Default:** `0.05` seconds.
117117
* `maxTime` {number} Maximum duration for the benchmark to run. **Default:** `0.5` seconds.
118118
* `repeatSuite` {number} Number of times to repeat benchmark to run. **Default:** `1` times.
119+
* `minSamples` {number} Number minimum of samples the each round. **Default:** `10` samples.
119120
* `fn` {Function|AsyncFunction} The benchmark function. Can be synchronous or asynchronous.
120121
* Returns: {Suite}
121122

examples/create-uint32array/node.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ suite
99
.add(`new Uint32Array(1024) with 10 repetitions`, {repeatSuite: 10}, function () {
1010
return new Uint32Array(1024);
1111
})
12+
.add(`new Uint32Array(1024) with min samples of 50`, {minSamples: 50}, function() {
13+
return new Uint32Array(1024);
14+
})
1215
.add(`[Managed] new Uint32Array(1024)`, function (timer) {
1316
const assert = require('node:assert');
1417

lib/index.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,16 @@ class Benchmark {
3939
maxTime;
4040
plugins;
4141
repeatSuite;
42+
minSamples;
4243

43-
constructor(name, fn, minTime, maxTime, plugins, repeatSuite) {
44+
constructor(name, fn, minTime, maxTime, plugins, repeatSuite, minSamples) {
4445
this.name = name;
4546
this.fn = fn;
4647
this.minTime = minTime;
4748
this.maxTime = maxTime;
4849
this.plugins = plugins;
4950
this.repeatSuite = repeatSuite;
51+
this.minSamples = minSamples;
5052

5153
this.hasArg = this.fn.length >= 1;
5254
if (this.fn.length > 1) {
@@ -75,6 +77,8 @@ const defaultBenchOptions = {
7577
maxTime: 0.5,
7678
// Number of times the benchmark will be repeated
7779
repeatSuite: 1,
80+
// Number minimum of samples the each round
81+
minSamples: 10,
7882
};
7983

8084
function throwIfNoNativesSyntax() {
@@ -133,6 +137,11 @@ class Suite {
133137
"options.repeatSuite",
134138
options.repeatSuite,
135139
);
140+
validateNumber(
141+
options.minSamples,
142+
"options.minSamples",
143+
options.minSamples,
144+
);
136145
}
137146
validateFunction(fn, "fn");
138147

@@ -143,6 +152,7 @@ class Suite {
143152
options.maxTime,
144153
this.#plugins,
145154
options.repeatSuite,
155+
options.minSamples,
146156
);
147157
this.#benchmarks.push(benchmark);
148158
return this;
@@ -174,7 +184,7 @@ class Suite {
174184
// Warmup is calculated to reduce noise/bias on the results
175185
const initialIterations = await getInitialIterations(benchmark);
176186
debugBench(
177-
`Starting ${benchmark.name} with minTime=${benchmark.minTime}, maxTime=${benchmark.maxTime}, repeatSuite=${benchmark.repeatSuite}`,
187+
`Starting ${benchmark.name} with minTime=${benchmark.minTime}, maxTime=${benchmark.maxTime}, repeatSuite=${benchmark.repeatSuite}, minSamples=${benchmark.minSamples}`,
178188
);
179189

180190
let result;
@@ -185,6 +195,7 @@ class Suite {
185195
benchmark,
186196
initialIterations,
187197
benchmark.repeatSuite,
198+
benchmark.minSamples,
188199
);
189200
}
190201
results[i] = result;
@@ -204,6 +215,7 @@ class Suite {
204215
benchmark,
205216
initialIterations,
206217
repeatSuite: benchmark.repeatSuite,
218+
minSamples: benchmark.minSamples,
207219
});
208220
return new Promise((resolve, reject) => {
209221
worker.on("message", (result) => {

lib/lifecycle.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,13 @@ async function runBenchmarkOnce(
114114
return { iterations, timeSpent };
115115
}
116116

117-
async function runBenchmark(bench, initialIterations, repeatSuite) {
117+
async function runBenchmark(bench, initialIterations, repeatSuite, minSamples) {
118118
const histogram = new StatisticalHistogram();
119119

120120
const maxDuration = bench.maxTime * timer.scale;
121-
const minSamples = 10;
122121

123122
let totalIterations = 0;
124123
let totalTimeSpent = 0;
125-
126124
for (let i = 0; i < repeatSuite; ++i) {
127125
const { iterations, timeSpent } = await runBenchmarkOnce(bench, histogram, {
128126
initialIterations,

test/basic.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,19 @@ describe("API Interface", () => {
160160
);
161161
}
162162
});
163+
164+
it("minSamples should be a valid number", () => {
165+
for (const r of ["ds", {}, () => {}]) {
166+
assert.throws(
167+
() => {
168+
bench.add("name", { minSamples: r }, noop);
169+
},
170+
{
171+
code: "ERR_INVALID_ARG_TYPE",
172+
},
173+
);
174+
}
175+
});
163176
});
164177
});
165178

test/plugin-api-doc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ describe("plugin API", async () => {
6767
"getReport(string)",
6868
"getResult(string)",
6969
"isSupported()",
70-
"onCompleteBenchmark([number, number, object], {fn, fnStr, hasArg, isAsync, maxTime, minTime, name, plugins, repeatSuite})",
70+
"onCompleteBenchmark([number, number, object], {fn, fnStr, hasArg, isAsync, maxTime, minSamples, minTime, name, plugins, repeatSuite})",
7171
"toJSON(string)",
7272
"toString()",
7373
]);

0 commit comments

Comments
 (0)