refactor: rename a worker choice strategy
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 2 Apr 2023 17:13:10 +0000 (19:13 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sun, 2 Apr 2023 17:13:10 +0000 (19:13 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
13 files changed:
CHANGELOG.md
README.md
benchmarks/internal/cluster/dynamic.js
benchmarks/internal/cluster/fixed.js
benchmarks/internal/thread/dynamic.js
benchmarks/internal/thread/fixed.js
src/pools/selection-strategies/less-used-worker-choice-strategy.ts [moved from src/pools/selection-strategies/less-recently-used-worker-choice-strategy.ts with 93% similarity]
src/pools/selection-strategies/selection-strategies-types.ts
src/pools/selection-strategies/selection-strategies-utils.ts
tests/pools/abstract/abstract-pool.test.js
tests/pools/selection-strategies/selection-strategies-utils.test.js
tests/pools/selection-strategies/selection-strategies.test.js
tests/pools/selection-strategies/worker-choice-strategy-context.test.js

index 84e8ac470ae86b65845b0b18c9aefd640c0f0fd1..17e3ff8ada090ba79be228bfc6ae35be70f554f9 100644 (file)
@@ -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
 
index 7087efa1f0ded2513c154cc966790e0b2ad59031..a4fab9d88cff228a0f82a6effc67177c43d80b23 100644 (file)
--- 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
 
index 105bbdaa9d77eef540cfa361e6d794095b400363..43fa0485e9e0ff93c03a8504bf27b8c5c713c665 100644 (file)
@@ -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(
index da691aa9237a387d1b6d2cfe4ffe01d64cf506f9..211db608f4d67b21fa9de8a78266f2f483745e08 100644 (file)
@@ -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(
index fa2e0f83bf56c4cf4eaf62814c32e46b8206af94..79d4721585c01a9e69dbb261ad98b1b81b8a2bb2 100644 (file)
@@ -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(
index 3f40f3fbcac920f9f4013bdb59d3c155fdb42b57..2d6ee355a1f30058a76dd0d96b5d824d3c07b16b 100644 (file)
@@ -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(
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 fc64a74a6bc7e2085b7e623b5ddc222825d019da..f220b2c8aba228a6d6f28bae86d06f791d7c8b83 100644 (file)
@@ -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
index 3e7c3427e84f7bedb015e8f6d7ef54215377bd40..23efd2993d64eefd07087032e8ae39be5c2f0ccd 100644 (file)
@@ -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.
    */
index 4c0fea61ef05a4ebb398a396c580bfe9d0afa084..75848544a295b221bad52c91a17f47fa3f4186a9 100644 (file)
@@ -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:
index 011456aea0aa23c4b8111ceb32230aa2a96ca558..83290d6a0553243840e9c8b313d2ececacd4386d 100644 (file)
@@ -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)
index eaac1342337eae176466a80adb06547d52e609ae..434960403278aa4eff3af043ef5692c25e276d3c 100644 (file)
@@ -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', () => {
index c55b971edb8a9ed3e47ccae1db4b0deff206b08b..226419475219094bdd4822c23ef16fcbdb7dbd12 100644 (file)
@@ -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 = []
index 3ce8b9fe040218f9b8e3be0b7a184b87737af0a6..0079e728295907bf76ca2c6cec5a25833372eac7 100644 (file)
@@ -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', () => {