forked from tinylibs/tinypool
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: convert benchmarks to vitest (tinylibs#93)
- Loading branch information
1 parent
040927a
commit d1ccdf4
Showing
9 changed files
with
154 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters