From: Jérôme Benoit Date: Tue, 8 Jun 2021 11:18:10 +0000 (+0200) Subject: Busy wait with a sleep inside. X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=a8bf8b7d73343b33657cb8d6f36091a3bcdc38d6;p=benchmarks-js.git Busy wait with a sleep inside. Signed-off-by: Jérôme Benoit --- diff --git a/busy-wait.js b/busy-wait.js index 5ea20aa..d49a5fd 100644 --- a/busy-wait.js +++ b/busy-wait.js @@ -4,20 +4,26 @@ const { LIST_FORMATTER, sleep } = require('./benchmark-utils') const suite = new Benchmark.Suite() const timeout = 2000 +const interval = 1000 /** * @param timeoutMs */ -function dummyTimeoutBusyWait (timeoutMs) { +async function sleepTimeoutBusyWait (timeoutMs) { const timeoutTimestampMs = Date.now() + timeoutMs - do {} while (Date.now() < timeoutTimestampMs) + 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 +36,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(() => { @@ -42,8 +48,8 @@ function setIntervalTimeoutBusyWait (timeoutMs, intervalMs = 200) { } suite - .add('dummyTimeoutBusyWait', function () { - dummyTimeoutBusyWait(timeout) + .add('sleepTimeoutBusyWait', async function () { + sleepTimeoutBusyWait(timeout) }) .add('divideAndConquerTimeoutBusyWait', async function () { await divideAndConquerTimeoutBusyWait(timeout)