@stdlib/bench-harness
Version:
Benchmark harness.
99 lines (87 loc) • 2.76 kB
JavaScript
/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
;
// MODULES //
var pretest = require( './pretest.js' );
var iterations = require( './iterations.js' );
// MAIN //
/**
* Performs benchmark initialization tasks.
*
* @private
* @param {string} name - benchmark name
* @param {Options} opts - benchmark options
* @param {(Function|undefined)} benchmark - function containing benchmark code
* @param {Callback} clbk - callback to invoke after completing initialization tasks
* @returns {void}
*/
function init( name, opts, benchmark, clbk ) {
// If no benchmark function, then the benchmark is considered a "todo", so no need to repeat multiple times...
if ( !benchmark ) {
opts.repeats = 1;
return clbk( name, opts, benchmark );
}
// If the `skip` option to `true`, no need to initialize or repeat multiple times as will not be running the benchmark:
if ( opts.skip ) {
opts.repeats = 1;
return clbk( name, opts, benchmark );
}
// Perform pretests:
pretest( name, opts, benchmark, onPreTest );
/**
* Callback invoked upon completing pretests.
*
* @private
* @param {Error} [error] - error object
* @returns {void}
*/
function onPreTest( error ) {
// If the pretests failed, don't run the benchmark multiple times...
if ( error ) {
opts.repeats = 1;
opts.iterations = 1;
return clbk( name, opts, benchmark );
}
// If a user specified an iteration number, we can begin running benchmarks...
if ( opts.iterations ) {
return clbk( name, opts, benchmark );
}
// Determine iteration number:
iterations( name, opts, benchmark, onIterations );
}
/**
* Callback invoked upon determining an iteration number.
*
* @private
* @param {(Error|null)} error - error object
* @param {PositiveInteger} iter - number of iterations
* @returns {void}
*/
function onIterations( error, iter ) {
// If provided an error, then a benchmark failed, and, similar to pretests, don't run the benchmark multiple times...
if ( error ) {
opts.repeats = 1;
opts.iterations = 1;
return clbk( name, opts, benchmark );
}
opts.iterations = iter;
return clbk( name, opts, benchmark );
}
}
// EXPORTS //
module.exports = init;