Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: convert benchmarks to vitest #93

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
on: [workflow_dispatch]

name: Benchmark

jobs:
test:
name: Test
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
node-version: [18.x, 20.x]

runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v2

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

- uses: pnpm/action-setup@v2

- name: Install Dependencies
run: pnpm install

- name: Build
run: pnpm build

- name: Benchmark
run: pnpm bench
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.
91 changes: 91 additions & 0 deletions benchmark/isolate-benchmark.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { bench } from 'vitest'
import { cpus } from 'node:os'
import { Worker } from 'node:worker_threads'
import { fork } from 'node:child_process'
import Tinypool, { Options } from '../dist/index'

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: './benchmark/fixtures/add.mjs',
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.

28 changes: 0 additions & 28 deletions benchmark/simple-benchmark.mjs

This file was deleted.

21 changes: 21 additions & 0 deletions benchmark/simple.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { bench } from 'vitest'
import Tinypool from '../dist/index'

bench(
'simple',
async () => {
const pool = new Tinypool({
filename: './benchmark/fixtures/add.mjs',
})

const tasks: Promise<void>[] = []

while (pool.queueSize === 0) {
tasks.push(pool.run({ a: 4, b: 6 }))
}

await Promise.all(tasks)
await pool.destroy()
},
{ time: 10_000 }
)
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'],
},
},
})