From: Jérôme Benoit Date: Sun, 25 Jun 2023 14:00:16 +0000 (+0200) Subject: refactor: convert internal benchmark code to ESM X-Git-Tag: v2.6.4~33 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=8a97042123ae9a0404637711b8da7c6e7e4424c7;p=poolifier.git refactor: convert internal benchmark code to ESM Signed-off-by: Jérôme Benoit --- diff --git a/benchmarks/benchmarks-utils.js b/benchmarks/benchmarks-utils.js index eb3a4f94..6195a983 100644 --- a/benchmarks/benchmarks-utils.js +++ b/benchmarks/benchmarks-utils.js @@ -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 ) } diff --git a/benchmarks/internal/bench.js b/benchmarks/internal/bench.mjs similarity index 97% rename from benchmarks/internal/bench.js rename to benchmarks/internal/bench.mjs index 627bd0ed..6fad24c7 100644 --- a/benchmarks/internal/bench.js +++ b/benchmarks/internal/bench.mjs @@ -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 diff --git a/benchmarks/internal/cluster-worker.js b/benchmarks/internal/cluster-worker.mjs similarity index 50% rename from benchmarks/internal/cluster-worker.js rename to benchmarks/internal/cluster-worker.mjs index a4742a28..511ad2eb 100644 --- a/benchmarks/internal/cluster-worker.js +++ b/benchmarks/internal/cluster-worker.mjs @@ -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 index 38958181..00000000 --- a/benchmarks/internal/thread-worker.js +++ /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 index 00000000..4f66a89a --- /dev/null +++ b/benchmarks/internal/thread-worker.mjs @@ -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) diff --git a/package.json b/package.json index 589f12bd..2007d121 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index b0da7b3e..1e1d3088 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -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') + } + } }