@@ -7,16 +7,15 @@ const {
77const { 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 */
1314function 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
2221function 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 */
6665async 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