perf: workaround ESM issue with cluster
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 29 Sep 2023 23:09:40 +0000 (01:09 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 29 Sep 2023 23:09:40 +0000 (01:09 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
benchmarks/benchmarks-types.js [moved from benchmarks/benchmarks-types.mjs with 70% similarity]
benchmarks/benchmarks-utils.js [moved from benchmarks/benchmarks-utils.mjs with 87% similarity]
benchmarks/internal/bench.mjs
benchmarks/internal/cluster-worker.js [moved from benchmarks/internal/cluster-worker.mjs with 52% similarity]
benchmarks/internal/thread-worker.mjs
benchmarks/worker-selection/least.mjs
benchmarks/worker-selection/round-robin.mjs
package.json
pnpm-lock.yaml

similarity index 70%
rename from benchmarks/benchmarks-types.mjs
rename to benchmarks/benchmarks-types.js
index 69ccce0dc771eb482a1d2bb1a81cc29ef4331f10..eaaf05d1be4df25ed55259d5878f26ff39c00bc1 100644 (file)
@@ -1,6 +1,8 @@
-export const TaskFunctions = {
+const TaskFunctions = {
   jsonIntegerSerialization: 'jsonIntegerSerialization',
   fibonacci: 'fibonacci',
   factorial: 'factorial',
   readWriteFiles: 'readWriteFiles'
 }
+
+module.exports = { TaskFunctions }
similarity index 87%
rename from benchmarks/benchmarks-utils.mjs
rename to benchmarks/benchmarks-utils.js
index 79971825e20e9e66c394b7b229eb6b4b5d852eaf..aaf7521ff3044807e02078c2cde0f45c584586b1 100644 (file)
@@ -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
+}
index 3484951cfb187a42bf1af5f325cd1c436adb6baf..7ef61bc6cba31225a4d8732b721422494deeb4d3 100644 (file)
@@ -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
+  }
+)
similarity index 52%
rename from benchmarks/internal/cluster-worker.mjs
rename to benchmarks/internal/cluster-worker.js
index 1d64fde30a19d82f0e4f52cb1781a7eab9efeae6..18bd648c72ce73d454b3503b038719aeade6bd11 100644 (file)
@@ -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)
index 5c6a691e47af247dde38a075a3cd4c78cc15d02c..b0994e77bd1af7183f98559481e43fdaf7ec92bb 100644 (file)
@@ -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 || {}
index 2abbae8275c147187a486b368ffec84c3bdcd579..0969369fb6e1a49c6b10563aa5ab5b6fc1935b5b 100644 (file)
@@ -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,
index 483098d2ff93d0a4c0bb9b225b11ccbe963e6bd2..302cd2c6dffa5e5fa1a76c42f6056327a857c895 100644 (file)
@@ -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()]
index d5bef2c8cc0ef590e5c7e6d482b4f247cd289678..4fccea21af32b8009dcdd598ff337d0a91191866 100644 (file)
     "@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",
index f271df03a696e8a84335f0a1ac48e14250f5263f..a6aecc6cf4653bde3b7911932af5b9c30130705e 100644 (file)
@@ -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