Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Worker Pool Test #3697

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
9 changes: 8 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions packages/evm/examples/keccakWorker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { keccak256 } from 'ethereum-cryptography/keccak.js'
import workerpool from 'workerpool'

function keccak256AB(i) {
const res = keccak256(new Uint8Array(i))
return res.buffer
}

workerpool.worker({
keccak256AB,
})
57 changes: 57 additions & 0 deletions packages/evm/examples/workerPool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { keccak256 } from 'ethereum-cryptography/keccak.js'
import path from 'path'
import { fileURLToPath } from 'url'
import workerpool from 'workerpool'

// Additional fix:
// https://iamwebwiz.medium.com/how-to-fix-dirname-is-not-defined-in-es-module-scope-34d94a86694d
const __filename = fileURLToPath(import.meta.url) // get the resolved path to the file
const __dirname = path.dirname(__filename) // get the name of the directory

const pool = workerpool.pool()
const pool2 = workerpool.pool(__dirname + '/keccakWorker.js')

function add(a: number, b: number) {
return a + b
}

function transferAB(i: ArrayBuffer) {
console.log(i)
console.log(new Uint8Array(i))
}

const main = async () => {
console.time('t')
for (let i = 0; i < 1000; i++) {
if (i % 2 === 0) {
const res = keccak256(new Uint8Array([1, 2, 3, 4, 5]))
console.log(res)
} else {
// First simple test to see if things work at all
/*await pool.exec(add, [1, 2]).then(function (result) {
console.log(result) // outputs 7
})*/
// Does not work ("ReferenceError: assert is not defined"), likely because datatype not allowed
/*await pool.exec(keccak256, [new Uint8Array([1, 2, 3, 4, 5])]).then(function (result) {
console.log(result) // outputs 7
})*/
// Uint8Array does not work, so we need to transfer the underlying ArrayBuffer, see e.g.
// https://advancedweb.hu/how-to-transfer-binary-data-efficiently-across-worker-threads-in-nodejs/
// https://stackoverflow.com/questions/37228285/uint8array-to-arraybuffer
/*await pool.exec(transferAB, [new Uint8Array([1, 2, 3, 4, 5]).buffer]).then(function (result) {
console.log(result) // outputs 7
})*/
// For keccak we cannot import the keccak256 function into a dynamic worker function,
// so now we need to switch to dedicated workers
await pool2
.exec('keccak256AB', [new Uint8Array([1, 2, 3, 4, 5]).buffer])
.then(function (result) {
console.log(new Uint8Array(result))
})
}
}
console.timeEnd('t')
await pool.terminate()
}

void main()
1 change: 1 addition & 0 deletions packages/evm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"benchmark": "^2.1.4",
"level": "^8.0.0",
"mcl-wasm": "^1.5.0",
"workerpool": "^9.1.3",
"memory-level": "^1.0.0",
"minimist": "^1.2.5",
"node-dir": "^0.1.17",
Expand Down
Loading