From ab7bb4f826a86d6d121b8902d6449f9fc4e59d70 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 30 Sep 2023 01:09:40 +0200 Subject: [PATCH] perf: workaround ESM issue with cluster MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- ...nchmarks-types.mjs => benchmarks-types.js} | 4 +- ...nchmarks-utils.mjs => benchmarks-utils.js} | 49 +++++++++---------- benchmarks/internal/bench.mjs | 24 ++++++++- .../{cluster-worker.mjs => cluster-worker.js} | 10 ++-- benchmarks/internal/thread-worker.mjs | 4 +- benchmarks/worker-selection/least.mjs | 2 +- benchmarks/worker-selection/round-robin.mjs | 2 +- package.json | 2 +- pnpm-lock.yaml | 34 ++++++------- 9 files changed, 75 insertions(+), 56 deletions(-) rename benchmarks/{benchmarks-types.mjs => benchmarks-types.js} (70%) rename benchmarks/{benchmarks-utils.mjs => benchmarks-utils.js} (87%) rename benchmarks/internal/{cluster-worker.mjs => cluster-worker.js} (52%) diff --git a/benchmarks/benchmarks-types.mjs b/benchmarks/benchmarks-types.js similarity index 70% rename from benchmarks/benchmarks-types.mjs rename to benchmarks/benchmarks-types.js index 69ccce0d..eaaf05d1 100644 --- a/benchmarks/benchmarks-types.mjs +++ b/benchmarks/benchmarks-types.js @@ -1,6 +1,8 @@ -export const TaskFunctions = { +const TaskFunctions = { jsonIntegerSerialization: 'jsonIntegerSerialization', fibonacci: 'fibonacci', factorial: 'factorial', readWriteFiles: 'readWriteFiles' } + +module.exports = { TaskFunctions } diff --git a/benchmarks/benchmarks-utils.mjs b/benchmarks/benchmarks-utils.js similarity index 87% rename from benchmarks/benchmarks-utils.mjs rename to benchmarks/benchmarks-utils.js index 79971825..aaf7521f 100644 --- a/benchmarks/benchmarks-utils.mjs +++ b/benchmarks/benchmarks-utils.js @@ -1,8 +1,8 @@ -import crypto from 'node:crypto' -import assert from 'node:assert' -import fs from 'node:fs' -import Benchmark from 'benchmark' -import { +const crypto = require('node:crypto') +const assert = require('node:assert') +const fs = require('node:fs') +const Benchmark = require('benchmark') +const { DynamicClusterPool, DynamicThreadPool, FixedClusterPool, @@ -11,15 +11,10 @@ import { PoolTypes, WorkerChoiceStrategies, WorkerTypes -} from '../lib/index.mjs' -import { TaskFunctions } from './benchmarks-types.mjs' +} = require('../lib/index.js') +const { TaskFunctions } = require('./benchmarks-types.js') -export const buildPoolifierPool = ( - workerType, - poolType, - poolSize, - poolOptions -) => { +const buildPoolifierPool = (workerType, poolType, poolSize, poolOptions) => { switch (poolType) { case PoolTypes.fixed: switch (workerType) { @@ -32,7 +27,7 @@ export const buildPoolifierPool = ( case WorkerTypes.cluster: return new FixedClusterPool( poolSize, - './benchmarks/internal/cluster-worker.mjs', + './benchmarks/internal/cluster-worker.js', poolOptions ) } @@ -50,7 +45,7 @@ export const buildPoolifierPool = ( return new DynamicClusterPool( Math.floor(poolSize / 2), poolSize, - './benchmarks/internal/cluster-worker.mjs', + './benchmarks/internal/cluster-worker.js', poolOptions ) } @@ -58,10 +53,7 @@ export const buildPoolifierPool = ( } } -export const runPoolifierPool = async ( - pool, - { taskExecutions, workerData } -) => { +const runPoolifierPool = async (pool, { taskExecutions, workerData }) => { return await new Promise((resolve, reject) => { let executions = 0 for (let i = 1; i <= taskExecutions; i++) { @@ -82,7 +74,7 @@ export const runPoolifierPool = async ( }) } -export const runPoolifierPoolBenchmark = async ( +const runPoolifierPoolBenchmark = async ( name, pool, { taskExecutions, workerData } @@ -168,15 +160,12 @@ export const runPoolifierPoolBenchmark = async ( }) } -export const LIST_FORMATTER = new Intl.ListFormat('en-US', { +const LIST_FORMATTER = new Intl.ListFormat('en-US', { style: 'long', type: 'conjunction' }) -export const generateRandomInteger = ( - max = Number.MAX_SAFE_INTEGER, - min = 0 -) => { +const generateRandomInteger = (max = Number.MAX_SAFE_INTEGER, min = 0) => { if (max < min || max < 0 || min < 0) { throw new RangeError('Invalid interval') } @@ -242,7 +231,7 @@ const readWriteFiles = ( return { ok: 1 } } -export const executeTaskFunction = data => { +const executeTaskFunction = data => { switch (data.function) { case TaskFunctions.jsonIntegerSerialization: return jsonIntegerSerialization(data.taskSize || 1000) @@ -256,3 +245,11 @@ export const executeTaskFunction = data => { throw new Error('Unknown task function') } } + +module.exports = { + LIST_FORMATTER, + buildPoolifierPool, + executeTaskFunction, + generateRandomInteger, + runPoolifierPoolBenchmark +} diff --git a/benchmarks/internal/bench.mjs b/benchmarks/internal/bench.mjs index 3484951c..7ef61bc6 100644 --- a/benchmarks/internal/bench.mjs +++ b/benchmarks/internal/bench.mjs @@ -3,11 +3,11 @@ import { WorkerTypes, availableParallelism } from '../../lib/index.mjs' -import { TaskFunctions } from '../benchmarks-types.mjs' +import { TaskFunctions } from '../benchmarks-types.js' import { buildPoolifierPool, runPoolifierPoolBenchmark -} from '../benchmarks-utils.mjs' +} from '../benchmarks-utils.js' const poolSize = availableParallelism() const taskExecutions = 1 @@ -35,3 +35,23 @@ await runPoolifierPoolBenchmark( workerData } ) + +// FixedClusterPool +await runPoolifierPoolBenchmark( + 'Poolifier FixedClusterPool', + buildPoolifierPool(WorkerTypes.cluster, PoolTypes.fixed, poolSize), + { + taskExecutions, + workerData + } +) + +// DynamicClusterPool +await runPoolifierPoolBenchmark( + 'Poolifier DynamicClusterPool', + buildPoolifierPool(WorkerTypes.cluster, PoolTypes.dynamic, poolSize), + { + taskExecutions, + workerData + } +) diff --git a/benchmarks/internal/cluster-worker.mjs b/benchmarks/internal/cluster-worker.js similarity index 52% rename from benchmarks/internal/cluster-worker.mjs rename to benchmarks/internal/cluster-worker.js index 1d64fde3..18bd648c 100644 --- a/benchmarks/internal/cluster-worker.mjs +++ b/benchmarks/internal/cluster-worker.js @@ -1,7 +1,7 @@ -import { isPrimary } from 'node:cluster' -import { ClusterWorker } from '../../lib/index.mjs' -import { executeTaskFunction } from '../benchmarks-utils.mjs' -import { TaskFunctions } from '../benchmarks-types.mjs' +const { isPrimary } = require('node:cluster') +const { ClusterWorker } = require('../../lib') +const { executeTaskFunction } = require('../benchmarks-utils.js') +const { TaskFunctions } = require('../benchmarks-types.js') const taskFunction = data => { data = data || {} @@ -12,4 +12,4 @@ const taskFunction = data => { return res } -export default new ClusterWorker(taskFunction) +module.exports = new ClusterWorker(taskFunction) diff --git a/benchmarks/internal/thread-worker.mjs b/benchmarks/internal/thread-worker.mjs index 5c6a691e..b0994e77 100644 --- a/benchmarks/internal/thread-worker.mjs +++ b/benchmarks/internal/thread-worker.mjs @@ -1,7 +1,7 @@ import { isMainThread } from 'node:worker_threads' import { ThreadWorker } from '../../lib/index.mjs' -import { executeTaskFunction } from '../benchmarks-utils.mjs' -import { TaskFunctions } from '../benchmarks-types.mjs' +import { executeTaskFunction } from '../benchmarks-utils.js' +import { TaskFunctions } from '../benchmarks-types.js' const taskFunction = data => { data = data || {} diff --git a/benchmarks/worker-selection/least.mjs b/benchmarks/worker-selection/least.mjs index 2abbae82..0969369f 100644 --- a/benchmarks/worker-selection/least.mjs +++ b/benchmarks/worker-selection/least.mjs @@ -1,5 +1,5 @@ import Benchmark from 'benchmark' -import { LIST_FORMATTER, generateRandomInteger } from '../benchmarks-utils.mjs' +import { LIST_FORMATTER, generateRandomInteger } from '../benchmarks-utils.js' function generateRandomTasksMap ( numberOfWorkers, diff --git a/benchmarks/worker-selection/round-robin.mjs b/benchmarks/worker-selection/round-robin.mjs index 483098d2..302cd2c6 100644 --- a/benchmarks/worker-selection/round-robin.mjs +++ b/benchmarks/worker-selection/round-robin.mjs @@ -1,5 +1,5 @@ import Benchmark from 'benchmark' -import { LIST_FORMATTER } from '../benchmarks-utils.mjs' +import { LIST_FORMATTER } from '../benchmarks-utils.js' function generateWorkersArray (numberOfWorkers) { return [...Array(numberOfWorkers).keys()] diff --git a/package.json b/package.json index d5bef2c8..4fccea21 100644 --- a/package.json +++ b/package.json @@ -108,7 +108,7 @@ "@release-it/keep-a-changelog": "^4.0.0", "@rollup/plugin-terser": "^0.4.3", "@rollup/plugin-typescript": "^11.1.4", - "@types/node": "^20.7.1", + "@types/node": "^20.7.2", "@typescript-eslint/eslint-plugin": "^6.7.3", "@typescript-eslint/parser": "^6.7.3", "benchmark": "^2.1.4", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f271df03..a6aecc6c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -30,8 +30,8 @@ devDependencies: specifier: ^11.1.4 version: 11.1.4(rollup@3.29.4)(typescript@5.2.2) '@types/node': - specifier: ^20.7.1 - version: 20.7.1 + specifier: ^20.7.2 + version: 20.7.2 '@typescript-eslint/eslint-plugin': specifier: ^6.7.3 version: 6.7.3(@typescript-eslint/parser@6.7.3)(eslint@8.50.0)(typescript@5.2.2) @@ -329,7 +329,7 @@ packages: lodash.merge: 4.6.2 lodash.uniq: 4.5.0 resolve-from: 5.0.0 - ts-node: 10.9.1(@types/node@20.7.1)(typescript@5.2.2) + ts-node: 10.9.1(@types/node@20.7.2)(typescript@5.2.2) typescript: 5.2.2 transitivePeerDependencies: - '@swc/core' @@ -506,8 +506,8 @@ packages: '@jest/schemas': 29.6.3 '@types/istanbul-lib-coverage': 2.0.4 '@types/istanbul-reports': 3.0.2 - '@types/node': 20.7.1 - '@types/yargs': 17.0.25 + '@types/node': 20.7.2 + '@types/yargs': 17.0.26 chalk: 4.1.2 dev: true @@ -889,7 +889,7 @@ packages: resolution: {integrity: sha512-ZUxbzKl0IfJILTS6t7ip5fQQM/J3TJYubDm3nMbgubNNYS62eXeUpoLUC8/7fJNiFYHTrGPQn7hspDUzIHX3UA==} dependencies: '@types/minimatch': 5.1.2 - '@types/node': 20.7.1 + '@types/node': 20.7.2 dev: true /@types/http-cache-semantics@4.0.2: @@ -932,8 +932,8 @@ packages: resolution: {integrity: sha512-4tT2UrL5LBqDwoed9wZ6N3umC4Yhz3W3FloMmiiG4JwmUJWpie0c7lcnUNd4gtMKuDEO4wRVS8B6Xa0uMRsMKg==} dev: true - /@types/node@20.7.1: - resolution: {integrity: sha512-LT+OIXpp2kj4E2S/p91BMe+VgGX2+lfO+XTpfXhh+bCk2LkQtHZSub8ewFBMGP5ClysPjTDFa4sMI8Q3n4T0wg==} + /@types/node@20.7.2: + resolution: {integrity: sha512-RcdC3hOBOauLP+r/kRt27NrByYtDjsXyAuSbR87O6xpsvi763WI+5fbSIvYJrXnt9w4RuxhV6eAXfIs7aaf/FQ==} dev: true /@types/normalize-package-data@2.4.2: @@ -952,8 +952,8 @@ packages: resolution: {integrity: sha512-axdPBuLuEJt0c4yI5OZssC19K2Mq1uKdrfZBzuxLvaztgqUtFYZUNw7lETExPYJR9jdEoIg4mb7RQKRQzOkeGQ==} dev: true - /@types/yargs@17.0.25: - resolution: {integrity: sha512-gy7iPgwnzNvxgAEi2bXOHWCVOG6f7xsprVJH4MjlAWeBmJ7vh/Y1kwMtUrs64ztf24zVIRCpr3n/z6gm9QIkgg==} + /@types/yargs@17.0.26: + resolution: {integrity: sha512-Y3vDy2X6zw/ZCumcwLpdhM5L7jmyGpmBCTYMHDLqT2IKVMYRRLdv6ZakA+wxhra6Z/3bwhNbNl9bDGXaFU+6rw==} dependencies: '@types/yargs-parser': 21.0.1 dev: true @@ -1912,7 +1912,7 @@ packages: dependencies: '@types/node': 20.5.1 cosmiconfig: 8.3.6(typescript@5.2.2) - ts-node: 10.9.1(@types/node@20.7.1)(typescript@5.2.2) + ts-node: 10.9.1(@types/node@20.7.2)(typescript@5.2.2) typescript: 5.2.2 dev: true @@ -4015,7 +4015,7 @@ packages: engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} dependencies: '@jest/types': 29.6.3 - '@types/node': 20.7.1 + '@types/node': 20.7.2 chalk: 4.1.2 ci-info: 3.8.0 graceful-fs: 4.2.11 @@ -4367,8 +4367,8 @@ packages: engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0} dev: true - /magic-string@0.30.3: - resolution: {integrity: sha512-B7xGbll2fG/VjP+SWg4sX3JynwIU0mjoTc6MPpKNuIvftk6u6vqhDnk1R80b8C2GBR6ywqy+1DcKBrevBg+bmw==} + /magic-string@0.30.4: + resolution: {integrity: sha512-Q/TKtsC5BPm0kGqgBIF9oXAs/xEf2vRKiIB4wCRQTJOQIByZ1d+NnUOotvJOvNpi5RNIgVOMC3pOuaP1ZTDlVg==} engines: {node: '>=12'} dependencies: '@jridgewell/sourcemap-codec': 1.4.15 @@ -5454,7 +5454,7 @@ packages: rollup: ^3.25 typescript: ^4.5 || ^5.0 dependencies: - magic-string: 0.30.3 + magic-string: 0.30.4 rollup: 3.29.4 typescript: 5.2.2 optionalDependencies: @@ -5971,7 +5971,7 @@ packages: typescript: 5.2.2 dev: true - /ts-node@10.9.1(@types/node@20.7.1)(typescript@5.2.2): + /ts-node@10.9.1(@types/node@20.7.2)(typescript@5.2.2): resolution: {integrity: sha512-NtVysVPkxxrwFGUUxGYhfux8k78pQB3JqYBXlLRZgdGUqTO5wU/UyHop5p70iEbGhB7q5KmiZiU0Y3KlJrScEw==} hasBin: true peerDependencies: @@ -5990,7 +5990,7 @@ packages: '@tsconfig/node12': 1.0.11 '@tsconfig/node14': 1.0.3 '@tsconfig/node16': 1.0.4 - '@types/node': 20.7.1 + '@types/node': 20.7.2 acorn: 8.10.0 acorn-walk: 8.2.0 arg: 4.1.3 -- 2.34.1