nope/modules/funcs/generateBenchmarkFunction.ts

50 lines
1.3 KiB
TypeScript
Raw Normal View History

2020-08-30 07:45:44 +00:00
/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @create date 2020-08-25 16:18:36
* @modify date 2020-11-10 09:39:14
2020-08-30 07:45:44 +00:00
* @desc [description]
*/
import { Logger } from 'winston';
import { exportFunctionToDispatcher } from '../../lib/decorators/dispatcherDecorators';
import { getNopeLogger } from '../../lib/logger/getLogger';
2020-08-30 07:45:44 +00:00
export function now() {
const _hr = process.hrtime();
return _hr[0] * 1000 + _hr[1] / 1000 / 1000;
}
/**
* Function to Generate a Benchmark-Function
* @param displayQpsAfter
* @param textToDisplay
*/
export const generateBenchmarkFunction = exportFunctionToDispatcher((displayQpsAfter: number, textToDisplay: string, logger?: Logger): () => void => {
let _counter = 0;
let _startTime = now();
const max = displayQpsAfter;
const _logger = logger !== undefined ? logger : getNopeLogger('algorithmen.benchmark')
2020-08-30 07:45:44 +00:00
return () => {
if (_counter === 1) {
_startTime = now();
} else if (_counter === max) {
const _endTime = now();
const _timeDifference = _endTime - _startTime;
const _qps = max / (_timeDifference / 1000);
// Show the QPS:
_logger.info(textToDisplay + ' QPS: ' + (Math.round(_qps * 100) / 100).toString() + ' [Calls/Sec]');
2020-08-30 07:45:44 +00:00
// Reset the Counter
2020-08-30 07:45:44 +00:00
_counter = 0;
}
_counter++;
};
}, {
id: 'generateBenchmarkFunction'
2020-08-30 07:45:44 +00:00
});