refactor: convert internal benchmark code to ESM
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 25 Jun 2023 14:00:16 +0000 (16:00 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sun, 25 Jun 2023 14:00:16 +0000 (16:00 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
benchmarks/benchmarks-utils.js
benchmarks/internal/bench.mjs [moved from benchmarks/internal/bench.js with 97% similarity]
benchmarks/internal/cluster-worker.mjs [moved from benchmarks/internal/cluster-worker.js with 50% similarity]
benchmarks/internal/thread-worker.js [deleted file]
benchmarks/internal/thread-worker.mjs [new file with mode: 0644]
package.json
src/worker/abstract-worker.ts

index eb3a4f945f40a64a95e010fe375b81c4bb874090..6195a983c3fe98da9e8f68e06dbc96818773c1fc 100644 (file)
@@ -119,13 +119,13 @@ function buildPool (workerType, poolType, poolSize, poolOptions) {
         case WorkerTypes.thread:
           return new FixedThreadPool(
             poolSize,
-            './benchmarks/internal/thread-worker.js',
+            './benchmarks/internal/thread-worker.mjs',
             poolOptions
           )
         case WorkerTypes.cluster:
           return new FixedClusterPool(
             poolSize,
-            './benchmarks/internal/cluster-worker.js',
+            './benchmarks/internal/cluster-worker.mjs',
             poolOptions
           )
       }
@@ -136,14 +136,14 @@ function buildPool (workerType, poolType, poolSize, poolOptions) {
           return new DynamicThreadPool(
             poolSize / 2,
             poolSize * 3,
-            './benchmarks/internal/thread-worker.js',
+            './benchmarks/internal/thread-worker.mjs',
             poolOptions
           )
         case WorkerTypes.cluster:
           return new DynamicClusterPool(
             poolSize / 2,
             poolSize * 3,
-            './benchmarks/internal/cluster-worker.js',
+            './benchmarks/internal/cluster-worker.mjs',
             poolOptions
           )
       }
similarity index 97%
rename from benchmarks/internal/bench.js
rename to benchmarks/internal/bench.mjs
index 627bd0ed48d6e12f3c6ecda13ed3aad0a88990ba..6fad24c76a85f1878a95c843eadcefc1fbcc8977 100644 (file)
@@ -1,11 +1,7 @@
-const Benchmark = require('benny')
-const { WorkerChoiceStrategies } = require('../../lib')
-const {
-  PoolTypes,
-  WorkerFunctions,
-  WorkerTypes
-} = require('../benchmarks-types')
-const { buildPool, runTest } = require('../benchmarks-utils')
+import Benchmark from 'benny'
+import { WorkerChoiceStrategies } from '../../lib/index.mjs'
+import { PoolTypes, WorkerFunctions, WorkerTypes } from '../benchmarks-types.js'
+import { buildPool, runTest } from '../benchmarks-utils.js'
 
 const poolSize = 30
 const taskExecutions = 1
similarity index 50%
rename from benchmarks/internal/cluster-worker.js
rename to benchmarks/internal/cluster-worker.mjs
index a4742a28d43fafe7fe1e21e2e0b6cfe3932805c8..511ad2eb7011956875951e02e7000be94afbcf29 100644 (file)
@@ -1,8 +1,7 @@
-'use strict'
-const { isMaster } = require('cluster')
-const { ClusterWorker } = require('../../lib')
-const { executeWorkerFunction } = require('../benchmarks-utils')
-const { WorkerFunctions } = require('../benchmarks-types')
+import { isMaster } from 'cluster'
+import { ClusterWorker } from '../../lib/index.mjs'
+import { executeWorkerFunction } from '../benchmarks-utils.js'
+import { WorkerFunctions } from '../benchmarks-types.js'
 
 const debug = false
 
@@ -14,4 +13,4 @@ function yourFunction (data) {
   return { ok: 1 }
 }
 
-module.exports = new ClusterWorker(yourFunction)
+export default new ClusterWorker(yourFunction)
diff --git a/benchmarks/internal/thread-worker.js b/benchmarks/internal/thread-worker.js
deleted file mode 100644 (file)
index 3895818..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-'use strict'
-const { isMainThread } = require('worker_threads')
-const { ThreadWorker } = require('../../lib')
-const { executeWorkerFunction } = require('../benchmarks-utils')
-const { WorkerFunctions } = require('../benchmarks-types')
-
-const debug = false
-
-function yourFunction (data) {
-  data = data || {}
-  data.function = data.function || WorkerFunctions.jsonIntegerSerialization
-  executeWorkerFunction(data)
-  debug === true && console.debug('This is the main thread ' + isMainThread)
-  return { ok: 1 }
-}
-
-module.exports = new ThreadWorker(yourFunction)
diff --git a/benchmarks/internal/thread-worker.mjs b/benchmarks/internal/thread-worker.mjs
new file mode 100644 (file)
index 0000000..4f66a89
--- /dev/null
@@ -0,0 +1,16 @@
+import { isMainThread } from 'worker_threads'
+import { ThreadWorker } from '../../lib/index.mjs'
+import { executeWorkerFunction } from '../benchmarks-utils.js'
+import { WorkerFunctions } from '../benchmarks-types.js'
+
+const debug = false
+
+function yourFunction (data) {
+  data = data || {}
+  data.function = data.function || WorkerFunctions.jsonIntegerSerialization
+  executeWorkerFunction(data)
+  debug === true && console.debug('This is the main thread ' + isMainThread)
+  return { ok: 1 }
+}
+
+export default new ThreadWorker(yourFunction)
index 589f12bd42fa11d28e5579a98ed7426b74141a8e..2007d121297c63bfaa4330dae860614c8648818d 100644 (file)
@@ -17,9 +17,9 @@
     "build:analyze": "rollup --config --environment ANALYZE,BUILD:development",
     "build:typedoc": "rollup --config --environment DOCUMENTATION,BUILD:development",
     "build:prod": "rollup --config",
-    "benchmark": "pnpm build && node -r source-map-support/register benchmarks/internal/bench.js",
-    "benchmark:debug": "pnpm build && node -r source-map-support/register --inspect benchmarks/internal/bench.js",
-    "benchmark:prod": "pnpm build:prod && node -r source-map-support/register benchmarks/internal/bench.js",
+    "benchmark": "pnpm build && node -r source-map-support/register benchmarks/internal/bench.mjs",
+    "benchmark:debug": "pnpm build && node -r source-map-support/register --inspect benchmarks/internal/bench.mjs",
+    "benchmark:prod": "pnpm build:prod && node -r source-map-support/register benchmarks/internal/bench.mjs",
     "test": "pnpm build && c8 mocha 'tests/**/*.test.js'",
     "test:debug": "pnpm build && mocha --no-parallel --inspect 'tests/**/*.test.js'",
     "coverage": "c8 report --reporter=lcov",
index b0da7b3e8526d2271bc18204c8b4ba6fc961ed21..1e1d3088fb7cce28ad22f1961899786e6edd5fdd 100644 (file)
@@ -45,7 +45,7 @@ export abstract class AbstractWorker<
    */
   protected lastTaskTimestamp!: number
   /**
-   * Performance statistics computation.
+   * Performance statistics computation requirements.
    */
   protected statistics!: WorkerStatistics
   /**
@@ -298,6 +298,7 @@ export abstract class AbstractWorker<
   }
 
   private beginTaskPerformance (): TaskPerformance {
+    this.checkStatistics()
     return {
       timestamp: performance.now(),
       ...(this.statistics.elu && { elu: performance.eventLoopUtilization() })
@@ -307,6 +308,7 @@ export abstract class AbstractWorker<
   private endTaskPerformance (
     taskPerformance: TaskPerformance
   ): TaskPerformance {
+    this.checkStatistics()
     return {
       ...taskPerformance,
       ...(this.statistics.runTime && {
@@ -317,4 +319,10 @@ export abstract class AbstractWorker<
       })
     }
   }
+
+  private checkStatistics (): void {
+    if (this.statistics == null) {
+      throw new Error('Performance statistics computation requirements not set')
+    }
+  }
 }