From 86bf340d3da6dd24b2963b86e093b23104a2c265 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 30 May 2023 12:32:41 +0200 Subject: [PATCH] test: adapt for tasks usage wait time introduction MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .eslintrc.js | 4 + src/utils.ts | 3 +- tests/pools/abstract/abstract-pool.test.js | 159 +++++++---- .../selection-strategies.test.js | 267 +++++++++++------- 4 files changed, 275 insertions(+), 158 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index d7004cc5..0b465dad 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -38,6 +38,10 @@ module.exports = defineConfig({ { skipWords: [ 'Benoit', + 'Uint8', + 'Uint16', + 'Uint32', + 'Uint64', 'benny', 'browserslist', 'builtins', diff --git a/src/utils.ts b/src/utils.ts index 164a2aca..baaf1c1c 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -12,7 +12,8 @@ export const EMPTY_FUNCTION: () => void = Object.freeze(() => { */ export const DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS: WorkerChoiceStrategyOptions = { - medRunTime: false + medRunTime: false, + medWaitTime: false } /** diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract/abstract-pool.test.js index 02baefaf..db45fa07 100644 --- a/tests/pools/abstract/abstract-pool.test.js +++ b/tests/pools/abstract/abstract-pool.test.js @@ -90,7 +90,8 @@ describe('Abstract pool test suite', () => { WorkerChoiceStrategies.ROUND_ROBIN ) expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ - medRunTime: false + medRunTime: false, + medWaitTime: false }) expect(pool.opts.messageHandler).toBeUndefined() expect(pool.opts.errorHandler).toBeUndefined() @@ -177,18 +178,26 @@ describe('Abstract pool test suite', () => { { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE } ) expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ - medRunTime: false + medRunTime: false, + medWaitTime: false }) for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext .workerChoiceStrategies) { - expect(workerChoiceStrategy.opts).toStrictEqual({ medRunTime: false }) + expect(workerChoiceStrategy.opts).toStrictEqual({ + medRunTime: false, + medWaitTime: false + }) } expect( - pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime - ).toBe(true) - expect( - pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime - ).toBe(false) + pool.workerChoiceStrategyContext.getRequiredStatistics() + ).toStrictEqual({ + runTime: true, + avgRunTime: true, + medRunTime: false, + waitTime: false, + avgWaitTime: false, + medWaitTime: false + }) pool.setWorkerChoiceStrategyOptions({ medRunTime: true }) expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ medRunTime: true @@ -198,11 +207,15 @@ describe('Abstract pool test suite', () => { expect(workerChoiceStrategy.opts).toStrictEqual({ medRunTime: true }) } expect( - pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime - ).toBe(false) - expect( - pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime - ).toBe(true) + pool.workerChoiceStrategyContext.getRequiredStatistics() + ).toStrictEqual({ + runTime: true, + avgRunTime: false, + medRunTime: true, + waitTime: false, + avgWaitTime: false, + medWaitTime: false + }) pool.setWorkerChoiceStrategyOptions({ medRunTime: false }) expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ medRunTime: false @@ -212,11 +225,15 @@ describe('Abstract pool test suite', () => { expect(workerChoiceStrategy.opts).toStrictEqual({ medRunTime: false }) } expect( - pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime - ).toBe(true) - expect( - pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime - ).toBe(false) + pool.workerChoiceStrategyContext.getRequiredStatistics() + ).toStrictEqual({ + runTime: true, + avgRunTime: true, + medRunTime: false, + waitTime: false, + avgWaitTime: false, + medWaitTime: false + }) await pool.destroy() }) @@ -275,15 +292,19 @@ describe('Abstract pool test suite', () => { './tests/worker-files/cluster/testWorker.js' ) for (const workerNode of pool.workerNodes) { - expect(workerNode.tasksUsage).toBeDefined() - expect(workerNode.tasksUsage.run).toBe(0) - expect(workerNode.tasksUsage.running).toBe(0) - expect(workerNode.tasksUsage.runTime).toBe(0) - expect(workerNode.tasksUsage.runTimeHistory).toBeInstanceOf(CircularArray) - expect(workerNode.tasksUsage.runTimeHistory.length).toBe(0) - expect(workerNode.tasksUsage.avgRunTime).toBe(0) - expect(workerNode.tasksUsage.medRunTime).toBe(0) - expect(workerNode.tasksUsage.error).toBe(0) + expect(workerNode.tasksUsage).toStrictEqual({ + run: 0, + running: 0, + runTime: 0, + runTimeHistory: expect.any(CircularArray), + avgRunTime: 0, + medRunTime: 0, + waitTime: 0, + waitTimeHistory: expect.any(CircularArray), + avgWaitTime: 0, + medWaitTime: 0, + error: 0 + }) } await pool.destroy() }) @@ -311,27 +332,35 @@ describe('Abstract pool test suite', () => { promises.push(pool.execute()) } for (const workerNode of pool.workerNodes) { - expect(workerNode.tasksUsage).toBeDefined() - expect(workerNode.tasksUsage.run).toBe(0) - expect(workerNode.tasksUsage.running).toBe(numberOfWorkers * 2) - expect(workerNode.tasksUsage.runTime).toBe(0) - expect(workerNode.tasksUsage.runTimeHistory).toBeInstanceOf(CircularArray) - expect(workerNode.tasksUsage.runTimeHistory.length).toBe(0) - expect(workerNode.tasksUsage.avgRunTime).toBe(0) - expect(workerNode.tasksUsage.medRunTime).toBe(0) - expect(workerNode.tasksUsage.error).toBe(0) + expect(workerNode.tasksUsage).toStrictEqual({ + run: 0, + running: numberOfWorkers * 2, + runTime: 0, + runTimeHistory: expect.any(CircularArray), + avgRunTime: 0, + medRunTime: 0, + waitTime: 0, + waitTimeHistory: expect.any(CircularArray), + avgWaitTime: 0, + medWaitTime: 0, + error: 0 + }) } await Promise.all(promises) for (const workerNode of pool.workerNodes) { - expect(workerNode.tasksUsage).toBeDefined() - expect(workerNode.tasksUsage.run).toBe(numberOfWorkers * 2) - expect(workerNode.tasksUsage.running).toBe(0) - expect(workerNode.tasksUsage.runTime).toBeGreaterThanOrEqual(0) - expect(workerNode.tasksUsage.runTimeHistory).toBeInstanceOf(CircularArray) - expect(workerNode.tasksUsage.runTimeHistory.length).toBe(0) - expect(workerNode.tasksUsage.avgRunTime).toBeGreaterThanOrEqual(0) - expect(workerNode.tasksUsage.medRunTime).toBe(0) - expect(workerNode.tasksUsage.error).toBe(0) + expect(workerNode.tasksUsage).toStrictEqual({ + run: numberOfWorkers * 2, + running: 0, + runTime: 0, + runTimeHistory: expect.any(CircularArray), + avgRunTime: 0, + medRunTime: 0, + waitTime: 0, + waitTimeHistory: expect.any(CircularArray), + avgWaitTime: 0, + medWaitTime: 0, + error: 0 + }) } await pool.destroy() }) @@ -348,27 +377,39 @@ describe('Abstract pool test suite', () => { } await Promise.all(promises) for (const workerNode of pool.workerNodes) { - expect(workerNode.tasksUsage).toBeDefined() - expect(workerNode.tasksUsage.run).toBe(numberOfWorkers * 2) - expect(workerNode.tasksUsage.running).toBe(0) + expect(workerNode.tasksUsage).toStrictEqual({ + run: numberOfWorkers * 2, + running: 0, + runTime: expect.any(Number), + runTimeHistory: expect.any(CircularArray), + avgRunTime: 0, + medRunTime: 0, + waitTime: 0, + waitTimeHistory: expect.any(CircularArray), + avgWaitTime: 0, + medWaitTime: 0, + error: 0 + }) expect(workerNode.tasksUsage.runTime).toBeGreaterThanOrEqual(0) - expect(workerNode.tasksUsage.runTimeHistory).toBeInstanceOf(CircularArray) - expect(workerNode.tasksUsage.runTimeHistory.length).toBe(0) expect(workerNode.tasksUsage.avgRunTime).toBeGreaterThanOrEqual(0) - expect(workerNode.tasksUsage.medRunTime).toBe(0) - expect(workerNode.tasksUsage.error).toBe(0) } pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE) for (const workerNode of pool.workerNodes) { expect(workerNode.tasksUsage).toBeDefined() - expect(workerNode.tasksUsage.run).toBe(0) - expect(workerNode.tasksUsage.running).toBe(0) - expect(workerNode.tasksUsage.runTime).toBe(0) - expect(workerNode.tasksUsage.runTimeHistory).toBeInstanceOf(CircularArray) + expect(workerNode.tasksUsage).toStrictEqual({ + run: 0, + running: 0, + runTime: 0, + runTimeHistory: expect.any(CircularArray), + avgRunTime: 0, + medRunTime: 0, + waitTime: 0, + waitTimeHistory: expect.any(CircularArray), + avgWaitTime: 0, + medWaitTime: 0, + error: 0 + }) expect(workerNode.tasksUsage.runTimeHistory.length).toBe(0) - expect(workerNode.tasksUsage.avgRunTime).toBe(0) - expect(workerNode.tasksUsage.medRunTime).toBe(0) - expect(workerNode.tasksUsage.error).toBe(0) } await pool.destroy() }) diff --git a/tests/pools/selection-strategies/selection-strategies.test.js b/tests/pools/selection-strategies/selection-strategies.test.js index 9e3e99e3..8ab97158 100644 --- a/tests/pools/selection-strategies/selection-strategies.test.js +++ b/tests/pools/selection-strategies/selection-strategies.test.js @@ -5,6 +5,7 @@ const { FixedThreadPool, FixedClusterPool } = require('../../../lib') +const { CircularArray } = require('../../../lib/circular-array') describe('Selection strategies test suite', () => { const min = 0 @@ -118,14 +119,15 @@ describe('Selection strategies test suite', () => { { workerChoiceStrategy } ) expect( - pool.workerChoiceStrategyContext.getRequiredStatistics().runTime - ).toBe(false) - expect( - pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime - ).toBe(false) - expect( - pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime - ).toBe(false) + pool.workerChoiceStrategyContext.getRequiredStatistics() + ).toStrictEqual({ + runTime: false, + avgRunTime: false, + medRunTime: false, + waitTime: false, + avgWaitTime: false, + medWaitTime: false + }) await pool.destroy() pool = new DynamicThreadPool( min, @@ -134,14 +136,15 @@ describe('Selection strategies test suite', () => { { workerChoiceStrategy } ) expect( - pool.workerChoiceStrategyContext.getRequiredStatistics().runTime - ).toBe(false) - expect( - pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime - ).toBe(false) - expect( - pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime - ).toBe(false) + pool.workerChoiceStrategyContext.getRequiredStatistics() + ).toStrictEqual({ + runTime: false, + avgRunTime: false, + medRunTime: false, + waitTime: false, + avgWaitTime: false, + medWaitTime: false + }) // We need to clean up the resources after our test await pool.destroy() }) @@ -251,14 +254,15 @@ describe('Selection strategies test suite', () => { { workerChoiceStrategy } ) expect( - pool.workerChoiceStrategyContext.getRequiredStatistics().runTime - ).toBe(false) - expect( - pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime - ).toBe(false) - expect( - pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime - ).toBe(false) + pool.workerChoiceStrategyContext.getRequiredStatistics() + ).toStrictEqual({ + runTime: false, + avgRunTime: false, + medRunTime: false, + waitTime: false, + avgWaitTime: false, + medWaitTime: false + }) await pool.destroy() pool = new DynamicThreadPool( min, @@ -267,14 +271,15 @@ describe('Selection strategies test suite', () => { { workerChoiceStrategy } ) expect( - pool.workerChoiceStrategyContext.getRequiredStatistics().runTime - ).toBe(false) - expect( - pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime - ).toBe(false) - expect( - pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime - ).toBe(false) + pool.workerChoiceStrategyContext.getRequiredStatistics() + ).toStrictEqual({ + runTime: false, + avgRunTime: false, + medRunTime: false, + waitTime: false, + avgWaitTime: false, + medWaitTime: false + }) // We need to clean up the resources after our test await pool.destroy() }) @@ -318,14 +323,15 @@ describe('Selection strategies test suite', () => { { workerChoiceStrategy } ) expect( - pool.workerChoiceStrategyContext.getRequiredStatistics().runTime - ).toBe(true) - expect( - pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime - ).toBe(false) - expect( - pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime - ).toBe(false) + pool.workerChoiceStrategyContext.getRequiredStatistics() + ).toStrictEqual({ + runTime: true, + avgRunTime: false, + medRunTime: false, + waitTime: false, + avgWaitTime: false, + medWaitTime: false + }) await pool.destroy() pool = new DynamicThreadPool( min, @@ -334,14 +340,15 @@ describe('Selection strategies test suite', () => { { workerChoiceStrategy } ) expect( - pool.workerChoiceStrategyContext.getRequiredStatistics().runTime - ).toBe(true) - expect( - pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime - ).toBe(false) - expect( - pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime - ).toBe(false) + pool.workerChoiceStrategyContext.getRequiredStatistics() + ).toStrictEqual({ + runTime: true, + avgRunTime: false, + medRunTime: false, + waitTime: false, + avgWaitTime: false, + medWaitTime: false + }) // We need to clean up the resources after our test await pool.destroy() }) @@ -385,14 +392,15 @@ describe('Selection strategies test suite', () => { { workerChoiceStrategy } ) expect( - pool.workerChoiceStrategyContext.getRequiredStatistics().runTime - ).toBe(true) - expect( - pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime - ).toBe(true) - expect( - pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime - ).toBe(false) + pool.workerChoiceStrategyContext.getRequiredStatistics() + ).toStrictEqual({ + runTime: true, + avgRunTime: true, + medRunTime: false, + waitTime: false, + avgWaitTime: false, + medWaitTime: false + }) await pool.destroy() pool = new DynamicThreadPool( min, @@ -401,14 +409,15 @@ describe('Selection strategies test suite', () => { { workerChoiceStrategy } ) expect( - pool.workerChoiceStrategyContext.getRequiredStatistics().runTime - ).toBe(true) - expect( - pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime - ).toBe(true) - expect( - pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime - ).toBe(false) + pool.workerChoiceStrategyContext.getRequiredStatistics() + ).toStrictEqual({ + runTime: true, + avgRunTime: true, + medRunTime: false, + waitTime: false, + avgWaitTime: false, + medWaitTime: false + }) // We need to clean up the resources after our test await pool.destroy() }) @@ -425,10 +434,20 @@ describe('Selection strategies test suite', () => { await pool.execute() } for (const workerNode of pool.workerNodes) { - expect(workerNode.tasksUsage.avgRunTime).toBeDefined() + expect(workerNode.tasksUsage).toStrictEqual({ + run: maxMultiplier, + running: 0, + runTime: expect.any(Number), + runTimeHistory: expect.any(CircularArray), + avgRunTime: expect.any(Number), + medRunTime: 0, + waitTime: 0, + waitTimeHistory: expect.any(CircularArray), + avgWaitTime: 0, + medWaitTime: 0, + error: 0 + }) expect(workerNode.tasksUsage.avgRunTime).toBeGreaterThanOrEqual(0) - expect(workerNode.tasksUsage.medRunTime).toBeDefined() - expect(workerNode.tasksUsage.medRunTime).toBe(0) } expect( pool.workerChoiceStrategyContext.workerChoiceStrategies.get( @@ -452,10 +471,20 @@ describe('Selection strategies test suite', () => { await pool.execute() } for (const workerNode of pool.workerNodes) { - expect(workerNode.tasksUsage.avgRunTime).toBeDefined() + expect(workerNode.tasksUsage).toStrictEqual({ + run: max * maxMultiplier, + running: 0, + runTime: expect.any(Number), + runTimeHistory: expect.any(CircularArray), + avgRunTime: expect.any(Number), + medRunTime: 0, + waitTime: 0, + waitTimeHistory: expect.any(CircularArray), + avgWaitTime: 0, + medWaitTime: 0, + error: 0 + }) expect(workerNode.tasksUsage.avgRunTime).toBeGreaterThanOrEqual(0) - expect(workerNode.tasksUsage.medRunTime).toBeDefined() - expect(workerNode.tasksUsage.medRunTime).toBe(0) } expect( pool.workerChoiceStrategyContext.workerChoiceStrategies.get( @@ -484,9 +513,19 @@ describe('Selection strategies test suite', () => { await pool.execute() } for (const workerNode of pool.workerNodes) { - expect(workerNode.tasksUsage.avgRunTime).toBeDefined() - expect(workerNode.tasksUsage.avgRunTime).toBe(0) - expect(workerNode.tasksUsage.medRunTime).toBeDefined() + expect(workerNode.tasksUsage).toStrictEqual({ + run: max * maxMultiplier, + running: 0, + runTime: expect.any(Number), + runTimeHistory: expect.any(CircularArray), + avgRunTime: 0, + medRunTime: expect.any(Number), + waitTime: 0, + waitTimeHistory: expect.any(CircularArray), + avgWaitTime: 0, + medWaitTime: 0, + error: 0 + }) expect(workerNode.tasksUsage.medRunTime).toBeGreaterThanOrEqual(0) } expect( @@ -580,14 +619,15 @@ describe('Selection strategies test suite', () => { { workerChoiceStrategy } ) expect( - pool.workerChoiceStrategyContext.getRequiredStatistics().runTime - ).toBe(true) - expect( - pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime - ).toBe(true) - expect( - pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime - ).toBe(false) + pool.workerChoiceStrategyContext.getRequiredStatistics() + ).toStrictEqual({ + runTime: true, + avgRunTime: true, + medRunTime: false, + waitTime: false, + avgWaitTime: false, + medWaitTime: false + }) await pool.destroy() pool = new DynamicThreadPool( min, @@ -596,14 +636,15 @@ describe('Selection strategies test suite', () => { { workerChoiceStrategy } ) expect( - pool.workerChoiceStrategyContext.getRequiredStatistics().runTime - ).toBe(true) - expect( - pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime - ).toBe(true) - expect( - pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime - ).toBe(false) + pool.workerChoiceStrategyContext.getRequiredStatistics() + ).toStrictEqual({ + runTime: true, + avgRunTime: true, + medRunTime: false, + waitTime: false, + avgWaitTime: false, + medWaitTime: false + }) // We need to clean up the resources after our test await pool.destroy() }) @@ -620,10 +661,20 @@ describe('Selection strategies test suite', () => { await pool.execute() } for (const workerNode of pool.workerNodes) { - expect(workerNode.tasksUsage.avgRunTime).toBeDefined() + expect(workerNode.tasksUsage).toStrictEqual({ + run: max * maxMultiplier, + running: 0, + runTime: expect.any(Number), + runTimeHistory: expect.any(CircularArray), + avgRunTime: expect.any(Number), + medRunTime: 0, + waitTime: 0, + waitTimeHistory: expect.any(CircularArray), + avgWaitTime: 0, + medWaitTime: 0, + error: 0 + }) expect(workerNode.tasksUsage.avgRunTime).toBeGreaterThanOrEqual(0) - expect(workerNode.tasksUsage.medRunTime).toBeDefined() - expect(workerNode.tasksUsage.medRunTime).toBe(0) } expect( pool.workerChoiceStrategyContext.workerChoiceStrategies.get( @@ -652,10 +703,20 @@ describe('Selection strategies test suite', () => { await pool.execute() } for (const workerNode of pool.workerNodes) { - expect(workerNode.tasksUsage.avgRunTime).toBeDefined() + expect(workerNode.tasksUsage).toStrictEqual({ + run: max * maxMultiplier, + running: 0, + runTime: expect.any(Number), + runTimeHistory: expect.any(CircularArray), + avgRunTime: expect.any(Number), + medRunTime: 0, + waitTime: 0, + waitTimeHistory: expect.any(CircularArray), + avgWaitTime: 0, + medWaitTime: 0, + error: 0 + }) expect(workerNode.tasksUsage.avgRunTime).toBeGreaterThanOrEqual(0) - expect(workerNode.tasksUsage.medRunTime).toBeDefined() - expect(workerNode.tasksUsage.medRunTime).toBe(0) } expect( pool.workerChoiceStrategyContext.workerChoiceStrategies.get( @@ -689,9 +750,19 @@ describe('Selection strategies test suite', () => { await pool.execute() } for (const workerNode of pool.workerNodes) { - expect(workerNode.tasksUsage.avgRunTime).toBeDefined() - expect(workerNode.tasksUsage.avgRunTime).toBe(0) - expect(workerNode.tasksUsage.medRunTime).toBeDefined() + expect(workerNode.tasksUsage).toStrictEqual({ + run: max * maxMultiplier, + running: 0, + runTime: expect.any(Number), + runTimeHistory: expect.any(CircularArray), + avgRunTime: 0, + medRunTime: expect.any(Number), + waitTime: 0, + waitTimeHistory: expect.any(CircularArray), + avgWaitTime: 0, + medWaitTime: 0, + error: 0 + }) expect(workerNode.tasksUsage.medRunTime).toBeGreaterThanOrEqual(0) } expect( -- 2.34.1