2021-06-22 00:09:13 +09:00
|
|
|
import { cpus } from 'os'
|
|
|
|
|
2020-09-30 15:34:26 +09:00
|
|
|
import b from 'benny'
|
|
|
|
|
2021-06-03 00:03:47 +09:00
|
|
|
const {
|
|
|
|
benchAsyncTask,
|
|
|
|
benchThreadsafeFunction,
|
|
|
|
benchTokioFuture,
|
|
|
|
} = require('./index.node')
|
2020-09-30 15:34:26 +09:00
|
|
|
|
|
|
|
const buffer = Buffer.from('hello 🚀 rust!')
|
|
|
|
|
2021-06-22 00:09:13 +09:00
|
|
|
const ALL_THREADS = Array.from({ length: cpus().length })
|
|
|
|
|
2020-09-30 15:34:26 +09:00
|
|
|
export const benchAsync = () =>
|
|
|
|
b.suite(
|
|
|
|
'Async task',
|
2020-12-28 15:02:28 +09:00
|
|
|
b.add('spawn task', async () => {
|
2021-06-22 00:09:13 +09:00
|
|
|
await Promise.all(ALL_THREADS.map(() => benchAsyncTask(buffer)))
|
2020-09-30 15:34:26 +09:00
|
|
|
}),
|
2021-06-03 00:03:47 +09:00
|
|
|
b.add('ThreadSafeFunction', async () => {
|
2021-06-22 00:09:13 +09:00
|
|
|
await Promise.all(
|
|
|
|
ALL_THREADS.map(
|
|
|
|
() =>
|
|
|
|
new Promise<number | undefined>((resolve, reject) => {
|
|
|
|
benchThreadsafeFunction(buffer, (err?: Error, value?: number) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err)
|
|
|
|
} else {
|
|
|
|
resolve(value)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
)
|
2020-12-28 15:02:28 +09:00
|
|
|
}),
|
2021-06-03 00:03:47 +09:00
|
|
|
b.add('Tokio future to Promise', async () => {
|
2021-06-22 00:09:13 +09:00
|
|
|
await Promise.all(ALL_THREADS.map(() => benchTokioFuture(buffer)))
|
2021-06-03 00:03:47 +09:00
|
|
|
}),
|
2020-09-30 15:34:26 +09:00
|
|
|
b.cycle(),
|
|
|
|
b.complete(),
|
|
|
|
)
|