refactor: more worker function -> task function renaming
authorJérôme Benoit <jerome.benoit@sap.com>
Wed, 2 Aug 2023 10:01:39 +0000 (12:01 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Wed, 2 Aug 2023 10:01:39 +0000 (12:01 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
23 files changed:
benchmarks/README.md
benchmarks/benchmarks-types.mjs
benchmarks/benchmarks-utils.mjs
benchmarks/internal/bench.mjs
benchmarks/internal/cluster-worker.mjs
benchmarks/internal/thread-worker.mjs
benchmarks/versus-external-pools/functions/function-to-bench.js
tests/pools/cluster/dynamic.test.js
tests/pools/cluster/fixed.test.js
tests/pools/thread/dynamic.test.js
tests/pools/thread/fixed.test.js
tests/test-types.js
tests/test-utils.js
tests/worker-files/cluster/asyncErrorWorker.js
tests/worker-files/cluster/asyncWorker.js
tests/worker-files/cluster/longRunningWorkerHardBehavior.js
tests/worker-files/cluster/longRunningWorkerSoftBehavior.js
tests/worker-files/cluster/testWorker.js
tests/worker-files/thread/asyncErrorWorker.js
tests/worker-files/thread/asyncWorker.js
tests/worker-files/thread/longRunningWorkerHardBehavior.js
tests/worker-files/thread/longRunningWorkerSoftBehavior.js
tests/worker-files/thread/testWorker.js

index e47bb2b28b3493c9f570364b3e5632f25accb523..4ea6072d20543bc5f6e0b49a7a7bb8538ed396ea 100644 (file)
@@ -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:
 
-  <!-- - [node-worker-threads-pool](https://github.com/SUCHMOKUO/node-worker-threads-pool): removed because it does not support dynamic modules import or import outside the worker function. The worker function is expected to be self-contained, which makes it difficult to use in real world application without ugly hacks. -->
+  <!-- - [node-worker-threads-pool](https://github.com/SUCHMOKUO/node-worker-threads-pool): removed because it does not support dynamic modules import or import outside the task function. The task function is expected to be self-contained, which makes it difficult to use in real world application without ugly hacks. -->
 
   - [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.
index 3287cc3c85d377a91db2464a2d9fe84ecc6bd2a4..69ccce0dc771eb482a1d2bb1a81cc29ef4331f10 100644 (file)
@@ -1,4 +1,4 @@
-export const WorkerFunctions = {
+export const TaskFunctions = {
   jsonIntegerSerialization: 'jsonIntegerSerialization',
   fibonacci: 'fibonacci',
   factorial: 'factorial',
index 48c8fc3e7cb7dacd019a4ca3ac8fa28749c61db5..259fca5696b0a32c62c040966196ed78f052d9ca 100644 (file)
@@ -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')
   }
 }
 
index 8235d5727b9e0043c0948fd4dfbbb8249a57e05c..2749da428bfcda15d881b0f934d35b54f8a603f2 100644 (file)
@@ -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 =>
index 2ec78054e655f25f181e888c0f90b6bd28ba4b66..efbcf7b1cfd0bbf60c126f8f5af716b3c6a7dd45 100644 (file)
@@ -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)
index 88d57670f712bae8528724dfeef28d310eb29f65..e56eaa4cbcbb7cb0844439d3efd489278be15755 100644 (file)
@@ -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)
index 6a05cb629ad7bdcc816d393215bb6ae5d80a48d5..5c63c794df69f26a61c12c8c04e8e3a18b0805ab 100644 (file)
@@ -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.
  */
index 7c24131b0624d845f44c8d24c16f93d50382696b..65dff559ab1618fb2395505fa055ea935821a6ae 100644 (file)
@@ -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)
   })
index 1f4337e423de3b1879f8b6b7865701eeb672711a..335e6d131a9edf726e022120a197a062cc75374a 100644 (file)
@@ -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)
   })
index b5ba62bb07a1b1abb06bee6bbfb411758f9b455a..ac926a927a2c22f11a8b82c95785c8658210cfe5 100644 (file)
@@ -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)
   })
index f38a56c60a52e2e5c65909480e02c6e17707320a..080ebad6ec8fa19b682bff11ae0f6634802c393e 100644 (file)
@@ -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)
   })
index 913803437597b46556f26e5dae46159b16dc7627..402aa305b54e163261a7dfc932aa9b0abf3ba5c1 100644 (file)
@@ -1,7 +1,7 @@
-const WorkerFunctions = {
+const TaskFunctions = {
   jsonIntegerSerialization: 'jsonIntegerSerialization',
   fibonacci: 'fibonacci',
   factorial: 'factorial'
 }
 
-module.exports = { WorkerFunctions }
+module.exports = { TaskFunctions }
index 67d10e4a93e690c3cd70f6fc3d2e63641f91ff83..b55f76215e3d8f651281148eb8c57be59e7cc9fb 100644 (file)
@@ -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
 }
index 0536e83b46601e9b7d924b1706aa18415bc9f340..028a7cf3a77a97689442576fb40d6457c623df8c 100644 (file)
@@ -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,
index 2460c20da272b999f71fce46593e33da4dc8eab6..b03a505486e4af776ba12c830c53fb80ad68f221 100644 (file)
@@ -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, {
index e308eb5ce3923306c36b71d458f013733bd72f56..f2c0c1e40bd1c23671aa62679a0475b08e6c2fb6 100644 (file)
@@ -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, {
index 208e5ba2d46da64e9d1418f68e0f10abe8a54deb..cd057ef06562455a2b30a93140ba23efa404c568 100644 (file)
@@ -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, {
index 25774bb239a084526fc1b833807c4ea2593d0ab3..8462307cd8826a5b7328ea46405c84a0fbbaf00d 100644 (file)
@@ -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, {
index 6bc0ae6b3b19226c557ce3455fe068b7b21e8bc4..c73fd2919230f03234726bba99e957e86596206b 100644 (file)
@@ -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,
index 5e3ec257f6b594b1d2cf49cccd09e36d8d2bb61a..2b834b87fd33d9002bd664707159581824f90301 100644 (file)
@@ -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, {
index d81f7f9020c1500d715a8ac5077c91c6e0334701..daa381edb226803d905f19314a31530dc357532b 100644 (file)
@@ -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, {
index eda5a4059643c3efd092a9e269ad54b18fc8ecd7..3b90236c97cbdb61677fb2b4aae4bb08fadc8ca6 100644 (file)
@@ -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, {
index 5779e231d0903c49edb0d85bd225e8ff1b1027ef..64e7921acd3d8fb4a7f2e031c32b92100980396e 100644 (file)
@@ -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, {