Skip to content

Commit 28e872e

Browse files
authored
refactor: Clarify the purpose and units of various numbers in lifecycle.js (#151)
1 parent d678f9d commit 28e872e

2 files changed

Lines changed: 18 additions & 24 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ const suite = new Suite({ reporter: false });
158158

159159
* `name` {string} The name of the benchmark, displayed when reporting results.
160160
* `options` {Object} Configuration options for the benchmark. Supported properties:
161-
* `minTime` {number} Minimum duration for the benchmark to run. **Default:** `0.05` seconds.
161+
* `minTime` {number} The minimum duration of each sampling interval. **Default:** `0.05` seconds.
162162
* `maxTime` {number} Maximum duration for the benchmark to run. **Default:** `0.5` seconds.
163163
* `repeatSuite` {number} Number of times to repeat benchmark to run. **Default:** `1` times.
164164
* `minSamples` {number} Number minimum of samples the each round. **Default:** `10` samples.

lib/lifecycle.js

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@ const {
77
const { StatisticalHistogram } = require("./histogram");
88

99
/**
10-
* @param {number} durationPerOp The amount of time each operation takes
11-
* @param {number} targetTime The amount of time we want the benchmark to execute
10+
* @param {number} durationPerOp The amount of time each operation takes, in timer.scale
11+
* @param {number} targetTime The amount of time we want the benchmark to execute, in seconds
12+
* @return {number} - a suggested iteration count >= 1
1213
*/
1314
function getItersForOpDuration(durationPerOp, targetTime) {
14-
const totalOpsForMinTime = targetTime / (durationPerOp / timer.scale);
15+
const secondsPerOp = durationPerOp / timer.scale;
16+
const opsForTargetTime = Math.round(targetTime / secondsPerOp);
1517

16-
return Math.min(
17-
Number.MAX_SAFE_INTEGER,
18-
Math.max(1, Math.round(totalOpsForMinTime)),
19-
);
18+
return Math.min(Number.MAX_SAFE_INTEGER, Math.max(1, opsForTargetTime));
2019
}
2120

2221
function parsePluginsResult(plugins, name) {
@@ -46,7 +45,7 @@ async function getInitialIterations(bench) {
4645
);
4746

4847
// TODO: is this a correct assumpion?
49-
if (durationPerOp / timer.scale >= bench.maxTime)
48+
if (durationPerOp > bench.maxTime * timer.scale)
5049
process.emitWarning(
5150
`The benchmark "${bench.name}" has a duration per operation greater than the maxTime.`,
5251
);
@@ -59,8 +58,8 @@ async function getInitialIterations(bench) {
5958
* @param {import('./index').Benchmark} bench - The benchmark object to be executed
6059
* @param {number} initialIterations - The initial number of iterations to run
6160
* @param {Object} options - Warmup options
62-
* @param {number} [options.minTime] - Minimum time for warmup, defaults to bench.minTime
63-
* @param {number} [options.maxTime] - Maximum time for warmup, defaults to bench.minTime
61+
* @param {number} [options.minTime] - Minimum time for warmup, in seconds. Defaults to bench.minTime
62+
* @param {number} [options.maxTime] - Maximum time for warmup, in seconds. Defaults to bench.minTime
6463
* @returns {Promise<void>}
6564
*/
6665
async function runWarmup(bench, initialIterations, { minTime, maxTime }) {
@@ -86,12 +85,10 @@ async function runWarmup(bench, initialIterations, { minTime, maxTime }) {
8685

8786
// Just to avoid issues with empty fn
8887
const durationPerOp = Math.max(MIN_RESOLUTION, duration / realIterations);
88+
const remainingTime = Math.max(0, (maxDuration - timeSpent) / timer.scale);
89+
const targetTime = Math.min(remainingTime, minTime);
8990

90-
const minWindowTime = Math.max(
91-
0,
92-
Math.min((maxDuration - timeSpent) / timer.scale, minTime),
93-
);
94-
initialIterations = getItersForOpDuration(durationPerOp, minWindowTime);
91+
initialIterations = getItersForOpDuration(durationPerOp, targetTime);
9592
samples++;
9693
}
9794
}
@@ -125,19 +122,16 @@ async function runBenchmarkOnce(
125122
);
126123
timeSpent += duration;
127124

128-
iterations += realIterations;
129-
iterations = Math.min(Number.MAX_SAFE_INTEGER, iterations);
125+
iterations = Math.min(Number.MAX_SAFE_INTEGER, iterations + realIterations);
130126

131127
// Just to avoid issues with empty fn
132128
const durationPerOp = Math.max(MIN_RESOLUTION, duration / realIterations);
133129

134130
histogram.record(durationPerOp);
135131

136-
const minWindowTime = Math.max(
137-
0,
138-
Math.min((maxDuration - timeSpent) / timer.scale, bench.minTime),
139-
);
140-
initialIterations = getItersForOpDuration(durationPerOp, minWindowTime);
132+
const remainingTime = Math.max(0, (maxDuration - timeSpent) / timer.scale);
133+
const targetTime = Math.min(remainingTime, bench.minTime);
134+
initialIterations = getItersForOpDuration(durationPerOp, targetTime);
141135
}
142136

143137
return { iterations, timeSpent };
@@ -186,8 +180,8 @@ async function runBenchmark(
186180
}
187181
histogram.finish();
188182

189-
const opsSec = totalIterations / (totalTimeSpent / timer.scale);
190183
const totalTime = totalTimeSpent / timer.scale; // Convert ns to seconds
184+
const opsSec = totalIterations / totalTime;
191185

192186
const sampleData = histogram.samples;
193187

0 commit comments

Comments
 (0)