import { strictEqual } from 'node:assert'
-
-import { bench, clear, group, run } from 'tatami-ng'
+import { bench, group, run } from 'tatami-ng'
import {
DynamicClusterPool,
Measurements,
PoolTypes,
WorkerChoiceStrategies,
- WorkerTypes
+ WorkerTypes,
} from '../lib/index.mjs'
import { executeTaskFunction } from './benchmarks-utils.cjs'
const buildPoolifierPool = (workerType, poolType, poolSize, poolOptions) => {
switch (poolType) {
- case PoolTypes.fixed:
+ case PoolTypes.dynamic:
switch (workerType) {
- case WorkerTypes.thread:
- return new FixedThreadPool(
- poolSize,
- './benchmarks/internal/thread-worker.mjs',
- poolOptions
- )
case WorkerTypes.cluster:
- return new FixedClusterPool(
+ return new DynamicClusterPool(
+ Math.floor(poolSize / 2),
poolSize,
'./benchmarks/internal/cluster-worker.cjs',
poolOptions
)
- }
- break
- case PoolTypes.dynamic:
- switch (workerType) {
case WorkerTypes.thread:
return new DynamicThreadPool(
Math.floor(poolSize / 2),
'./benchmarks/internal/thread-worker.mjs',
poolOptions
)
+ }
+ break
+ case PoolTypes.fixed:
+ switch (workerType) {
case WorkerTypes.cluster:
- return new DynamicClusterPool(
- Math.floor(poolSize / 2),
+ return new FixedClusterPool(
poolSize,
'./benchmarks/internal/cluster-worker.cjs',
poolOptions
)
+ case WorkerTypes.thread:
+ return new FixedThreadPool(
+ poolSize,
+ './benchmarks/internal/thread-worker.mjs',
+ poolOptions
+ )
}
break
}
}
const runPoolifierPool = async (pool, { taskExecutions, workerData }) => {
- return await new Promise((resolve, reject) => {
- let executions = 0
- for (let i = 1; i <= taskExecutions; i++) {
- pool
- .execute(workerData)
- .then(() => {
- ++executions
- if (executions === taskExecutions) {
- resolve({ ok: 1 })
- }
- return undefined
- })
- .catch(err => {
- console.error(err)
- reject(err)
- })
- }
- })
+ for (let i = 1; i <= taskExecutions; i++) {
+ await pool.execute(workerData)
+ }
}
export const runPoolifierBenchmarkTatamiNg = async (
workerType,
poolType,
poolSize,
+ benchmarkReporter,
{ taskExecutions, workerData }
) => {
try {
async () => {
await runPoolifierPool(pool, {
taskExecutions,
- workerData
+ workerData,
})
},
{
before: () => {
pool.setWorkerChoiceStrategy(workerChoiceStrategy, {
- measurement
+ measurement,
})
pool.enableTasksQueue(enableTasksQueue)
strictEqual(
pool.opts.workerChoiceStrategyOptions.measurement,
measurement
)
- }
+ },
}
)
})
async () => {
await runPoolifierPool(pool, {
taskExecutions,
- workerData
+ workerData,
})
},
{
workerChoiceStrategy
)
strictEqual(pool.opts.enableTasksQueue, enableTasksQueue)
- }
+ },
}
)
})
}
}
}
- const report = await run()
- clear()
+ const report = await run({ reporter: benchmarkReporter })
await pool.destroy()
return report
} catch (error) {
}
}
-export const convertTatamiNgToBmf = report => {
- return report.benchmarks
- .map(({ name, stats }) => {
- return {
- [name]: {
- latency: {
- value: stats?.avg,
- lower_value: stats?.min,
- upper_value: stats?.max
- },
- throughput: {
- value: stats?.iter
- }
- }
- }
- })
- .reduce((obj, item) => Object.assign(obj, item), {})
-}
-
export { executeTaskFunction }