| 1 | import { strictEqual } from 'node:assert' |
| 2 | |
| 3 | import Benchmark from 'benchmark' |
| 4 | import { bench, group } from 'mitata' |
| 5 | |
| 6 | import { |
| 7 | DynamicClusterPool, |
| 8 | DynamicThreadPool, |
| 9 | FixedClusterPool, |
| 10 | FixedThreadPool, |
| 11 | Measurements, |
| 12 | PoolTypes, |
| 13 | WorkerChoiceStrategies, |
| 14 | WorkerTypes |
| 15 | } from '../lib/index.mjs' |
| 16 | import { executeTaskFunction } from './benchmarks-utils.cjs' |
| 17 | |
| 18 | const buildPoolifierPool = (workerType, poolType, poolSize, poolOptions) => { |
| 19 | switch (poolType) { |
| 20 | case PoolTypes.fixed: |
| 21 | switch (workerType) { |
| 22 | case WorkerTypes.thread: |
| 23 | return new FixedThreadPool( |
| 24 | poolSize, |
| 25 | './benchmarks/internal/thread-worker.mjs', |
| 26 | poolOptions |
| 27 | ) |
| 28 | case WorkerTypes.cluster: |
| 29 | return new FixedClusterPool( |
| 30 | poolSize, |
| 31 | './benchmarks/internal/cluster-worker.cjs', |
| 32 | poolOptions |
| 33 | ) |
| 34 | } |
| 35 | break |
| 36 | case PoolTypes.dynamic: |
| 37 | switch (workerType) { |
| 38 | case WorkerTypes.thread: |
| 39 | return new DynamicThreadPool( |
| 40 | Math.floor(poolSize / 2), |
| 41 | poolSize, |
| 42 | './benchmarks/internal/thread-worker.mjs', |
| 43 | poolOptions |
| 44 | ) |
| 45 | case WorkerTypes.cluster: |
| 46 | return new DynamicClusterPool( |
| 47 | Math.floor(poolSize / 2), |
| 48 | poolSize, |
| 49 | './benchmarks/internal/cluster-worker.cjs', |
| 50 | poolOptions |
| 51 | ) |
| 52 | } |
| 53 | break |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | const runPoolifierPool = async (pool, { taskExecutions, workerData }) => { |
| 58 | return await new Promise((resolve, reject) => { |
| 59 | let executions = 0 |
| 60 | for (let i = 1; i <= taskExecutions; i++) { |
| 61 | pool |
| 62 | .execute(workerData) |
| 63 | .then(() => { |
| 64 | ++executions |
| 65 | if (executions === taskExecutions) { |
| 66 | resolve({ ok: 1 }) |
| 67 | } |
| 68 | return undefined |
| 69 | }) |
| 70 | .catch(err => { |
| 71 | console.error(err) |
| 72 | reject(err) |
| 73 | }) |
| 74 | } |
| 75 | }) |
| 76 | } |
| 77 | |
| 78 | export const runPoolifierBenchmarkBenchmarkJs = async ( |
| 79 | name, |
| 80 | workerType, |
| 81 | poolType, |
| 82 | poolSize, |
| 83 | { taskExecutions, workerData } |
| 84 | ) => { |
| 85 | return await new Promise((resolve, reject) => { |
| 86 | const pool = buildPoolifierPool(workerType, poolType, poolSize) |
| 87 | const suite = new Benchmark.Suite(name, { |
| 88 | onComplete: () => { |
| 89 | if (pool.started && !pool.destroying) { |
| 90 | pool.destroy().then(resolve).catch(reject) |
| 91 | } else { |
| 92 | resolve() |
| 93 | } |
| 94 | }, |
| 95 | onCycle: event => { |
| 96 | console.info(event.target.toString()) |
| 97 | }, |
| 98 | onError: event => { |
| 99 | if (pool.started && !pool.destroying) { |
| 100 | pool |
| 101 | .destroy() |
| 102 | .then(() => { |
| 103 | return reject(event.target.error) |
| 104 | }) |
| 105 | .catch(() => {}) |
| 106 | } else { |
| 107 | reject(event.target.error) |
| 108 | } |
| 109 | } |
| 110 | }) |
| 111 | for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) { |
| 112 | for (const enableTasksQueue of [false, true]) { |
| 113 | if (workerChoiceStrategy === WorkerChoiceStrategies.FAIR_SHARE) { |
| 114 | for (const measurement of [Measurements.runTime, Measurements.elu]) { |
| 115 | suite.add( |
| 116 | `${name} with ${workerChoiceStrategy}, with measurement ${measurement} and ${ |
| 117 | enableTasksQueue ? 'with' : 'without' |
| 118 | } tasks queue`, |
| 119 | async () => { |
| 120 | await runPoolifierPool(pool, { |
| 121 | taskExecutions, |
| 122 | workerData |
| 123 | }) |
| 124 | }, |
| 125 | { |
| 126 | onStart: () => { |
| 127 | pool.setWorkerChoiceStrategy(workerChoiceStrategy, { |
| 128 | measurement |
| 129 | }) |
| 130 | pool.enableTasksQueue(enableTasksQueue) |
| 131 | strictEqual( |
| 132 | pool.opts.workerChoiceStrategy, |
| 133 | workerChoiceStrategy |
| 134 | ) |
| 135 | strictEqual(pool.opts.enableTasksQueue, enableTasksQueue) |
| 136 | strictEqual( |
| 137 | pool.opts.workerChoiceStrategyOptions.measurement, |
| 138 | measurement |
| 139 | ) |
| 140 | } |
| 141 | } |
| 142 | ) |
| 143 | } |
| 144 | } else { |
| 145 | suite.add( |
| 146 | `${name} with ${workerChoiceStrategy} and ${ |
| 147 | enableTasksQueue ? 'with' : 'without' |
| 148 | } tasks queue`, |
| 149 | async () => { |
| 150 | await runPoolifierPool(pool, { |
| 151 | taskExecutions, |
| 152 | workerData |
| 153 | }) |
| 154 | }, |
| 155 | { |
| 156 | onStart: () => { |
| 157 | pool.setWorkerChoiceStrategy(workerChoiceStrategy) |
| 158 | pool.enableTasksQueue(enableTasksQueue) |
| 159 | strictEqual( |
| 160 | pool.opts.workerChoiceStrategy, |
| 161 | workerChoiceStrategy |
| 162 | ) |
| 163 | strictEqual(pool.opts.enableTasksQueue, enableTasksQueue) |
| 164 | } |
| 165 | } |
| 166 | ) |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | suite |
| 171 | .on('complete', function () { |
| 172 | console.info( |
| 173 | 'Fastest is ' + |
| 174 | LIST_FORMATTER.format(this.filter('fastest').map('name')) |
| 175 | ) |
| 176 | }) |
| 177 | .run({ async: true }) |
| 178 | }) |
| 179 | } |
| 180 | |
| 181 | export const buildPoolifierBenchmarkMitata = ( |
| 182 | name, |
| 183 | workerType, |
| 184 | poolType, |
| 185 | poolSize, |
| 186 | { taskExecutions, workerData } |
| 187 | ) => { |
| 188 | try { |
| 189 | const pool = buildPoolifierPool(workerType, poolType, poolSize) |
| 190 | for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) { |
| 191 | for (const enableTasksQueue of [false, true]) { |
| 192 | if (workerChoiceStrategy === WorkerChoiceStrategies.FAIR_SHARE) { |
| 193 | for (const measurement of [Measurements.runTime, Measurements.elu]) { |
| 194 | group(name, () => { |
| 195 | bench( |
| 196 | `${name} with ${workerChoiceStrategy}, with ${measurement} and ${ |
| 197 | enableTasksQueue ? 'with' : 'without' |
| 198 | } tasks queue`, |
| 199 | async () => { |
| 200 | pool.setWorkerChoiceStrategy(workerChoiceStrategy, { |
| 201 | measurement |
| 202 | }) |
| 203 | pool.enableTasksQueue(enableTasksQueue) |
| 204 | strictEqual( |
| 205 | pool.opts.workerChoiceStrategy, |
| 206 | workerChoiceStrategy |
| 207 | ) |
| 208 | strictEqual(pool.opts.enableTasksQueue, enableTasksQueue) |
| 209 | strictEqual( |
| 210 | pool.opts.workerChoiceStrategyOptions.measurement, |
| 211 | measurement |
| 212 | ) |
| 213 | await runPoolifierPool(pool, { |
| 214 | taskExecutions, |
| 215 | workerData |
| 216 | }) |
| 217 | } |
| 218 | ) |
| 219 | }) |
| 220 | } |
| 221 | } else { |
| 222 | group(name, () => { |
| 223 | bench( |
| 224 | `${name} with ${workerChoiceStrategy} and ${ |
| 225 | enableTasksQueue ? 'with' : 'without' |
| 226 | } tasks queue`, |
| 227 | async () => { |
| 228 | pool.setWorkerChoiceStrategy(workerChoiceStrategy) |
| 229 | pool.enableTasksQueue(enableTasksQueue) |
| 230 | strictEqual( |
| 231 | pool.opts.workerChoiceStrategy, |
| 232 | workerChoiceStrategy |
| 233 | ) |
| 234 | strictEqual(pool.opts.enableTasksQueue, enableTasksQueue) |
| 235 | await runPoolifierPool(pool, { |
| 236 | taskExecutions, |
| 237 | workerData |
| 238 | }) |
| 239 | } |
| 240 | ) |
| 241 | }) |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | return pool |
| 246 | } catch (error) { |
| 247 | console.error(error) |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | const LIST_FORMATTER = new Intl.ListFormat('en-US', { |
| 252 | style: 'long', |
| 253 | type: 'conjunction' |
| 254 | }) |
| 255 | |
| 256 | export { executeTaskFunction } |