From 737c6d97f8089996a9e30f77104d58afcf5791ba Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 2 Apr 2023 19:13:10 +0200 Subject: [PATCH] refactor: rename a worker choice strategy MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- CHANGELOG.md | 3 +- README.md | 2 +- benchmarks/internal/cluster/dynamic.js | 2 +- benchmarks/internal/cluster/fixed.js | 2 +- benchmarks/internal/thread/dynamic.js | 2 +- benchmarks/internal/thread/fixed.js | 2 +- ...ts => less-used-worker-choice-strategy.ts} | 4 +-- .../selection-strategies-types.ts | 4 +-- .../selection-strategies-utils.ts | 6 ++-- tests/pools/abstract/abstract-pool.test.js | 4 +-- .../selection-strategies-utils.test.js | 10 +++---- .../selection-strategies.test.js | 28 +++++++++---------- .../worker-choice-strategy-context.test.js | 16 +++++------ 13 files changed, 43 insertions(+), 42 deletions(-) rename src/pools/selection-strategies/{less-recently-used-worker-choice-strategy.ts => less-used-worker-choice-strategy.ts} (93%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 84e8ac47..17e3ff8a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Optimize worker storage in pool. - Optimize worker alive status check. -- Optimize `LESS_RECENTLY_USED` worker choice strategy. +- Rename worker choice strategy `LESS_RECENTLY_USED to `LESS_USED` . +- Optimize `LESS_USED` worker choice strategy. ### Fixed diff --git a/README.md b/README.md index 7087efa1..a4fab9d8 100644 --- a/README.md +++ b/README.md @@ -163,7 +163,7 @@ Node versions >= 16.x are supported. - `workerChoiceStrategy` (optional) - The worker choice strategy to use in this pool: - `WorkerChoiceStrategies.ROUND_ROBIN`: Submit tasks to worker in a round robbin fashion - - `WorkerChoiceStrategies.LESS_RECENTLY_USED`: Submit tasks to the less recently used worker + - `WorkerChoiceStrategies.LESS_USED`: Submit tasks to the less used worker - `WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN` Submit tasks to worker using a weighted round robin scheduling algorithm based on tasks execution time - `WorkerChoiceStrategies.FAIR_SHARE`: Submit tasks to worker using a fair share tasks scheduling algorithm based on tasks execution time diff --git a/benchmarks/internal/cluster/dynamic.js b/benchmarks/internal/cluster/dynamic.js index 105bbdaa..43fa0485 100644 --- a/benchmarks/internal/cluster/dynamic.js +++ b/benchmarks/internal/cluster/dynamic.js @@ -17,7 +17,7 @@ const dynamicPoolLessRecentlyUsed = new DynamicClusterPool( size / 2, size * 3, './benchmarks/internal/cluster/worker.js', - { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED } + { workerChoiceStrategy: WorkerChoiceStrategies.LESS_USED } ) const dynamicPoolWeightedRoundRobin = new DynamicClusterPool( diff --git a/benchmarks/internal/cluster/fixed.js b/benchmarks/internal/cluster/fixed.js index da691aa9..211db608 100644 --- a/benchmarks/internal/cluster/fixed.js +++ b/benchmarks/internal/cluster/fixed.js @@ -15,7 +15,7 @@ const fixedPool = new FixedClusterPool( const fixedPoolLessRecentlyUsed = new FixedClusterPool( size, './benchmarks/internal/cluster/worker.js', - { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED } + { workerChoiceStrategy: WorkerChoiceStrategies.LESS_USED } ) const fixedPoolWeightedRoundRobin = new FixedClusterPool( diff --git a/benchmarks/internal/thread/dynamic.js b/benchmarks/internal/thread/dynamic.js index fa2e0f83..79d47215 100644 --- a/benchmarks/internal/thread/dynamic.js +++ b/benchmarks/internal/thread/dynamic.js @@ -17,7 +17,7 @@ const dynamicPoolLessRecentlyUsed = new DynamicThreadPool( size / 2, size * 3, './benchmarks/internal/thread/worker.js', - { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED } + { workerChoiceStrategy: WorkerChoiceStrategies.LESS_USED } ) const dynamicPoolWeightedRoundRobin = new DynamicThreadPool( diff --git a/benchmarks/internal/thread/fixed.js b/benchmarks/internal/thread/fixed.js index 3f40f3fb..2d6ee355 100644 --- a/benchmarks/internal/thread/fixed.js +++ b/benchmarks/internal/thread/fixed.js @@ -15,7 +15,7 @@ const fixedPool = new FixedThreadPool( const fixedPoolLessRecentlyUsed = new FixedThreadPool( size, './benchmarks/internal/thread/worker.js', - { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED } + { workerChoiceStrategy: WorkerChoiceStrategies.LESS_USED } ) const fixedPoolWeightedRoundRobin = new FixedThreadPool( diff --git a/src/pools/selection-strategies/less-recently-used-worker-choice-strategy.ts b/src/pools/selection-strategies/less-used-worker-choice-strategy.ts similarity index 93% rename from src/pools/selection-strategies/less-recently-used-worker-choice-strategy.ts rename to src/pools/selection-strategies/less-used-worker-choice-strategy.ts index fc64a74a..f220b2c8 100644 --- a/src/pools/selection-strategies/less-recently-used-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/less-used-worker-choice-strategy.ts @@ -2,13 +2,13 @@ import type { IPoolWorker } from '../pool-worker' import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' /** - * Selects the less recently used worker. + * Selects the less used worker. * * @typeParam Worker - Type of worker which manages the strategy. * @typeParam Data - Type of data sent to the worker. This can only be serializable data. * @typeParam Response - Type of response of execution. This can only be serializable data. */ -export class LessRecentlyUsedWorkerChoiceStrategy< +export class LessUsedWorkerChoiceStrategy< Worker extends IPoolWorker, Data, Response diff --git a/src/pools/selection-strategies/selection-strategies-types.ts b/src/pools/selection-strategies/selection-strategies-types.ts index 3e7c3427..23efd299 100644 --- a/src/pools/selection-strategies/selection-strategies-types.ts +++ b/src/pools/selection-strategies/selection-strategies-types.ts @@ -9,9 +9,9 @@ export const WorkerChoiceStrategies = Object.freeze({ */ ROUND_ROBIN: 'ROUND_ROBIN', /** - * Less recently used worker selection strategy. + * Less used worker selection strategy. */ - LESS_RECENTLY_USED: 'LESS_RECENTLY_USED', + LESS_USED: 'LESS_USED', /** * Fair share worker selection strategy. */ diff --git a/src/pools/selection-strategies/selection-strategies-utils.ts b/src/pools/selection-strategies/selection-strategies-utils.ts index 4c0fea61..75848544 100644 --- a/src/pools/selection-strategies/selection-strategies-utils.ts +++ b/src/pools/selection-strategies/selection-strategies-utils.ts @@ -1,7 +1,7 @@ import type { IPoolInternal } from '../pool-internal' import type { IPoolWorker } from '../pool-worker' import { FairShareWorkerChoiceStrategy } from './fair-share-worker-choice-strategy' -import { LessRecentlyUsedWorkerChoiceStrategy } from './less-recently-used-worker-choice-strategy' +import { LessUsedWorkerChoiceStrategy } from './less-used-worker-choice-strategy' import { RoundRobinWorkerChoiceStrategy } from './round-robin-worker-choice-strategy' import type { IWorkerChoiceStrategy, @@ -28,8 +28,8 @@ export function getWorkerChoiceStrategy< switch (workerChoiceStrategy) { case WorkerChoiceStrategies.ROUND_ROBIN: return new RoundRobinWorkerChoiceStrategy(pool) - case WorkerChoiceStrategies.LESS_RECENTLY_USED: - return new LessRecentlyUsedWorkerChoiceStrategy(pool) + case WorkerChoiceStrategies.LESS_USED: + return new LessUsedWorkerChoiceStrategy(pool) case WorkerChoiceStrategies.FAIR_SHARE: return new FairShareWorkerChoiceStrategy(pool) case WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN: diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract/abstract-pool.test.js index 011456ae..83290d6a 100644 --- a/tests/pools/abstract/abstract-pool.test.js +++ b/tests/pools/abstract/abstract-pool.test.js @@ -98,7 +98,7 @@ describe('Abstract pool test suite', () => { numberOfWorkers, './tests/worker-files/thread/testWorker.js', { - workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED, + workerChoiceStrategy: WorkerChoiceStrategies.LESS_USED, enableEvents: false, messageHandler: testHandler, errorHandler: testHandler, @@ -109,7 +109,7 @@ describe('Abstract pool test suite', () => { expect(pool.opts.enableEvents).toBe(false) expect(pool.emitter).toBeUndefined() expect(pool.opts.workerChoiceStrategy).toBe( - WorkerChoiceStrategies.LESS_RECENTLY_USED + WorkerChoiceStrategies.LESS_USED ) expect(pool.opts.messageHandler).toStrictEqual(testHandler) expect(pool.opts.errorHandler).toStrictEqual(testHandler) diff --git a/tests/pools/selection-strategies/selection-strategies-utils.test.js b/tests/pools/selection-strategies/selection-strategies-utils.test.js index eaac1342..43496040 100644 --- a/tests/pools/selection-strategies/selection-strategies-utils.test.js +++ b/tests/pools/selection-strategies/selection-strategies-utils.test.js @@ -11,8 +11,8 @@ const { RoundRobinWorkerChoiceStrategy } = require('../../../lib/pools/selection-strategies/round-robin-worker-choice-strategy') const { - LessRecentlyUsedWorkerChoiceStrategy -} = require('../../../lib/pools/selection-strategies/less-recently-used-worker-choice-strategy') + LessUsedWorkerChoiceStrategy +} = require('../../../lib/pools/selection-strategies/less-used-worker-choice-strategy') const { FairShareWorkerChoiceStrategy } = require('../../../lib/pools/selection-strategies/fair-share-worker-choice-strategy') @@ -49,12 +49,12 @@ describe('Selection strategies utils test suite', () => { expect(strategy).toBeInstanceOf(RoundRobinWorkerChoiceStrategy) }) - it('Verify that getWorkerChoiceStrategy() can return LESS_RECENTLY_USED strategy', () => { + it('Verify that getWorkerChoiceStrategy() can return LESS_USED strategy', () => { const strategy = getWorkerChoiceStrategy( pool, - WorkerChoiceStrategies.LESS_RECENTLY_USED + WorkerChoiceStrategies.LESS_USED ) - expect(strategy).toBeInstanceOf(LessRecentlyUsedWorkerChoiceStrategy) + expect(strategy).toBeInstanceOf(LessUsedWorkerChoiceStrategy) }) it('Verify that getWorkerChoiceStrategy() can return FAIR_SHARE strategy', () => { diff --git a/tests/pools/selection-strategies/selection-strategies.test.js b/tests/pools/selection-strategies/selection-strategies.test.js index c55b971e..22641947 100644 --- a/tests/pools/selection-strategies/selection-strategies.test.js +++ b/tests/pools/selection-strategies/selection-strategies.test.js @@ -12,7 +12,7 @@ describe('Selection strategies test suite', () => { it('Verify that WorkerChoiceStrategies enumeration provides string values', () => { expect(WorkerChoiceStrategies.ROUND_ROBIN).toBe('ROUND_ROBIN') - expect(WorkerChoiceStrategies.LESS_RECENTLY_USED).toBe('LESS_RECENTLY_USED') + expect(WorkerChoiceStrategies.LESS_USED).toBe('LESS_USED') expect(WorkerChoiceStrategies.FAIR_SHARE).toBe('FAIR_SHARE') expect(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN).toBe( 'WEIGHTED_ROUND_ROBIN' @@ -179,38 +179,38 @@ describe('Selection strategies test suite', () => { await pool.destroy() }) - it('Verify LESS_RECENTLY_USED strategy is taken at pool creation', async () => { + it('Verify LESS_USED strategy is taken at pool creation', async () => { const pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.js', - { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED } + { workerChoiceStrategy: WorkerChoiceStrategies.LESS_USED } ) expect(pool.opts.workerChoiceStrategy).toBe( - WorkerChoiceStrategies.LESS_RECENTLY_USED + WorkerChoiceStrategies.LESS_USED ) // We need to clean up the resources after our test await pool.destroy() }) - it('Verify LESS_RECENTLY_USED strategy can be set after pool creation', async () => { + it('Verify LESS_USED strategy can be set after pool creation', async () => { const pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.js' ) - pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_RECENTLY_USED) + pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_USED) expect(pool.opts.workerChoiceStrategy).toBe( - WorkerChoiceStrategies.LESS_RECENTLY_USED + WorkerChoiceStrategies.LESS_USED ) // We need to clean up the resources after our test await pool.destroy() }) - it('Verify LESS_RECENTLY_USED strategy default tasks usage statistics requirements', async () => { + it('Verify LESS_USED strategy default tasks usage statistics requirements', async () => { let pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.js' ) - pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_RECENTLY_USED) + pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_USED) expect( pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() .requiredStatistics.runTime @@ -221,7 +221,7 @@ describe('Selection strategies test suite', () => { max, './tests/worker-files/thread/testWorker.js' ) - pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_RECENTLY_USED) + pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_USED) expect( pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() .requiredStatistics.runTime @@ -230,11 +230,11 @@ describe('Selection strategies test suite', () => { await pool.destroy() }) - it('Verify LESS_RECENTLY_USED strategy can be run in a fixed pool', async () => { + it('Verify LESS_USED strategy can be run in a fixed pool', async () => { const pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.js', - { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED } + { workerChoiceStrategy: WorkerChoiceStrategies.LESS_USED } ) // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose` const promises = [] @@ -246,12 +246,12 @@ describe('Selection strategies test suite', () => { await pool.destroy() }) - it('Verify LESS_RECENTLY_USED strategy can be run in a dynamic pool', async () => { + it('Verify LESS_USED strategy can be run in a dynamic pool', async () => { const pool = new DynamicThreadPool( min, max, './tests/worker-files/thread/testWorker.js', - { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED } + { workerChoiceStrategy: WorkerChoiceStrategies.LESS_USED } ) // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose` const promises = [] diff --git a/tests/pools/selection-strategies/worker-choice-strategy-context.test.js b/tests/pools/selection-strategies/worker-choice-strategy-context.test.js index 3ce8b9fe..0079e728 100644 --- a/tests/pools/selection-strategies/worker-choice-strategy-context.test.js +++ b/tests/pools/selection-strategies/worker-choice-strategy-context.test.js @@ -12,8 +12,8 @@ const { RoundRobinWorkerChoiceStrategy } = require('../../../lib/pools/selection-strategies/round-robin-worker-choice-strategy') const { - LessRecentlyUsedWorkerChoiceStrategy -} = require('../../../lib/pools/selection-strategies/less-recently-used-worker-choice-strategy') + LessUsedWorkerChoiceStrategy +} = require('../../../lib/pools/selection-strategies/less-used-worker-choice-strategy') const { FairShareWorkerChoiceStrategy } = require('../../../lib/pools/selection-strategies/fair-share-worker-choice-strategy') @@ -113,31 +113,31 @@ describe('Worker choice strategy context test suite', () => { ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy) }) - it('Verify that setWorkerChoiceStrategy() works with LESS_RECENTLY_USED and fixed pool', () => { + it('Verify that setWorkerChoiceStrategy() works with LESS_USED and fixed pool', () => { const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( fixedPool ) workerChoiceStrategyContext.setWorkerChoiceStrategy( - WorkerChoiceStrategies.LESS_RECENTLY_USED + WorkerChoiceStrategies.LESS_USED ) expect( workerChoiceStrategyContext.getWorkerChoiceStrategy() - ).toBeInstanceOf(LessRecentlyUsedWorkerChoiceStrategy) + ).toBeInstanceOf(LessUsedWorkerChoiceStrategy) }) - it('Verify that setWorkerChoiceStrategy() works with LESS_RECENTLY_USED and dynamic pool', () => { + it('Verify that setWorkerChoiceStrategy() works with LESS_USED and dynamic pool', () => { const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( dynamicPool ) workerChoiceStrategyContext.setWorkerChoiceStrategy( - WorkerChoiceStrategies.LESS_RECENTLY_USED + WorkerChoiceStrategies.LESS_USED ) expect( workerChoiceStrategyContext.getWorkerChoiceStrategy() ).toBeInstanceOf(DynamicPoolWorkerChoiceStrategy) expect( workerChoiceStrategyContext.getWorkerChoiceStrategy().workerChoiceStrategy - ).toBeInstanceOf(LessRecentlyUsedWorkerChoiceStrategy) + ).toBeInstanceOf(LessUsedWorkerChoiceStrategy) }) it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and fixed pool', () => { -- 2.34.1