Created
March 21, 2019 19:30
-
-
Save saii9/b1c82dad4657b60e22fbcff369762f44 to your computer and use it in GitHub Desktop.
Node spawn delay test
This file contains 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 cluster = require('cluster'); | |
if(cluster.isMaster){ | |
function test(num){ | |
let workers = {}; | |
let count = num; | |
for (let i = 0; i < num; i++) { | |
workers[i] = []; | |
workers[i].push((new Date).getTime()); | |
const worker = cluster.fork(); | |
workers[i].push(worker); | |
const workernum = i; | |
worker.on('online', function () { | |
workers[workernum].push((new Date).getTime() - workers[i][0]) | |
}); | |
worker.on('message', function (message) { | |
if(! --count){ | |
let total = 0; | |
Object.keys(workers).forEach(key=>{ | |
workers[key][1].kill() | |
total = total + workers[key][2] | |
}); | |
console.log(`${num} ----- ${total/num}`) | |
setImmediate(()=> test(num + (num/5)*5)) | |
} | |
}); | |
} | |
} | |
test(5) | |
} else{ | |
process.send(new Date().getMilliseconds()); | |
setTimeout(process.exit, 600000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment