Files
salvium-rs/test/test-workers-parallel.js
T
Matt Hess 7730b6993f ● Add AssemblyScript WASM VM for RandomX full mode
- Create assembly/vm.ts with full RandomX VM implementation
    - 256 instructions, 2048 iterations per hash
    - Native u64/f64 operations in WebAssembly
    - Full mode dataset lookups (2GB pre-computed)

  - Update miner to use WASM VM for full mode
    - mining-worker-asm.js uses pre-compiled WASM
    - ~32 H/s per thread (4x faster than light mode)
    - 8 threads achieves ~260 H/s

  - Clean up redundant code
    - Remove mining-worker-full.js (old JIT approach)
    - Consolidate 'asm' mode into 'full' mode
2026-01-18 01:16:10 +00:00

86 lines
2.3 KiB
JavaScript

/**
* Test Worker Threads Parallelism
*
* Verifies that worker threads actually run in parallel.
*/
import { Worker, isMainThread, parentPort, workerData } from 'worker_threads';
import { fileURLToPath } from 'url';
if (!isMainThread) {
// Worker code
const { workerId } = workerData;
// Do CPU-intensive work
let count = 0;
const start = Date.now();
// Count for 2 seconds
while (Date.now() - start < 2000) {
count++;
// Simulate work
for (let i = 0; i < 1000; i++) {
Math.sqrt(i);
}
}
parentPort.postMessage({ workerId, count });
} else {
// Main thread
async function runTest(numWorkers) {
console.log(`\nTesting with ${numWorkers} worker(s)...`);
const workerPath = fileURLToPath(import.meta.url);
const promises = [];
for (let i = 0; i < numWorkers; i++) {
promises.push(new Promise((resolve, reject) => {
const worker = new Worker(workerPath, {
workerData: { workerId: i }
});
worker.on('message', (msg) => {
resolve(msg);
worker.terminate();
});
worker.on('error', reject);
}));
}
const results = await Promise.all(promises);
let totalCount = 0;
for (const { workerId, count } of results) {
console.log(` Worker ${workerId}: ${count.toLocaleString()} iterations`);
totalCount += count;
}
console.log(` Total: ${totalCount.toLocaleString()} iterations`);
return totalCount;
}
async function main() {
console.log('Worker Threads Parallelism Test');
console.log('================================');
const result1 = await runTest(1);
const result2 = await runTest(2);
const result4 = await runTest(4);
console.log('\nResults:');
console.log(` 1 worker: ${result1.toLocaleString()} iterations (baseline)`);
console.log(` 2 workers: ${result2.toLocaleString()} iterations (${(result2 / result1).toFixed(2)}x)`);
console.log(` 4 workers: ${result4.toLocaleString()} iterations (${(result4 / result1).toFixed(2)}x)`);
if (result4 >= result1 * 2) {
console.log('\n✓ Worker threads are running in parallel!');
} else {
console.log('\n✗ Worker threads may NOT be running in parallel');
console.log(' Expected 4 workers to do at least 2x work of 1 worker');
}
}
main().catch(console.error);
}