/** * @author Martin Karkowski * @email m.karkowski@zema.de * @create date 2020-08-25 16:18:36 * @modify date 2020-11-10 09:39:14 * @desc [description] */ import { Logger } from 'winston'; import { exportFunctionToDispatcher } from '../../lib/decorators/dispatcherDecorators'; import { getNopeLogger } from '../../lib/logger/getLogger'; 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') 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]'); // Reset the Counter _counter = 0; } _counter++; }; }, { id: 'generateBenchmarkFunction' });