-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
+ * @param intervalMs
*/
-async function sleepTimeoutBusyWait (timeoutMs) {
+async function sleepTimeoutBusyWait (timeoutMs, intervalMs = interval) {
const timeoutTimestampMs = Date.now() + timeoutMs
do {
- await sleep(interval)
+ await sleep(intervalMs)
} while (Date.now() < timeoutTimestampMs)
}
* @param timeoutMs
* @param intervalMs
*/
-function setIntervalTimeoutBusyWait (timeoutMs, intervalMs = interval) {
+async function setIntervalTimeoutBusyWait (timeoutMs, intervalMs = interval) {
const tries = Math.round(timeoutMs / intervalMs)
let count = 0
const triesSetInterval = setInterval(() => {
count++
if (count === tries) {
clearInterval(triesSetInterval)
+ return Promise.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 })
+)