From: Jérôme Benoit Date: Sat, 1 Jul 2023 10:28:17 +0000 (+0200) Subject: docs: document availableParallelism() usage X-Git-Tag: v2.6.6~2 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=6961ca9a305bb2049105e6710a53a4f5f7970cb6;p=poolifier.git docs: document availableParallelism() usage Signed-off-by: Jérôme Benoit --- diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a6fb386..91cf115f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Add safe helper `availableParallelism` to help sizing the pool. + ### Fixed - Ensure message handler is only registered in worker. diff --git a/README.md b/README.md index ee40061d..53ea832d 100644 --- a/README.md +++ b/README.md @@ -114,10 +114,10 @@ Instantiate your pool based on your needs : ```js 'use strict' -const { DynamicThreadPool, FixedThreadPool, PoolEvents } = require('poolifier') +const { DynamicThreadPool, FixedThreadPool, PoolEvents, availableParallelism } = require('poolifier') // a fixed worker-threads pool -const pool = new FixedThreadPool(15, './yourWorker.js', { +const pool = new FixedThreadPool(availableParallelism(), './yourWorker.js', { errorHandler: e => console.error(e), onlineHandler: () => console.info('worker is online') }) @@ -125,7 +125,7 @@ const pool = new FixedThreadPool(15, './yourWorker.js', { pool.emitter.on(PoolEvents.busy, () => console.info('Pool is busy')) // or a dynamic worker-threads pool -const pool = new DynamicThreadPool(10, 100, './yourWorker.js', { +const pool = new DynamicThreadPool(availableParallelism() / 2, availableParallelism(), './yourWorker.js', { errorHandler: e => console.error(e), onlineHandler: () => console.info('worker is online') }) diff --git a/examples/dynamicExample.js b/examples/dynamicExample.js index 59d992a6..6af04560 100644 --- a/examples/dynamicExample.js +++ b/examples/dynamicExample.js @@ -1,14 +1,24 @@ -const { DynamicThreadPool, PoolEvents } = require('poolifier') -let resolved = 0 +const { + DynamicThreadPool, + PoolEvents, + availableParallelism +} = require('poolifier') + +const pool = new DynamicThreadPool( + availableParallelism() / 2, + availableParallelism(), + './yourWorker.js', + { + errorHandler: e => console.error(e), + onlineHandler: () => console.info('worker is online') + } +) let poolFull = 0 let poolBusy = 0 -const pool = new DynamicThreadPool(10, 20, './yourWorker.js', { - errorHandler: e => console.error(e), - onlineHandler: () => console.info('worker is online') -}) pool.emitter.on(PoolEvents.full, () => poolFull++) pool.emitter.on(PoolEvents.busy, () => poolBusy++) +let resolved = 0 const start = performance.now() const iterations = 1000 for (let i = 1; i <= iterations; i++) { diff --git a/examples/fixedExample.js b/examples/fixedExample.js index 478c37eb..2346f7b4 100644 --- a/examples/fixedExample.js +++ b/examples/fixedExample.js @@ -1,12 +1,17 @@ -const { FixedThreadPool, PoolEvents } = require('poolifier') -let resolved = 0 -let poolBusy = 0 -const pool = new FixedThreadPool(15, './yourWorker.js', { +const { + FixedThreadPool, + PoolEvents, + availableParallelism +} = require('poolifier') + +const pool = new FixedThreadPool(availableParallelism(), './yourWorker.js', { errorHandler: e => console.error(e), onlineHandler: () => console.info('worker is online') }) +let poolBusy = 0 pool.emitter.on(PoolEvents.busy, () => poolBusy++) +let resolved = 0 const start = performance.now() const iterations = 1000 for (let i = 1; i <= iterations; i++) { diff --git a/examples/multiFunctionExample.js b/examples/multiFunctionExample.js index 055f143f..829ba93d 100644 --- a/examples/multiFunctionExample.js +++ b/examples/multiFunctionExample.js @@ -1,8 +1,13 @@ -const { FixedThreadPool } = require('poolifier') -const pool = new FixedThreadPool(15, './multiFunctionWorker.js', { - errorHandler: e => console.error(e), - onlineHandler: () => console.info('worker is online') -}) +const { FixedThreadPool, availableParallelism } = require('poolifier') + +const pool = new FixedThreadPool( + availableParallelism(), + './multiFunctionWorker.js', + { + errorHandler: e => console.error(e), + onlineHandler: () => console.info('worker is online') + } +) pool .execute({ text: 'hello' }, 'fn0') diff --git a/examples/typescript/pool.ts b/examples/typescript/pool.ts index 869e62a8..b6bb4da3 100644 --- a/examples/typescript/pool.ts +++ b/examples/typescript/pool.ts @@ -1,9 +1,13 @@ import { join } from 'path' import type { MyData, MyResponse } from './worker' -import { DynamicThreadPool, FixedThreadPool } from 'poolifier' +import { + DynamicThreadPool, + FixedThreadPool, + availableParallelism +} from 'poolifier' export const fixedPool = new FixedThreadPool>( - 8, + availableParallelism(), join(__dirname, 'worker.js'), { errorHandler: (e: Error) => { @@ -16,8 +20,8 @@ export const fixedPool = new FixedThreadPool>( ) export const dynamicPool = new DynamicThreadPool>( - 2, - 8, + availableParallelism() / 2, + availableParallelism(), join(__dirname, 'worker.js'), { errorHandler: (e: Error) => { diff --git a/tests/utils.test.js b/tests/utils.test.js index b1a91776..f62d49c6 100644 --- a/tests/utils.test.js +++ b/tests/utils.test.js @@ -1,5 +1,5 @@ const { expect } = require('expect') -const { isPlainObject, median } = require('../lib/utils') +const { isPlainObject, median, availableParallelism } = require('../lib/utils') const { isKillBehavior, KillBehaviors @@ -60,4 +60,8 @@ describe('Utils test suite', () => { expect(isKillBehavior(KillBehaviors.HARD, null)).toBe(false) expect(isKillBehavior(KillBehaviors.SOFT, 'unknown')).toBe(false) }) + + it('Verify availableParallelism() behavior', () => { + expect(typeof availableParallelism() === 'number').toBe(true) + }) })