Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions examples/worker-threads/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
10 changes: 9 additions & 1 deletion lib/worker-runner.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
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);
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(
Expand Down
16 changes: 14 additions & 2 deletions test/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,27 @@ describe("Using worker_threads", () => {
})
.add("Import without node: prefix", () => {
return import("node:fs");
})
.add("async test", async () => {
return import("node:fs");
})
.add("async with timer", async (timer) => {
timer.start();
let i = 0;
while (i++ < timer.count) {
await import("node:fs");
}
timer.end(timer.count);
});

await bench.run();
});

after(() => {
mock.restoreAll();
});

it("should create a new Worker 2 times", () => {
assert.strictEqual(workerThreads.Worker.mock.calls.length, 2);
it("should create a new Worker 4 times", () => {
assert.strictEqual(workerThreads.Worker.mock.calls.length, 4);
});
});
Loading