forked from moshen/wasmagic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.mjs
More file actions
42 lines (36 loc) · 1.27 KB
/
index.mjs
File metadata and controls
42 lines (36 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import * as fs from "node:fs/promises";
import * as assert from "node:assert";
import * as path from "node:path";
import * as os from "node:os";
import { Piscina } from "piscina";
import { cases as testCases } from "../../dist/test/integration/data.js";
const numCpus = os.cpus().length - 1;
const wasmagicWorkerPool = new Piscina({
filename: "./worker.mjs",
minThreads: numCpus,
maxThreads: numCpus,
idleTimeout: 10000,
});
// Fill Buffers
for (const testCase of testCases) {
const file = await fs.open(path.join("..", "..", testCase[0]));
const stats = await file.stat();
// Copying directly into a SharedArrayBuffer is faster than copying
// the buffer to the worker.
// If you're loading from a file, you could just pass the file path
// instead of the Buffer, and load the file from the worker thread.
const sharedBuf = Buffer.from(new SharedArrayBuffer(stats.size));
const { bytesRead } = await file.read({ buffer: sharedBuf });
await file.close();
assert.equal(stats.size, bytesRead);
testCase.push(sharedBuf);
}
// Kick off the workers
for (const testCase of testCases) {
testCase.push(wasmagicWorkerPool.run(testCase[3]));
}
// Check results
for (const testCase of testCases) {
const result = await testCase[4];
assert.equal(result, testCase[1]);
}