Created
May 17, 2019 06:31
-
-
Save NeoyeElf/90c1b9568b03082fcb6caa9f547d7a1c to your computer and use it in GitHub Desktop.
worker threads vs fork process
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Benchmark = require('benchmark') | |
const doThreadsJob = require('./microjob') | |
const doProcessJob = require('./workform') | |
var suite = new Benchmark.Suite; | |
suite | |
.add('worker threads', { | |
defer: true, | |
fn: function (deferred) { | |
console.log('doing threads job...') | |
doThreadsJob().then(() => { | |
deferred.resolve() | |
}).catch(err => { | |
throw err | |
}) | |
} | |
}) | |
.add('fork process', { | |
defer: true, | |
fn: function (deferred) { | |
console.log('doing process job...') | |
doProcessJob().then(() => { | |
deferred.resolve() | |
}).catch(err => { | |
throw err | |
}) | |
} | |
}) | |
// add listeners | |
.on('cycle', function(event) { | |
console.log(String(event.target)); | |
}) | |
.on('complete', function() { | |
console.log('Fastest is ' + this.filter('fastest').map('name')); | |
}) | |
.on('error', function(err) { | |
console.log('error occured!', err) | |
}) | |
// run async | |
.run({ 'async': true }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
heavyJob: data => { | |
let i = 0; | |
let res = typeof data === "number" ? data : data.n | |
for (i = 0; i < 100000000; i++) { | |
res += 1 | |
} | |
return res | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { start, job } = require("microjob"); | |
const { heavyJob } = require('./demo-job'); | |
let jobStarted = false | |
async function ready() { | |
if (jobStarted) { | |
return | |
} | |
await start({ maxWorkers: 2 }) | |
} | |
async function executeJob(n) { | |
await job(heavyJob, { data: { n } }) | |
} | |
async function main() { | |
await ready() | |
await executeJob(0) | |
} | |
// main().catch(console.error) | |
module.exports = main |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { heavyJob } = require('./demo-job'); | |
exports.heavyJob = wrap(heavyJob) | |
function wrap (fn) { | |
return function (arg, callback) { | |
try { | |
const ret = fn(arg) | |
callback(null, ret) | |
} catch (e) { | |
callback(e) | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const workerFarm = require('worker-farm'); | |
const workers = workerFarm( | |
{ | |
maxConcurrentWorkers: 2, | |
maxConcurrentCallsPerWorker: 1, | |
}, | |
require.resolve('./workers.js'), | |
[ | |
'heavyJob', | |
] | |
); | |
async function main() { | |
return new Promise((resolve, reject) => { | |
workers.heavyJob(0, (err, out) => { | |
if (err) { | |
return reject(err); | |
} | |
resolve(out); | |
}); | |
}); | |
} | |
// main().then(console.log).catch(console.error) | |
module.exports = main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Instead of this:
you can use: