X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=busy-wait.mjs;h=ec8a70d816cf8272e5d0af0ca44e3ca3dd45dc9b;hb=HEAD;hp=578e90c06228df105853fb0623f4a5e72e236f0e;hpb=4247cbbd3c312d34802d06ca303be866cbc987ad;p=benchmarks-js.git diff --git a/busy-wait.mjs b/busy-wait.mjs index 578e90c..3786e8c 100644 --- a/busy-wait.mjs +++ b/busy-wait.mjs @@ -1,29 +1,10 @@ -import Benchmark from 'benny' +import { bench, group, run } from 'tatami-ng' + import { sleep } from './benchmark-utils.mjs' const timeout = 2000 const interval = 1000 -/** - * @param timeoutMs - */ -function dummyTimeoutBusyWait (timeoutMs) { - const timeoutTimestampMs = performance.now() + timeoutMs - // eslint-disable-next-line no-empty - do {} while (performance.now() < timeoutTimestampMs) -} - -/** - * @param timeoutMs - * @param intervalMs - */ -async function sleepTimeoutBusyWait (timeoutMs, intervalMs = interval) { - const timeoutTimestampMs = performance.now() + timeoutMs - do { - await sleep(intervalMs) - } while (performance.now() < timeoutTimestampMs) -} - /** * @param timeoutMs * @param intervalMs @@ -40,43 +21,59 @@ async function divideAndConquerTimeoutBusyWait ( } while (count <= tries) } +/** + * @param timeoutMs + */ +function dummyTimeoutBusyWait (timeoutMs) { + const timeoutTimestampMs = performance.now() + timeoutMs + // eslint-disable-next-line no-empty + do {} while (performance.now() < timeoutTimestampMs) +} + /** * @param timeoutMs * @param intervalMs */ async function setIntervalTimeoutBusyWait (timeoutMs, intervalMs = interval) { - return await new Promise(resolve => { + await new Promise(resolve => { const tries = Math.round(timeoutMs / intervalMs) let count = 0 const triesSetInterval = setInterval(() => { count++ if (count === tries) { clearInterval(triesSetInterval) - return resolve() + resolve() } }, intervalMs) }) } -Benchmark.suite( - 'Busy wait', - Benchmark.add('dummyTimeoutBusyWait', () => { +/** + * @param timeoutMs + * @param intervalMs + */ +async function sleepTimeoutBusyWait (timeoutMs, intervalMs = interval) { + const timeoutTimestampMs = performance.now() + timeoutMs + do { + await sleep(intervalMs) + } while (performance.now() < timeoutTimestampMs) +} + +group('Busy wait', () => { + bench('dummyTimeoutBusyWait', () => { dummyTimeoutBusyWait(timeout) - }), - Benchmark.add('sleepTimeoutBusyWait', async () => { + }) + bench('sleepTimeoutBusyWait', async () => { await sleepTimeoutBusyWait(timeout) - }), - Benchmark.add('divideAndConquerTimeoutBusyWait', async () => { + }) + bench('divideAndConquerTimeoutBusyWait', async () => { await divideAndConquerTimeoutBusyWait(timeout) - }), - Benchmark.add('setIntervalTimeoutBusyWait', async () => { + }) + bench('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) + }) +}) + +await run({ + units: true, })