From dbca3be954130834defeb4084096272ecf660c5c Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 2 Aug 2023 12:01:39 +0200 Subject: [PATCH] refactor: more worker function -> task function renaming MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- benchmarks/README.md | 2 +- benchmarks/benchmarks-types.mjs | 2 +- benchmarks/benchmarks-utils.mjs | 14 +++++++------- benchmarks/internal/bench.mjs | 4 ++-- benchmarks/internal/cluster-worker.mjs | 12 ++++++------ benchmarks/internal/thread-worker.mjs | 12 ++++++------ .../functions/function-to-bench.js | 4 ++-- tests/pools/cluster/dynamic.test.js | 6 +++--- tests/pools/cluster/fixed.test.js | 6 +++--- tests/pools/thread/dynamic.test.js | 6 +++--- tests/pools/thread/fixed.test.js | 6 +++--- tests/test-types.js | 4 ++-- tests/test-utils.js | 16 ++++++++-------- tests/worker-files/cluster/asyncErrorWorker.js | 4 ++-- tests/worker-files/cluster/asyncWorker.js | 4 ++-- .../cluster/longRunningWorkerHardBehavior.js | 4 ++-- .../cluster/longRunningWorkerSoftBehavior.js | 4 ++-- tests/worker-files/cluster/testWorker.js | 8 ++++---- tests/worker-files/thread/asyncErrorWorker.js | 4 ++-- tests/worker-files/thread/asyncWorker.js | 4 ++-- .../thread/longRunningWorkerHardBehavior.js | 4 ++-- .../thread/longRunningWorkerSoftBehavior.js | 4 ++-- tests/worker-files/thread/testWorker.js | 8 ++++---- 23 files changed, 71 insertions(+), 71 deletions(-) diff --git a/benchmarks/README.md b/benchmarks/README.md index e47bb2b2..4ea6072d 100644 --- a/benchmarks/README.md +++ b/benchmarks/README.md @@ -27,7 +27,7 @@ We chose to use this tool because it allows to run isolated Node.js processes so - External pools with which we used to compare the poolifier results: - + - [worker-threads-pool](https://github.com/watson/worker-threads-pool): removed because unmaintained since more than 4 years. - [threadwork](https://github.com/kevlened/threadwork): removed because unmaintained since more than 3 years. diff --git a/benchmarks/benchmarks-types.mjs b/benchmarks/benchmarks-types.mjs index 3287cc3c..69ccce0d 100644 --- a/benchmarks/benchmarks-types.mjs +++ b/benchmarks/benchmarks-types.mjs @@ -1,4 +1,4 @@ -export const WorkerFunctions = { +export const TaskFunctions = { jsonIntegerSerialization: 'jsonIntegerSerialization', fibonacci: 'fibonacci', factorial: 'factorial', diff --git a/benchmarks/benchmarks-utils.mjs b/benchmarks/benchmarks-utils.mjs index 48c8fc3e..259fca56 100644 --- a/benchmarks/benchmarks-utils.mjs +++ b/benchmarks/benchmarks-utils.mjs @@ -8,7 +8,7 @@ import { PoolTypes, WorkerTypes } from '../lib/index.mjs' -import { WorkerFunctions } from './benchmarks-types.mjs' +import { TaskFunctions } from './benchmarks-types.mjs' export const runTest = async (pool, { taskExecutions, workerData }) => { return new Promise((resolve, reject) => { @@ -100,18 +100,18 @@ const readWriteFiles = ( return { ok: 1 } } -export const executeWorkerFunction = data => { +export const executeTaskFunction = data => { switch (data.function) { - case WorkerFunctions.jsonIntegerSerialization: + case TaskFunctions.jsonIntegerSerialization: return jsonIntegerSerialization(data.taskSize || 1000) - case WorkerFunctions.fibonacci: + case TaskFunctions.fibonacci: return fibonacci(data.taskSize || 1000) - case WorkerFunctions.factorial: + case TaskFunctions.factorial: return factorial(data.taskSize || 1000) - case WorkerFunctions.readWriteFiles: + case TaskFunctions.readWriteFiles: return readWriteFiles(data.taskSize || 1000) default: - throw new Error('Unknown worker function') + throw new Error('Unknown task function') } } diff --git a/benchmarks/internal/bench.mjs b/benchmarks/internal/bench.mjs index 8235d572..2749da42 100644 --- a/benchmarks/internal/bench.mjs +++ b/benchmarks/internal/bench.mjs @@ -6,7 +6,7 @@ import { WorkerTypes, availableParallelism } from '../../lib/index.mjs' -import { WorkerFunctions } from '../benchmarks-types.mjs' +import { TaskFunctions } from '../benchmarks-types.mjs' import { buildPool, runTest } from '../benchmarks-utils.mjs' const poolSize = availableParallelism() @@ -47,7 +47,7 @@ for (const poolType of Object.values(PoolTypes)) { const taskExecutions = 1 const workerData = { - function: WorkerFunctions.jsonIntegerSerialization, + function: TaskFunctions.jsonIntegerSerialization, taskSize: 1000 } const addPools = pools => diff --git a/benchmarks/internal/cluster-worker.mjs b/benchmarks/internal/cluster-worker.mjs index 2ec78054..efbcf7b1 100644 --- a/benchmarks/internal/cluster-worker.mjs +++ b/benchmarks/internal/cluster-worker.mjs @@ -1,16 +1,16 @@ import { isMaster } from 'cluster' import { ClusterWorker } from '../../lib/index.mjs' -import { executeWorkerFunction } from '../benchmarks-utils.mjs' -import { WorkerFunctions } from '../benchmarks-types.mjs' +import { executeTaskFunction } from '../benchmarks-utils.mjs' +import { TaskFunctions } from '../benchmarks-types.mjs' const debug = false -function workerFunction (data) { +function taskFunction (data) { data = data || {} - data.function = data.function || WorkerFunctions.jsonIntegerSerialization - const res = executeWorkerFunction(data) + data.function = data.function || TaskFunctions.jsonIntegerSerialization + const res = executeTaskFunction(data) debug === true && console.debug('This is the main thread ' + isMaster) return res } -export default new ClusterWorker(workerFunction) +export default new ClusterWorker(taskFunction) diff --git a/benchmarks/internal/thread-worker.mjs b/benchmarks/internal/thread-worker.mjs index 88d57670..e56eaa4c 100644 --- a/benchmarks/internal/thread-worker.mjs +++ b/benchmarks/internal/thread-worker.mjs @@ -1,16 +1,16 @@ import { isMainThread } from 'worker_threads' import { ThreadWorker } from '../../lib/index.mjs' -import { executeWorkerFunction } from '../benchmarks-utils.mjs' -import { WorkerFunctions } from '../benchmarks-types.mjs' +import { executeTaskFunction } from '../benchmarks-utils.mjs' +import { TaskFunctions } from '../benchmarks-types.mjs' const debug = false -function workerFunction (data) { +function taskFunction (data) { data = data || {} - data.function = data.function || WorkerFunctions.jsonIntegerSerialization - const res = executeWorkerFunction(data) + data.function = data.function || TaskFunctions.jsonIntegerSerialization + const res = executeTaskFunction(data) debug === true && console.debug('This is the main thread ' + isMainThread) return res } -export default new ThreadWorker(workerFunction) +export default new ThreadWorker(taskFunction) diff --git a/benchmarks/versus-external-pools/functions/function-to-bench.js b/benchmarks/versus-external-pools/functions/function-to-bench.js index 6a05cb62..5c63c794 100644 --- a/benchmarks/versus-external-pools/functions/function-to-bench.js +++ b/benchmarks/versus-external-pools/functions/function-to-bench.js @@ -1,7 +1,7 @@ 'use strict' /** - * The worker function to execute during pools benchmarks. - * NOTE: This function requires to be self-contained, thread-safe and re-entrant. + * The task function to execute during pools benchmarks. + * NOTE: This function requires to be self-contained, thread-safe and re-entrant (node-worker-threads-pool requirement). * @param {*} data The worker data. * @returns {*} The result. */ diff --git a/tests/pools/cluster/dynamic.test.js b/tests/pools/cluster/dynamic.test.js index 7c24131b..65dff559 100644 --- a/tests/pools/cluster/dynamic.test.js +++ b/tests/pools/cluster/dynamic.test.js @@ -1,6 +1,6 @@ const { expect } = require('expect') const { DynamicClusterPool, PoolEvents } = require('../../../lib') -const { WorkerFunctions } = require('../../test-types') +const { TaskFunctions } = require('../../test-types') const { sleep, waitWorkerEvents } = require('../../test-utils') describe('Dynamic cluster pool test suite', () => { @@ -17,11 +17,11 @@ describe('Dynamic cluster pool test suite', () => { it('Verify that the function is executed in a worker cluster', async () => { let result = await pool.execute({ - function: WorkerFunctions.fibonacci + function: TaskFunctions.fibonacci }) expect(result).toBe(75025) result = await pool.execute({ - function: WorkerFunctions.factorial + function: TaskFunctions.factorial }) expect(result).toBe(9.33262154439441e157) }) diff --git a/tests/pools/cluster/fixed.test.js b/tests/pools/cluster/fixed.test.js index 1f4337e4..335e6d13 100644 --- a/tests/pools/cluster/fixed.test.js +++ b/tests/pools/cluster/fixed.test.js @@ -1,6 +1,6 @@ const { expect } = require('expect') const { FixedClusterPool, PoolEvents } = require('../../../lib') -const { WorkerFunctions } = require('../../test-types') +const { TaskFunctions } = require('../../test-types') const { waitPoolEvents, waitWorkerEvents } = require('../../test-utils') describe('Fixed cluster pool test suite', () => { @@ -64,11 +64,11 @@ describe('Fixed cluster pool test suite', () => { it('Verify that the function is executed in a worker cluster', async () => { let result = await pool.execute({ - function: WorkerFunctions.fibonacci + function: TaskFunctions.fibonacci }) expect(result).toBe(75025) result = await pool.execute({ - function: WorkerFunctions.factorial + function: TaskFunctions.factorial }) expect(result).toBe(9.33262154439441e157) }) diff --git a/tests/pools/thread/dynamic.test.js b/tests/pools/thread/dynamic.test.js index b5ba62bb..ac926a92 100644 --- a/tests/pools/thread/dynamic.test.js +++ b/tests/pools/thread/dynamic.test.js @@ -1,6 +1,6 @@ const { expect } = require('expect') const { DynamicThreadPool, PoolEvents } = require('../../../lib') -const { WorkerFunctions } = require('../../test-types') +const { TaskFunctions } = require('../../test-types') const { sleep, waitWorkerEvents } = require('../../test-utils') describe('Dynamic thread pool test suite', () => { @@ -17,11 +17,11 @@ describe('Dynamic thread pool test suite', () => { it('Verify that the function is executed in a worker thread', async () => { let result = await pool.execute({ - function: WorkerFunctions.fibonacci + function: TaskFunctions.fibonacci }) expect(result).toBe(75025) result = await pool.execute({ - function: WorkerFunctions.factorial + function: TaskFunctions.factorial }) expect(result).toBe(9.33262154439441e157) }) diff --git a/tests/pools/thread/fixed.test.js b/tests/pools/thread/fixed.test.js index f38a56c6..080ebad6 100644 --- a/tests/pools/thread/fixed.test.js +++ b/tests/pools/thread/fixed.test.js @@ -1,6 +1,6 @@ const { expect } = require('expect') const { FixedThreadPool, PoolEvents } = require('../../../lib') -const { WorkerFunctions } = require('../../test-types') +const { TaskFunctions } = require('../../test-types') const { waitPoolEvents, waitWorkerEvents } = require('../../test-utils') describe('Fixed thread pool test suite', () => { @@ -64,11 +64,11 @@ describe('Fixed thread pool test suite', () => { it('Verify that the function is executed in a worker thread', async () => { let result = await pool.execute({ - function: WorkerFunctions.fibonacci + function: TaskFunctions.fibonacci }) expect(result).toBe(75025) result = await pool.execute({ - function: WorkerFunctions.factorial + function: TaskFunctions.factorial }) expect(result).toBe(9.33262154439441e157) }) diff --git a/tests/test-types.js b/tests/test-types.js index 91380343..402aa305 100644 --- a/tests/test-types.js +++ b/tests/test-types.js @@ -1,7 +1,7 @@ -const WorkerFunctions = { +const TaskFunctions = { jsonIntegerSerialization: 'jsonIntegerSerialization', fibonacci: 'fibonacci', factorial: 'factorial' } -module.exports = { WorkerFunctions } +module.exports = { TaskFunctions } diff --git a/tests/test-utils.js b/tests/test-utils.js index 67d10e4a..b55f7621 100644 --- a/tests/test-utils.js +++ b/tests/test-utils.js @@ -1,4 +1,4 @@ -const { WorkerFunctions } = require('./test-types') +const { TaskFunctions } = require('./test-types') const waitWorkerEvents = async (pool, workerEvent, numberOfEventsToWait) => { return new Promise(resolve => { @@ -36,7 +36,7 @@ const sleep = async ms => { return new Promise(resolve => setTimeout(resolve, ms)) } -const sleepWorkerFunction = async ( +const sleepTaskFunction = async ( data, ms, rejection = false, @@ -97,13 +97,13 @@ const factorial = n => { return factorial(n - 1) * n } -const executeWorkerFunction = data => { +const executeTaskFunction = data => { switch (data.function) { - case WorkerFunctions.jsonIntegerSerialization: + case TaskFunctions.jsonIntegerSerialization: return jsonIntegerSerialization(data.n || 100) - case WorkerFunctions.fibonacci: + case TaskFunctions.fibonacci: return fibonacci(data.n || 25) - case WorkerFunctions.factorial: + case TaskFunctions.factorial: return factorial(data.n || 100) default: throw new Error('Unknown worker function') @@ -111,13 +111,13 @@ const executeWorkerFunction = data => { } module.exports = { - executeWorkerFunction, + executeTaskFunction, factorial, fibonacci, generateRandomInteger, jsonIntegerSerialization, sleep, - sleepWorkerFunction, + sleepTaskFunction, waitPoolEvents, waitWorkerEvents } diff --git a/tests/worker-files/cluster/asyncErrorWorker.js b/tests/worker-files/cluster/asyncErrorWorker.js index 0536e83b..028a7cf3 100644 --- a/tests/worker-files/cluster/asyncErrorWorker.js +++ b/tests/worker-files/cluster/asyncErrorWorker.js @@ -1,9 +1,9 @@ 'use strict' const { ClusterWorker, KillBehaviors } = require('../../../lib') -const { sleepWorkerFunction } = require('../../test-utils') +const { sleepTaskFunction } = require('../../test-utils') async function error (data) { - return sleepWorkerFunction( + return sleepTaskFunction( data, 2000, true, diff --git a/tests/worker-files/cluster/asyncWorker.js b/tests/worker-files/cluster/asyncWorker.js index 2460c20d..b03a5054 100644 --- a/tests/worker-files/cluster/asyncWorker.js +++ b/tests/worker-files/cluster/asyncWorker.js @@ -1,9 +1,9 @@ 'use strict' const { ClusterWorker, KillBehaviors } = require('../../../lib') -const { sleepWorkerFunction } = require('../../test-utils') +const { sleepTaskFunction } = require('../../test-utils') async function sleep (data) { - return sleepWorkerFunction(data, 2000) + return sleepTaskFunction(data, 2000) } module.exports = new ClusterWorker(sleep, { diff --git a/tests/worker-files/cluster/longRunningWorkerHardBehavior.js b/tests/worker-files/cluster/longRunningWorkerHardBehavior.js index e308eb5c..f2c0c1e4 100644 --- a/tests/worker-files/cluster/longRunningWorkerHardBehavior.js +++ b/tests/worker-files/cluster/longRunningWorkerHardBehavior.js @@ -1,9 +1,9 @@ 'use strict' const { ClusterWorker, KillBehaviors } = require('../../../lib') -const { sleepWorkerFunction } = require('../../test-utils') +const { sleepTaskFunction } = require('../../test-utils') async function sleep (data) { - return sleepWorkerFunction(data, 50000) + return sleepTaskFunction(data, 50000) } module.exports = new ClusterWorker(sleep, { diff --git a/tests/worker-files/cluster/longRunningWorkerSoftBehavior.js b/tests/worker-files/cluster/longRunningWorkerSoftBehavior.js index 208e5ba2..cd057ef0 100644 --- a/tests/worker-files/cluster/longRunningWorkerSoftBehavior.js +++ b/tests/worker-files/cluster/longRunningWorkerSoftBehavior.js @@ -1,9 +1,9 @@ 'use strict' const { ClusterWorker } = require('../../../lib') -const { sleepWorkerFunction } = require('../../test-utils') +const { sleepTaskFunction } = require('../../test-utils') async function sleep (data) { - return sleepWorkerFunction(data, 50000) + return sleepTaskFunction(data, 50000) } module.exports = new ClusterWorker(sleep, { diff --git a/tests/worker-files/cluster/testWorker.js b/tests/worker-files/cluster/testWorker.js index 25774bb2..8462307c 100644 --- a/tests/worker-files/cluster/testWorker.js +++ b/tests/worker-files/cluster/testWorker.js @@ -1,12 +1,12 @@ 'use strict' const { ClusterWorker, KillBehaviors } = require('../../../lib') -const { executeWorkerFunction } = require('../../test-utils') -const { WorkerFunctions } = require('../../test-types') +const { executeTaskFunction } = require('../../test-utils') +const { TaskFunctions } = require('../../test-types') function test (data) { data = data || {} - data.function = data.function || WorkerFunctions.jsonIntegerSerialization - return executeWorkerFunction(data) + data.function = data.function || TaskFunctions.jsonIntegerSerialization + return executeTaskFunction(data) } module.exports = new ClusterWorker(test, { diff --git a/tests/worker-files/thread/asyncErrorWorker.js b/tests/worker-files/thread/asyncErrorWorker.js index 6bc0ae6b..c73fd291 100644 --- a/tests/worker-files/thread/asyncErrorWorker.js +++ b/tests/worker-files/thread/asyncErrorWorker.js @@ -1,9 +1,9 @@ 'use strict' const { ThreadWorker, KillBehaviors } = require('../../../lib') -const { sleepWorkerFunction } = require('../../test-utils') +const { sleepTaskFunction } = require('../../test-utils') async function error (data) { - return sleepWorkerFunction( + return sleepTaskFunction( data, 2000, true, diff --git a/tests/worker-files/thread/asyncWorker.js b/tests/worker-files/thread/asyncWorker.js index 5e3ec257..2b834b87 100644 --- a/tests/worker-files/thread/asyncWorker.js +++ b/tests/worker-files/thread/asyncWorker.js @@ -1,9 +1,9 @@ 'use strict' const { ThreadWorker, KillBehaviors } = require('../../../lib') -const { sleepWorkerFunction } = require('../../test-utils') +const { sleepTaskFunction } = require('../../test-utils') async function sleep (data) { - return sleepWorkerFunction(data, 2000) + return sleepTaskFunction(data, 2000) } module.exports = new ThreadWorker(sleep, { diff --git a/tests/worker-files/thread/longRunningWorkerHardBehavior.js b/tests/worker-files/thread/longRunningWorkerHardBehavior.js index d81f7f90..daa381ed 100644 --- a/tests/worker-files/thread/longRunningWorkerHardBehavior.js +++ b/tests/worker-files/thread/longRunningWorkerHardBehavior.js @@ -1,9 +1,9 @@ 'use strict' const { ThreadWorker, KillBehaviors } = require('../../../lib') -const { sleepWorkerFunction } = require('../../test-utils') +const { sleepTaskFunction } = require('../../test-utils') async function sleep (data) { - return sleepWorkerFunction(data, 50000) + return sleepTaskFunction(data, 50000) } module.exports = new ThreadWorker(sleep, { diff --git a/tests/worker-files/thread/longRunningWorkerSoftBehavior.js b/tests/worker-files/thread/longRunningWorkerSoftBehavior.js index eda5a405..3b90236c 100644 --- a/tests/worker-files/thread/longRunningWorkerSoftBehavior.js +++ b/tests/worker-files/thread/longRunningWorkerSoftBehavior.js @@ -1,9 +1,9 @@ 'use strict' const { ThreadWorker } = require('../../../lib') -const { sleepWorkerFunction } = require('../../test-utils') +const { sleepTaskFunction } = require('../../test-utils') async function sleep (data) { - return sleepWorkerFunction(data, 50000) + return sleepTaskFunction(data, 50000) } module.exports = new ThreadWorker(sleep, { diff --git a/tests/worker-files/thread/testWorker.js b/tests/worker-files/thread/testWorker.js index 5779e231..64e7921a 100644 --- a/tests/worker-files/thread/testWorker.js +++ b/tests/worker-files/thread/testWorker.js @@ -1,12 +1,12 @@ 'use strict' const { ThreadWorker, KillBehaviors } = require('../../../lib') -const { executeWorkerFunction } = require('../../test-utils') -const { WorkerFunctions } = require('../../test-types') +const { executeTaskFunction } = require('../../test-utils') +const { TaskFunctions } = require('../../test-types') function test (data) { data = data || {} - data.function = data.function || WorkerFunctions.jsonIntegerSerialization - return executeWorkerFunction(data) + data.function = data.function || TaskFunctions.jsonIntegerSerialization + return executeTaskFunction(data) } module.exports = new ThreadWorker(test, { -- 2.34.1