X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=busy-wait.js;h=a851a30e911870542cd9612b2777c7654c057a36;hb=938a5f27be90a49ea486298844fc31c869827049;hp=eece669424406485a677c5248337e335e4bee008;hpb=ed40d2b012a740d121f1951b9be91df341d6eaf3;p=benchmarks-js.git diff --git a/busy-wait.js b/busy-wait.js index eece669..a851a30 100644 --- a/busy-wait.js +++ b/busy-wait.js @@ -1,7 +1,5 @@ -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 @@ -10,19 +8,20 @@ const interval = 1000 * @param timeoutMs */ function dummyTimeoutBusyWait (timeoutMs) { - const timeoutTimestampMs = Date.now() + timeoutMs + const timeoutTimestampMs = performance.now() + timeoutMs // eslint-disable-next-line no-empty - do {} while (Date.now() < timeoutTimestampMs) + do {} while (performance.now() < timeoutTimestampMs) } /** * @param timeoutMs + * @param intervalMs */ -async function sleepTimeoutBusyWait (timeoutMs) { - const timeoutTimestampMs = Date.now() + timeoutMs +async function sleepTimeoutBusyWait (timeoutMs, intervalMs = interval) { + const timeoutTimestampMs = performance.now() + timeoutMs do { - await sleep(interval) - } while (Date.now() < timeoutTimestampMs) + await sleep(intervalMs) + } while (performance.now() < timeoutTimestampMs) } /** @@ -45,38 +44,39 @@ async function divideAndConquerTimeoutBusyWait ( * @param timeoutMs * @param intervalMs */ -function setIntervalTimeoutBusyWait (timeoutMs, intervalMs = interval) { - const tries = Math.round(timeoutMs / intervalMs) - let count = 0 - const triesSetInterval = setInterval(() => { - count++ - if (count === tries) { - clearInterval(triesSetInterval) - } - }, intervalMs) +async function setIntervalTimeoutBusyWait (timeoutMs, intervalMs = interval) { + return new Promise(resolve => { + const tries = Math.round(timeoutMs / intervalMs) + let count = 0 + const triesSetInterval = setInterval(() => { + count++ + if (count === tries) { + clearInterval(triesSetInterval) + return resolve() + } + }, intervalMs) + }) } -suite - .add('dummyTimeoutBusyWait', () => { +Benchmark.suite( + 'Busy wait', + Benchmark.add('dummyTimeoutBusyWait', () => { dummyTimeoutBusyWait(timeout) - }) - .add('sleepTimeoutBusyWait', async () => { + }), + Benchmark.add('sleepTimeoutBusyWait', async () => { await sleepTimeoutBusyWait(timeout) - }) - .add('divideAndConquerTimeoutBusyWait', async () => { + }), + Benchmark.add('divideAndConquerTimeoutBusyWait', async () => { await divideAndConquerTimeoutBusyWait(timeout) - }) - .add('setIntervalTimeoutBusyWait', () => { - setIntervalTimeoutBusyWait(timeout) - }) - .on('cycle', 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 n/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 }) +).catch(err => { + console.error(err) +})