X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=busy-wait.js;h=66d58967ed9e86c0cf00b61708379dc46f2e0be4;hb=b3b242bed0253f6471f1718c3baf0a91f4952f39;hp=9ff5a000f72463fcb600c45644e8f95deb4e05dd;hpb=a9c78d5d3394dce4eccd9f244c88879666cba677;p=benchmarks-js.git diff --git a/busy-wait.js b/busy-wait.js index 9ff5a00..66d5896 100644 --- a/busy-wait.js +++ b/busy-wait.js @@ -1,64 +1,78 @@ -const Benchmark = require('benchmark') -const { LIST_FORMATTER, sleep } = require('./benchmark-utils') - -const suite = new Benchmark.Suite() +const Benchmark = require('benny') +const { sleep } = require('./benchmark-utils') const timeout = 2000 +const interval = 1000 /** * @param timeoutMs */ function dummyTimeoutBusyWait (timeoutMs) { const timeoutTimestampMs = Date.now() + timeoutMs + // eslint-disable-next-line no-empty do {} while (Date.now() < timeoutTimestampMs) } /** * @param timeoutMs - * @param delayMs + * @param intervalMs + */ +async function sleepTimeoutBusyWait (timeoutMs, intervalMs = interval) { + const timeoutTimestampMs = Date.now() + timeoutMs + do { + await sleep(intervalMs) + } while (Date.now() < timeoutTimestampMs) +} + +/** + * @param timeoutMs + * @param intervalMs */ -async function divideAndConquerTimeoutBusyWait (timeoutMs, delayMs = 200) { - const tries = Math.round(timeoutMs / delayMs) +async function divideAndConquerTimeoutBusyWait ( + timeoutMs, + intervalMs = interval +) { + const tries = Math.round(timeoutMs / intervalMs) let count = 0 do { count++ - await sleep(delayMs) + await sleep(intervalMs) } while (count <= tries) } /** * @param timeoutMs - * @param delayMs + * @param intervalMs */ -function setIntervalTimeoutBusyWait (timeoutMs, delayMs = 200) { - const tries = Math.round(timeoutMs / delayMs) +async function setIntervalTimeoutBusyWait (timeoutMs, intervalMs = interval) { + const tries = Math.round(timeoutMs / intervalMs) let count = 0 const triesSetInterval = setInterval(() => { count++ if (count === tries) { clearInterval(triesSetInterval) + return Promise.resolve() } - }, delayMs) + }, intervalMs) } -suite - .add('dummyTimeoutBusyWait', function () { +Benchmark.suite( + 'Busy wait', + Benchmark.add('dummyTimeoutBusyWait', () => { dummyTimeoutBusyWait(timeout) - }) - .add('divideAndConquerTimeoutBusyWait', async function () { + }), + Benchmark.add('sleepTimeoutBusyWait', async () => { + await sleepTimeoutBusyWait(timeout) + }), + Benchmark.add('divideAndConquerTimeoutBusyWait', async () => { await divideAndConquerTimeoutBusyWait(timeout) - }) - .add('setIntervalTimeoutBusyWait', function () { - setIntervalTimeoutBusyWait(timeout) - }) - .on('cycle', function (event) { - console.log(event.target.toString()) - }) - .on('complete', function () { - console.log( - 'Fastest is ' + LIST_FORMATTER.format(this.filter('fastest').map('name')) - ) - // eslint-disable-next-line no-process-exit - process.exit() - }) - .run() + }), + Benchmark.add('setIntervalTimeoutBusyWait', async () => { + await setIntervalTimeoutBusyWait(timeout) + }), + Benchmark.cycle(), + Benchmark.complete(), + Benchmark.save({ file: 'busy-wait', format: 'json', details: true }), + Benchmark.save({ file: 'busy-wait', format: 'chart.html', details: true }), + Benchmark.save({ file: 'busy-wait', format: 'table.html', details: true }) +)