nodejs进程线程优化性能
1. node.js 单线程的特点
node.js 以异步非阻塞单线程,作为其执行速度的保障。什么是非阻塞单线程?
举一个现实生活中的例子,我去巢大食堂打饭,我选择了A套餐,然后工作人员区为我配餐,我就在旁边等着直到阿姨叫号然后我取餐。这个过程是同步。
如果工作人员在为我配餐的同时,仍然可以接受排在我后面的同学的点餐,这样餐厅并没有因为我在等待A套餐而停止。这个过程是非阻塞。
如果我在阿姨为我配餐的同时,我去旁边小超市买了杯冷饮,等阿姨叫号时我再去取餐。这个过程就是异步非阻塞,同时阿姨叫号我去取餐的行为叫做回调。
- 高性能(不用考虑多线程间来回调用引起性能的损耗)
- 线程安全(不用担心同意变量会被多线程进行读写而造成程序的崩溃)
- 底层多线程
说node.js 是单线程其实也是不全面的,node.js 底层库会使用libuv调用多线程来处理I/O 操作。这就像食堂只有一个窗口,只能有按顺序一个个的接收点餐,但是后厨配菜的员工却有很多,他们各司其职保证出餐的速度。
如果服务器是多核且有充足的物理资源,如何充分发挥这些物理资源达到性能最大化。既食堂后厨有很多大师傅,一个窗口售卖的速度太慢,许多大师傅都是空闲的窗台。邪恶的资本家你们懂的?
2.如何通过多线程提高node.js 的性能
- cluster: 为了利用多核系统,用户有时会想启动一个 Node.js 进程的集群去处理负载。
const Koa = require('Koa'); const koaRouter = require('koa-router'); const app = new Koa(); const router = new koaRouter(); function fibo(n) { return n > 1 ? fibo(n - 1) + fibo(n-2) : 1 } app.use(router['routes']()); router.get('/', function(ctx, next) { var result = fibo(35); ctx.body = `${result}`; }); if (!module.parent) { app.listen(8080); console.log(`Server was start.`); }
通过ab 压力测试命令:
ab -c 20 -n 100 http://localhost:8080/
没有经过cluster 集群优化:
Server Software: Server Hostname: localhost Server Port: 8080 Document Path: / Document Length: 8 bytes Concurrency Level: 20 Time taken for tests: 13.569 seconds Complete requests: 100 Failed requests: 0 Total transferred: 14300 bytes HTML transferred: 800 bytes Requests per second: 7.37 [#/sec] (mean) Time per request: 2713.723 [ms] (mean) Time per request: 135.686 [ms] (mean, across all concurrent requests) Transfer rate: 1.03 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.2 0 1 Processing: 155 2465 622.9 2738 2762 Waiting: 153 2465 623.0 2737 2762 Total: 155 2465 622.8 2738 2762 Percentage of the requests served within a certain time (ms) 50% 2738 66% 2743 75% 2746 80% 2747 90% 2753 95% 2757 98% 2761 99% 2762 100% 2762 (longest request)
通过cluster 集群优化后:
const Koa = require('Koa'); const koaRouter = require('koa-router'); const numCpus = require('os').cpus().length; const cluster = require('cluster'); const app = new Koa(); const router = new koaRouter(); if (cluster.isMaster) { console.log(`${numCpus}`); for (var index = 0; index < numCpus; index++) { cluster.fork(); } } else { app.use(router['routes']()); router.get('/', function(ctx, next) { var result = fibo(35); ctx.body = `${result}`; }); if (!module.parent) { app.listen(8080); console.log(`Server was start.`); } } function fibo(n) { return n > 1 ? fibo(n - 1) + fibo(n-2) : 1 }
通过ab 压力测试命令:
ab -c 20 -n 100 http://localhost:8080/
Server Software: Server Hostname: localhost Server Port: 8080 Document Path: / Document Length: 8 bytes Concurrency Level: 20 Time taken for tests: 6.513 seconds Complete requests: 100 Failed requests: 0 Total transferred: 14300 bytes HTML transferred: 800 bytes Requests per second: 15.35 [#/sec] (mean) Time per request: 1302.524 [ms] (mean) Time per request: 65.126 [ms] (mean, across all concurrent requests) Transfer rate: 2.14 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.5 0 2 Processing: 279 1198 258.7 1294 1335 Waiting: 279 1198 258.7 1294 1335 Total: 281 1198 258.3 1295 1335 Percentage of the requests served within a certain time (ms) 50% 1295 66% 1301 75% 1303 80% 1308 90% 1322 95% 1328 98% 1333 99% 1335 100% 1335 (longest request)
对比两次的测试结果:
优化后,
Requests per second (每秒处理的任务数) 由 7.37 提高到 15.35.
Time per request (每个用户的平均处理时间) 由 135.686 降低到 65.126
3. 如何通过多进程提高node.js 的性能
- Child Process 创建进程
main.js
const Koa = require('Koa'); const koaRouter = require('koa-router'); const fork = require('child_process').fork; const app = new Koa(); const router = new koaRouter(); app.use(router['routes']()); router.get('/', function(ctx, next) { var worker = fork('./work_fibo.js'); worker.on('message', function(m) { if ('object' === typeof m && m.type === 'fibo') { worker.kill(); ctx.body = m.result.toString(); } }); worker.send({type: "fibo", num: 35}, (err) => { console.log(`${err}`); }); console.log(`${worker.pid}`); }); if (!module.parent) { app.listen(8080); console.log(`Server was start.`); }
work_fibo.js
var fibo = function fibo(n) { return n > 1 ? fibo(n - 1) + fibo(n - 2) : 1; } process.on('message', function(m) { if (typeof m === 'object' && m.type === 'fibo') { var num = fibo(m.num); process.send({type: 'fibo', result: num}); } }); process.on('SIGHUP', function() { process.exist(); });
通过ab 压力测试命令:
ab -c 20 -n 100 http://localhost:8080/
Server Software: Server Hostname: localhost Server Port: 8080 Document Path: / Document Length: 9 bytes Concurrency Level: 20 Time taken for tests: 3.619 seconds Complete requests: 100 Failed requests: 0 Non-2xx responses: 100 Total transferred: 15100 bytes HTML transferred: 900 bytes Requests per second: 27.63 [#/sec] (mean) Time per request: 723.764 [ms] (mean) Time per request: 36.188 [ms] (mean, across all concurrent requests) Transfer rate: 4.07 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 1 4.5 0 43 Processing: 21 611 270.7 650 1134 Waiting: 18 610 270.7 649 1132 Total: 22 612 270.2 652 1134 Percentage of the requests served within a certain time (ms) 50% 652 66% 744 75% 794 80% 835 90% 958 95% 1054 98% 1122 99% 1134 100% 1134 (longest request)
对比两次的测试结果:
优化后,
Requests per second (每秒处理的任务数) 由 7.37 提高到 15.35 最后通过进程优化后达到 27.63
Time per request (每个用户的平均处理时间) 由 135.686 降低到 65.126 最后通过进程优化后达到 36.188
转载:https://www.jianshu.com/p/f25388f030be