From 6961ca9a305bb2049105e6710a53a4f5f7970cb6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 1 Jul 2023 12:28:17 +0200 Subject: [PATCH] docs: document availableParallelism() usage MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- CHANGELOG.md | 4 ++++ README.md | 6 +++--- examples/dynamicExample.js | 22 ++++++++++++++++------ examples/fixedExample.js | 13 +++++++++---- examples/multiFunctionExample.js | 15 ++++++++++----- examples/typescript/pool.ts | 12 ++++++++---- tests/utils.test.js | 6 +++++- 7 files changed, 55 insertions(+), 23 deletions(-) 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) + }) }) -- 2.34.1