概述
原文链接: nodejs cluster 利用多核cpu
上一篇: nodejs worker_threads 简单使用
下一篇: nodejs crypto 加密 对称加密 非对称加密
使用 cluster 优化fib
普通的方法求前40项, 3000ms左右, 用cluster可降到2500ms左右
const cluster = require('cluster');
function fib(n = 1) {
return n < 2 ? n : fib(n - 1) + fib(n - 2)
}
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
// Fork workers.
let st = +new Date()
let cnt = 0
let size = 40
let arr = []
for (let i = 0; i <= size; i++) {
let w = cluster.fork();
// console.log('w', w)
w.process.send(i)
w.process.on('message', (data) => {
cnt++
// console.log('fib', data)
arr.push(data)
if (cnt === size + 1) {
console.log(arr.sort((a, b) => a - b))
console.log('end', +new Date() - st)
}
})
}
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
});
} else {
process.on('message', (msg) => {
// console.log('child', msg)
process.send(fib(msg))
process.exit()
})
}
//
14930352, 24157817, 39088169, 63245986,
//
102334155
// ]
// end 2534
对于cpu核数的影响, 先求 [30, 40]之间的fib
function fib(n = 1) {
return n < 2 ? n : fib(n - 1) + fib(n - 2)
}
let st = +new Date()
for (let i = 30; i <= 40; i++) {
let f = fib(i)
console.log(f)
}
let ed = +new Date()
console.log(ed - st) // 3094
// 832040
// 1346269
// 2178309
// 3524578
// 5702887
// 9227465
// 14930352
// 24157817
// 39088169
// 63245986
// 102334155
// 3240
这次就相对好点, 因为worker数目小于cpu核数, 效果会好一点
5702887,
9227465,
14930352, 24157817,
39088169, 63245986,
102334155
]
end 1661
但是限制worker数目, 并不能提高太多, 猜测还是ipc通讯效率问题
const cluster = require('cluster');
const numCPUs = require('os').cpus().length;
function fib(n = 1) {
return n < 2 ? n : fib(n - 1) + fib(n - 2)
}
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
// Fork workers.
let st = +new Date()
let cnt = 0
let size = 40
let arr = []
let now = 0
for (let i = 0; i < 8; i++) {
let w = cluster.fork();
now++
w.process.send(i)
w.process.on('message', (data) => {
cnt++
// console.log('fib', data)
arr.push(data)
w.process.send(now++)
if (cnt === size + 1) {
console.log(arr.sort((a, b) => a - b))
console.log('end', +new Date() - st)
}
})
}
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
});
} else {
process.on('message', (msg) => {
// console.log('child', msg)
process.send(fib(msg))
// process.exit()
})
}
按照配置负载均衡, windows上失败?
开10个worker, 是只有大量并发一万的情况下, 只能获得3个不同的server信息
http://blog.fens.me/nodejs-core-cluster/
减少worker数目成功
server 端
var cluster = require('cluster');
var http = require('http');
if (cluster.isMaster) {
console.log('[master] ' + "start master...");
for (var i = 0; i < 8; i++) {
cluster.fork();
}
cluster.on('listening', function (worker, address) {
console.log('[master] ' + 'listening: worker' + worker.id + ',pid:' + worker.process.pid + ', Address:' + address.address + ":" + address.port);
});
} else if (cluster.isWorker) {
console.log('[worker] ' + "start worker ..." + cluster.worker.id);
http.createServer(function (req, res) {
console.log('worker'+cluster.worker.id);
res.end('worker'+cluster.worker.id+',PID:'+process.pid);
}).listen(3000);
}
模拟请求, 返回不同的信息
const axios = require('axios')
let url = 'http://localhost:3000'
let s = new Set()
for (let i = 0; i < 10000; i++) {
axios.get(url).then(
({data}) => {
s.add(data)
console.log(s.size)
}
)
}
// 8
// 8
// 8
// 8
// 8
// 8
// 8
最后
以上就是会撒娇跳跳糖为你收集整理的nodejs cluster 利用多核cpu的全部内容,希望文章能够帮你解决nodejs cluster 利用多核cpu所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复