X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=busy-wait.js;h=9d0d31b4d6ee0285a9fe7fcd19a54e96e17de913;hb=662a730a20d42b0057cf65d1baeb77982e29d3ba;hp=5ea20aad653f21df72c89d403b11a57506db2a0b;hpb=e47f67dbe01272e83e27e6eafede736e925266fd;p=benchmarks-js.git diff --git a/busy-wait.js b/busy-wait.js index 5ea20aa..9d0d31b 100644 --- a/busy-wait.js +++ b/busy-wait.js @@ -1,23 +1,36 @@ -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 + */ +async function sleepTimeoutBusyWait (timeoutMs) { + const timeoutTimestampMs = Date.now() + timeoutMs + do { + await sleep(interval) + } while (Date.now() < timeoutTimestampMs) +} + /** * @param timeoutMs * @param intervalMs */ -async function divideAndConquerTimeoutBusyWait (timeoutMs, intervalMs = 200) { +async function divideAndConquerTimeoutBusyWait ( + timeoutMs, + intervalMs = interval +) { const tries = Math.round(timeoutMs / intervalMs) let count = 0 do { @@ -30,7 +43,7 @@ async function divideAndConquerTimeoutBusyWait (timeoutMs, intervalMs = 200) { * @param timeoutMs * @param intervalMs */ -function setIntervalTimeoutBusyWait (timeoutMs, intervalMs = 200) { +function setIntervalTimeoutBusyWait (timeoutMs, intervalMs = interval) { const tries = Math.round(timeoutMs / intervalMs) let count = 0 const triesSetInterval = setInterval(() => { @@ -41,24 +54,23 @@ function setIntervalTimeoutBusyWait (timeoutMs, intervalMs = 200) { }, 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 () { + }), + Benchmark.add('setIntervalTimeoutBusyWait', () => { 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.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 }) +)