From fe7488e988f14a6f744aeadff61c2df9bd2239eb Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 31 Mar 2024 16:08:15 +0200 Subject: [PATCH] refactor(benchmark): improve benchmark.js code MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- benchmarks/benchmarks-utils.mjs | 50 ++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/benchmarks/benchmarks-utils.mjs b/benchmarks/benchmarks-utils.mjs index f04e4d0f..96d597cf 100644 --- a/benchmarks/benchmarks-utils.mjs +++ b/benchmarks/benchmarks-utils.mjs @@ -83,9 +83,36 @@ export const runPoolifierBenchmarkBenchmarkJs = async ( { taskExecutions, workerData } ) => { return await new Promise((resolve, reject) => { - const pool = buildPoolifierPool(workerType, poolType, poolSize) + let pool = buildPoolifierPool(workerType, poolType, poolSize) try { - const suite = new Benchmark.Suite(name) + const suite = new Benchmark.Suite(name, { + onComplete: () => { + const destroyTimeout = setTimeout(() => { + console.error('Pool destroy timeout reached (30s)') + resolve() + pool = undefined + }, 30000) + pool + .destroy() + .then(resolve) + .catch(reject) + .finally(() => { + clearTimeout(destroyTimeout) + }) + .catch(() => {}) + }, + onCycle: event => { + console.info(event.target.toString()) + }, + onError: event => { + pool + .destroy() + .then(() => { + return reject(event.target.error) + }) + .catch(() => {}) + } + }) for (const workerChoiceStrategy of Object.values( WorkerChoiceStrategies )) { @@ -96,7 +123,7 @@ export const runPoolifierBenchmarkBenchmarkJs = async ( Measurements.elu ]) { suite.add( - `${name} with ${workerChoiceStrategy}, with ${measurement} and ${ + `${name} with ${workerChoiceStrategy}, with measurement ${measurement} and ${ enableTasksQueue ? 'with' : 'without' } tasks queue`, async () => { @@ -151,28 +178,13 @@ export const runPoolifierBenchmarkBenchmarkJs = async ( } } suite - .on('cycle', event => { - console.info(event.target.toString()) - }) .on('complete', function () { console.info( 'Fastest is ' + LIST_FORMATTER.format(this.filter('fastest').map('name')) ) - const destroyTimeout = setTimeout(() => { - console.error('Pool destroy timeout reached (30s)') - resolve() - }, 30000) - pool - .destroy() - .then(resolve) - .catch(reject) - .finally(() => { - clearTimeout(destroyTimeout) - }) - .catch(() => {}) }) - .run({ async: true }) + .run({ async: true, queued: true }) } catch (error) { pool .destroy() -- 2.34.1