1 const Benchmark
= require('benny')
2 const { sleep
} = require('./benchmark-utils')
10 function dummyTimeoutBusyWait (timeoutMs
) {
11 const timeoutTimestampMs
= Date
.now() + timeoutMs
12 // eslint-disable-next-line no-empty
13 do {} while (Date
.now() < timeoutTimestampMs
)
20 async
function sleepTimeoutBusyWait (timeoutMs
, intervalMs
= interval
) {
21 const timeoutTimestampMs
= Date
.now() + timeoutMs
23 await
sleep(intervalMs
)
24 } while (Date
.now() < timeoutTimestampMs
)
31 async
function divideAndConquerTimeoutBusyWait (
35 const tries
= Math
.round(timeoutMs
/ intervalMs
)
39 await
sleep(intervalMs
)
40 } while (count
<= tries
)
47 async
function setIntervalTimeoutBusyWait (timeoutMs
, intervalMs
= interval
) {
48 const tries
= Math
.round(timeoutMs
/ intervalMs
)
50 const triesSetInterval
= setInterval(() => {
52 if (count
=== tries
) {
53 clearInterval(triesSetInterval
)
54 return Promise
.resolve()
61 Benchmark
.add('dummyTimeoutBusyWait', () => {
62 dummyTimeoutBusyWait(timeout
)
64 Benchmark
.add('sleepTimeoutBusyWait', async () => {
65 await
sleepTimeoutBusyWait(timeout
)
67 Benchmark
.add('divideAndConquerTimeoutBusyWait', async () => {
68 await
divideAndConquerTimeoutBusyWait(timeout
)
70 Benchmark
.add('setIntervalTimeoutBusyWait', async () => {
71 await
setIntervalTimeoutBusyWait(timeout
)
75 Benchmark
.save({ file
: 'busy-wait', format
: 'json', details
: true }),
76 Benchmark
.save({ file
: 'busy-wait', format
: 'chart.html', details
: true }),
77 Benchmark
.save({ file
: 'busy-wait', format
: 'table.html', details
: true })