Skip to content

Commit

Permalink
test: convert benchmarks to vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed May 8, 2024
1 parent 040927a commit e2aba05
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 65 deletions.
5 changes: 5 additions & 0 deletions benchmark/fixtures/add-process.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import add from './add.mjs'

process.on('message', (message) => {
process.send(add(message))
})
File renamed without changes.
92 changes: 92 additions & 0 deletions benchmark/isolate-benchmark.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { bench } from 'vitest'
import { cpus } from 'node:os'
import { Worker } from 'node:worker_threads'

import Tinypool, { Options } from '../dist/index'
import { fork } from 'node:child_process'

const THREADS = cpus().length - 1
const ROUNDS = THREADS * 10
const ITERATIONS = 100

for (const runtime of [
'worker_threads',
'child_process',
] as Options['runtime'][]) {
bench(
`Tinypool { runtime: '${runtime}' }`,
async () => {
const pool = new Tinypool({
runtime,
filename: new URL('./fixtures/add.mjs', import.meta.url).href,
isolateWorkers: true,
minThreads: THREADS,
maxThreads: THREADS,
})

await Promise.all(
Array(ROUNDS)
.fill(0)
.map(() => pool.run({ a: 1, b: 2 }))
)

await pool.destroy()
},
{ iterations: ITERATIONS }
)
}

for (const { task, name } of [
{ name: 'worker_threads', task: workerThreadTask },
{ name: 'child_process', task: childProcessTask },
] as const) {
bench(
`node:${name}`,
async () => {
const pool = Array(ROUNDS).fill(task)

await Promise.all(
Array(THREADS)
.fill(execute)
.map((_task) => _task())
)

async function execute() {
const _task = pool.shift()

if (_task) {
await _task()
return execute()
}
}
},
{ iterations: ITERATIONS }
)
}

async function workerThreadTask() {
const worker = new Worker('./benchmark/fixtures/add-worker.mjs')
const onMessage = new Promise<void>((resolve, reject) =>
worker.on('message', (sum) => (sum === 3 ? resolve() : reject('Not 3')))
)

worker.postMessage({ a: 1, b: 2 })
await onMessage

await worker.terminate()
}

async function childProcessTask() {
const subprocess = fork('./benchmark/fixtures/add-process.mjs')

const onExit = new Promise((resolve) => subprocess.on('exit', resolve))
const onMessage = new Promise<void>((resolve, reject) =>
subprocess.on('message', (sum) => (sum === 3 ? resolve() : reject('Not 3')))
)

subprocess.send({ a: 1, b: 2 })
await onMessage

subprocess.kill()
await onExit
}
65 changes: 0 additions & 65 deletions benchmark/isolate-benchmark.mjs

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
},
"scripts": {
"test": "vitest",
"bench": "vitest bench",
"dev": "tsup --watch",
"build": "tsup",
"publish": "clean-publish",
Expand Down
4 changes: 4 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,9 @@ export default defineConfig({

// simple.test.ts expects to be run in main thread
poolMatchGlobs: [['**/simple.test.ts', 'forks']],

benchmark: {
include: ['**/**.bench.ts'],
},
},
})

0 comments on commit e2aba05

Please sign in to comment.