@duanyubin
2016-04-12T03:06:04.000000Z
字数 4168
阅读 455
NODE
Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.
| I/O | CPU Cycle |
|---|---|
| L1-cache | 3 |
| L2-cache | 14 |
| RAM | 250 |
| Disk | 41000000 |
| Network | 240000000 |


每个 request 都在单独的线程中处理,即使某一个请求发生很严重的错误也不会影响到其它请求
通过fork的方法,依据CPU核心数,创建对应的进程数
多个进程之间会竞争 accpet 一个连接,产生惊群现象,效率比较低。
由于无法控制一个新的连接由哪个进程来处理,必然导致各 worker 进程之间的负载非常不均衡。
通过nginx处理负载均衡
http {upstream cluster {server 127.0.0.1:3000;server 127.0.0.1:3001;server 127.0.0.1:3002;server 127.0.0.1:3003;}server {listen 80;server_name www.domain.com;location / {proxy_pass http://cluster;}}}
var cluster = require('cluster');var http = require('http');var os = require('os');var numCPUs = os.cpus().length;if (cluster.isMaster) {// Master:// Let's fork as many workers as you have CPU coresfor (var i = 0; i < numCPUs; ++i) {cluster.fork();}} else {// Worker:// Let's spawn a HTTP server// (Workers can share any TCP connection.// In this case its a HTTP server)http.createServer(function(req, res) {res.writeHead(200);res.end("hello world");}).listen(8080);}
pm2 start app.js -i 0
const http = require('http')const hostname = '127.0.0.1'const port = 3000const server = http.createServer((req, res) => {res.statusCode = 200res.setHeader('Content-Type', 'text/plain')res.end('Hello World\n')})server.listen(port, hostname, () => {console.log(`Server running at http://${hostname}:${port}/`)})
naughtyDog:~ dyb$ loadtest -n 10000 -c 100 http://127.0.0.1:3000/
naughtyDog:~ dyb$ loadtest -n 10000 -c 100 http://127.0.0.1:3000/[Mon Apr 11 2016 14:11:43 GMT+0800 (CST)] INFO Requests: 0 (0%), requests per second: 0, mean latency: 0 ms[Mon Apr 11 2016 14:11:48 GMT+0800 (CST)] INFO Requests: 6378 (64%), requests per second: 1275, mean latency: 60 ms[Mon Apr 11 2016 14:11:53 GMT+0800 (CST)] INFO Requests: 8148 (81%), requests per second: 353, mean latency: 340 ms[Mon Apr 11 2016 14:11:55 GMT+0800 (CST)] INFO[Mon Apr 11 2016 14:11:55 GMT+0800 (CST)] INFO Target URL: http://127.0.0.1:3000/[Mon Apr 11 2016 14:11:55 GMT+0800 (CST)] INFO Max requests: 10000[Mon Apr 11 2016 14:11:55 GMT+0800 (CST)] INFO Concurrency level: 100[Mon Apr 11 2016 14:11:55 GMT+0800 (CST)] INFO Agent: none[Mon Apr 11 2016 14:11:55 GMT+0800 (CST)] INFO[Mon Apr 11 2016 14:11:55 GMT+0800 (CST)] INFO Completed requests: 10000[Mon Apr 11 2016 14:11:55 GMT+0800 (CST)] INFO Total errors: 0[Mon Apr 11 2016 14:11:55 GMT+0800 (CST)] INFO Total time: 12.325297339 s[Mon Apr 11 2016 14:11:55 GMT+0800 (CST)] INFO Requests per second: 811[Mon Apr 11 2016 14:11:55 GMT+0800 (CST)] INFO Total time: 12.325297339 s[Mon Apr 11 2016 14:11:55 GMT+0800 (CST)] INFO[Mon Apr 11 2016 14:11:55 GMT+0800 (CST)] INFO Percentage of the requests served within a certain time[Mon Apr 11 2016 14:11:55 GMT+0800 (CST)] INFO 50% 60 ms[Mon Apr 11 2016 14:11:55 GMT+0800 (CST)] INFO 90% 83 ms[Mon Apr 11 2016 14:11:55 GMT+0800 (CST)] INFO 95% 98 ms[Mon Apr 11 2016 14:11:55 GMT+0800 (CST)] INFO 99% 4052 ms[Mon Apr 11 2016 14:11:55 GMT+0800 (CST)] INFO 100% 4103 ms (longest request)
naughtyDog:~ dyb$ loadtest -n 10000 -c 100 http://127.0.0.1:3000/[Mon Apr 11 2016 14:11:19 GMT+0800 (CST)] INFO Requests: 0 (0%), requests per second: 0, mean latency: 0 ms[Mon Apr 11 2016 14:11:24 GMT+0800 (CST)] INFO Requests: 7812 (78%), requests per second: 1560, mean latency: 60 ms[Mon Apr 11 2016 14:11:26 GMT+0800 (CST)] INFO[Mon Apr 11 2016 14:11:26 GMT+0800 (CST)] INFO Target URL: http://127.0.0.1:3000/[Mon Apr 11 2016 14:11:26 GMT+0800 (CST)] INFO Max requests: 10000[Mon Apr 11 2016 14:11:26 GMT+0800 (CST)] INFO Concurrency level: 100[Mon Apr 11 2016 14:11:26 GMT+0800 (CST)] INFO Agent: none[Mon Apr 11 2016 14:11:26 GMT+0800 (CST)] INFO[Mon Apr 11 2016 14:11:26 GMT+0800 (CST)] INFO Completed requests: 10000[Mon Apr 11 2016 14:11:26 GMT+0800 (CST)] INFO Total errors: 0[Mon Apr 11 2016 14:11:26 GMT+0800 (CST)] INFO Total time: 7.234391098 s[Mon Apr 11 2016 14:11:26 GMT+0800 (CST)] INFO Requests per second: 1382[Mon Apr 11 2016 14:11:26 GMT+0800 (CST)] INFO Total time: 7.234391098 s[Mon Apr 11 2016 14:11:26 GMT+0800 (CST)] INFO[Mon Apr 11 2016 14:11:26 GMT+0800 (CST)] INFO Percentage of the requests served within a certain time[Mon Apr 11 2016 14:11:26 GMT+0800 (CST)] INFO 50% 62 ms[Mon Apr 11 2016 14:11:26 GMT+0800 (CST)] INFO 90% 85 ms[Mon Apr 11 2016 14:11:26 GMT+0800 (CST)] INFO 95% 96 ms[Mon Apr 11 2016 14:11:26 GMT+0800 (CST)] INFO 99% 624 ms[Mon Apr 11 2016 14:11:26 GMT+0800 (CST)] INFO 100% 627 ms (longest request)