From 4614cfefcda310f437af0a6a9262d31dd15dba10 Mon Sep 17 00:00:00 2001 From: Jason Date: Sat, 6 Dec 2025 00:23:19 -0800 Subject: [PATCH 1/2] fix: Benchmarks that use timers cannot use workers. Fixes #135 --- examples/worker-threads/node.js | 8 ++++++++ lib/worker-runner.js | 3 ++- test/worker.js | 13 +++++++++++-- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/examples/worker-threads/node.js b/examples/worker-threads/node.js index 9fac723..977095f 100644 --- a/examples/worker-threads/node.js +++ b/examples/worker-threads/node.js @@ -11,4 +11,12 @@ suite .add('Using import with node: prefix', function () { return import('node:fs'); }) + .add('async test', async function (timer) { + timer.start(); + let i = 0; + while (i++ < timer.count) { + await import("node:fs"); + } + timer.end(timer.count); + }) .run(); diff --git a/lib/worker-runner.js b/lib/worker-runner.js index 7425d60..dfb043c 100644 --- a/lib/worker-runner.js +++ b/lib/worker-runner.js @@ -1,9 +1,10 @@ const { parentPort } = require("node:worker_threads"); const { runBenchmark } = require("./lifecycle"); +const AsyncFunction = Object.getPrototypeOf(async () => {}).constructor; // Deserialize the benchmark function function deserializeBenchmark(benchmark) { - benchmark.fn = new Function(benchmark.fn); + benchmark.fn = new AsyncFunction("timer", benchmark.fn); } parentPort.on( diff --git a/test/worker.js b/test/worker.js index c4de713..9012996 100644 --- a/test/worker.js +++ b/test/worker.js @@ -21,7 +21,16 @@ describe("Using worker_threads", () => { }) .add("Import without node: prefix", () => { return import("node:fs"); + }) + .add("async test", async (timer) => { + timer.start(); + let i = 0; + while (i++ < timer.count) { + await import("node:fs"); + } + timer.end(timer.count); }); + await bench.run(); }); @@ -29,7 +38,7 @@ describe("Using worker_threads", () => { mock.restoreAll(); }); - it("should create a new Worker 2 times", () => { - assert.strictEqual(workerThreads.Worker.mock.calls.length, 2); + it("should create a new Worker 3 times", () => { + assert.strictEqual(workerThreads.Worker.mock.calls.length, 3); }); }); From d30e4b78b8b7e815297bda4a49a380364278f694 Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 8 Dec 2025 12:33:56 -0800 Subject: [PATCH 2/2] chore: Code review revisions. Emulate the same optimizations from clock.js in worker-runner.js. This will only create an async function if the origin function is async, and will only trigger the Timer logic flow if the origin function contained the same. --- lib/worker-runner.js | 9 ++++++++- test/worker.js | 9 ++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/worker-runner.js b/lib/worker-runner.js index dfb043c..3b49c10 100644 --- a/lib/worker-runner.js +++ b/lib/worker-runner.js @@ -4,7 +4,14 @@ const AsyncFunction = Object.getPrototypeOf(async () => {}).constructor; // Deserialize the benchmark function function deserializeBenchmark(benchmark) { - benchmark.fn = new AsyncFunction("timer", benchmark.fn); + const { isAsync, hasArg } = benchmark; + const fnPrototype = isAsync ? AsyncFunction : Function; + + if (hasArg) { + benchmark.fn = new fnPrototype("timer", benchmark.fn); + } else { + benchmark.fn = new fnPrototype(benchmark.fn); + } } parentPort.on( diff --git a/test/worker.js b/test/worker.js index 9012996..95c3396 100644 --- a/test/worker.js +++ b/test/worker.js @@ -22,7 +22,10 @@ describe("Using worker_threads", () => { .add("Import without node: prefix", () => { return import("node:fs"); }) - .add("async test", async (timer) => { + .add("async test", async () => { + return import("node:fs"); + }) + .add("async with timer", async (timer) => { timer.start(); let i = 0; while (i++ < timer.count) { @@ -38,7 +41,7 @@ describe("Using worker_threads", () => { mock.restoreAll(); }); - it("should create a new Worker 3 times", () => { - assert.strictEqual(workerThreads.Worker.mock.calls.length, 3); + it("should create a new Worker 4 times", () => { + assert.strictEqual(workerThreads.Worker.mock.calls.length, 4); }); });