| 1 | import { strictEqual } from 'node:assert' |
| 2 | |
| 3 | import { bench, clear, group, run } from 'tatami-ng' |
| 4 | |
| 5 | import { |
| 6 | DynamicClusterPool, |
| 7 | DynamicThreadPool, |
| 8 | FixedClusterPool, |
| 9 | FixedThreadPool, |
| 10 | Measurements, |
| 11 | PoolTypes, |
| 12 | WorkerChoiceStrategies, |
| 13 | WorkerTypes, |
| 14 | } from '../lib/index.mjs' |
| 15 | import { executeTaskFunction } from './benchmarks-utils.cjs' |
| 16 | |
| 17 | const buildPoolifierPool = (workerType, poolType, poolSize, poolOptions) => { |
| 18 | switch (poolType) { |
| 19 | case PoolTypes.fixed: |
| 20 | switch (workerType) { |
| 21 | case WorkerTypes.thread: |
| 22 | return new FixedThreadPool( |
| 23 | poolSize, |
| 24 | './benchmarks/internal/thread-worker.mjs', |
| 25 | poolOptions |
| 26 | ) |
| 27 | case WorkerTypes.cluster: |
| 28 | return new FixedClusterPool( |
| 29 | poolSize, |
| 30 | './benchmarks/internal/cluster-worker.cjs', |
| 31 | poolOptions |
| 32 | ) |
| 33 | } |
| 34 | break |
| 35 | case PoolTypes.dynamic: |
| 36 | switch (workerType) { |
| 37 | case WorkerTypes.thread: |
| 38 | return new DynamicThreadPool( |
| 39 | Math.floor(poolSize / 2), |
| 40 | poolSize, |
| 41 | './benchmarks/internal/thread-worker.mjs', |
| 42 | poolOptions |
| 43 | ) |
| 44 | case WorkerTypes.cluster: |
| 45 | return new DynamicClusterPool( |
| 46 | Math.floor(poolSize / 2), |
| 47 | poolSize, |
| 48 | './benchmarks/internal/cluster-worker.cjs', |
| 49 | poolOptions |
| 50 | ) |
| 51 | } |
| 52 | break |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | const runPoolifierPool = async (pool, { taskExecutions, workerData }) => { |
| 57 | for (let i = 1; i <= taskExecutions; i++) { |
| 58 | await pool.execute(workerData) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | export const runPoolifierBenchmarkTatamiNg = async ( |
| 63 | name, |
| 64 | workerType, |
| 65 | poolType, |
| 66 | poolSize, |
| 67 | { taskExecutions, workerData } |
| 68 | ) => { |
| 69 | try { |
| 70 | const pool = buildPoolifierPool(workerType, poolType, poolSize) |
| 71 | for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) { |
| 72 | for (const enableTasksQueue of [false, true]) { |
| 73 | if (workerChoiceStrategy === WorkerChoiceStrategies.FAIR_SHARE) { |
| 74 | for (const measurement of [Measurements.runTime, Measurements.elu]) { |
| 75 | group(name, () => { |
| 76 | bench( |
| 77 | `${name} with ${workerChoiceStrategy}, with ${measurement} and ${ |
| 78 | enableTasksQueue ? 'with' : 'without' |
| 79 | } tasks queue`, |
| 80 | async () => { |
| 81 | await runPoolifierPool(pool, { |
| 82 | taskExecutions, |
| 83 | workerData, |
| 84 | }) |
| 85 | }, |
| 86 | { |
| 87 | before: () => { |
| 88 | pool.setWorkerChoiceStrategy(workerChoiceStrategy, { |
| 89 | measurement, |
| 90 | }) |
| 91 | pool.enableTasksQueue(enableTasksQueue) |
| 92 | strictEqual( |
| 93 | pool.opts.workerChoiceStrategy, |
| 94 | workerChoiceStrategy |
| 95 | ) |
| 96 | strictEqual(pool.opts.enableTasksQueue, enableTasksQueue) |
| 97 | strictEqual( |
| 98 | pool.opts.workerChoiceStrategyOptions.measurement, |
| 99 | measurement |
| 100 | ) |
| 101 | }, |
| 102 | } |
| 103 | ) |
| 104 | }) |
| 105 | } |
| 106 | } else { |
| 107 | group(name, () => { |
| 108 | bench( |
| 109 | `${name} with ${workerChoiceStrategy} and ${ |
| 110 | enableTasksQueue ? 'with' : 'without' |
| 111 | } tasks queue`, |
| 112 | async () => { |
| 113 | await runPoolifierPool(pool, { |
| 114 | taskExecutions, |
| 115 | workerData, |
| 116 | }) |
| 117 | }, |
| 118 | { |
| 119 | before: () => { |
| 120 | pool.setWorkerChoiceStrategy(workerChoiceStrategy) |
| 121 | pool.enableTasksQueue(enableTasksQueue) |
| 122 | strictEqual( |
| 123 | pool.opts.workerChoiceStrategy, |
| 124 | workerChoiceStrategy |
| 125 | ) |
| 126 | strictEqual(pool.opts.enableTasksQueue, enableTasksQueue) |
| 127 | }, |
| 128 | } |
| 129 | ) |
| 130 | }) |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | const report = await run() |
| 135 | clear() |
| 136 | await pool.destroy() |
| 137 | return report |
| 138 | } catch (error) { |
| 139 | console.error(error) |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | export const convertTatamiNgToBmf = report => { |
| 144 | return report.benchmarks |
| 145 | .map(({ name, stats }) => { |
| 146 | return { |
| 147 | [name]: { |
| 148 | latency: { |
| 149 | value: stats?.avg, |
| 150 | lower_value: stats?.min, |
| 151 | upper_value: stats?.max, |
| 152 | }, |
| 153 | throughput: { |
| 154 | value: stats?.iter, |
| 155 | }, |
| 156 | }, |
| 157 | } |
| 158 | }) |
| 159 | .reduce((obj, item) => Object.assign(obj, item), {}) |
| 160 | } |
| 161 | |
| 162 | export { executeTaskFunction } |