refactor(benchmark): improve benchmark.js code
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Sun, 31 Mar 2024 14:08:15 +0000 (16:08 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Sun, 31 Mar 2024 14:08:15 +0000 (16:08 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
benchmarks/benchmarks-utils.mjs

index f04e4d0f64a0608b9258d5b346a5f4ce9fd21501..96d597cfc922ad48f9695482979a6d8b1a807851 100644 (file)
@@ -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()