lib/RunState.js

const {createHistogram} = require('node:perf_hooks');

module.exports = RunState;

/**
 * RecordableHistogram
 *
 * @external RecordableHistogram
 * @see {@link https://nodejs.org/docs/latest/api/perf_hooks.html#class-recordablehistogram-extends-histogram}
 */

/**
 * @example
 * const RunState = require('pete/lib/RunState');
 * var state = new RunState();
 *
 * @alias module:pete/lib/RunState
 * @class
 * @param {number} [maxHDRValue=Number.MAX_SAFE_INTEGER]   Must be higher than 1
 */
function RunState (maxHDRValue = Number.MAX_SAFE_INTEGER) {
	/**
	 * @type {external:RecordableHistogram}
	 */
	this.hdr = createHistogram({
		lowest : 1,
		highest: maxHDRValue > 1 && maxHDRValue <= Number.MAX_SAFE_INTEGER ? maxHDRValue : Number.MAX_SAFE_INTEGER
	});

	this.reset();
}

/**
 * Reset values to initial state.
 */
RunState.prototype.reset = function reset () {
	/**
	 * File path of run type used for testing target function.
	 *
	 * @type {string}
	 * @default ''
	 */
	this.runType = '';
	/**
	 * For example: "regular", "skip" or "todo".
	 *
	 * @type {string}
	 * @default ''
	 */
	this.mode = '';
	/**
	 * @type {bigint}
	 * @default 0
	 */
	this.started = BigInt(0);
	/**
	 * @type {bigint}
	 * @default 0
	 */
	this.current = BigInt(0);
	/**
	 * @type {bigint}
	 * @default 0
	 */
	this.finished = BigInt(0);
	/**
	 * @type {bigint}
	 * @default 0
	 */
	this.totalCPUTime = BigInt(0);
	/**
	 * @type {bigint}
	 * @default 0
	 */
	this.totalRealTime = BigInt(0);
	/**
	 * @type {number}
	 * @default 0
	 */
	this.samplesCount = 0;
	/**
	 * @type {Error|null}
	 * @default null
	 */
	this.error = null;

	this.hdr.reset();
};