NodeJS 分布式和多进程的测试与记录


目的:测试与记录高并发环境下,分布式多进程 的情况
测试:对来访数据进行记录,类似日志的处理
目录:

  • 测试环境 — 软、硬件
  • 单服务器单进程
  • 单服务器多进程
  • 多服务器单进程
  • 多服务器多进程

备注:该测试不含数据库操作

测试环境

- CPU RAM(GB) OS OS type NodeJS
Server 00 Intel(R) Core(TM) i5-8250U
1.60GHz x 4 1.80GHz
8.00 Win10 x64 v8.11.4
Server 01 Intel(R) Core(TM) i5-3470S
2.90GHz x 4 2.90GHz
8.00 Win10 x64 v10.16.0
Server 02 Intel(R) Core(TM) i5-3470S
3.20GHz x 4
9.60 Ubuntu 18.04.2 LTS x64 v10.16.0
ApacheBench v2.3
nginx 1.14.2

单服务器,单进程(利用 Server 02)

Server 搭建
利用 http 模块搭建服务端。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const http = require('http')
const fs = require('fs')

let num = 0

http.createServer((req, res) => {
++num
const file = './log/file.txt'
const data = JSON.stringify(Object.assign({
num,
create_time: new Date()
}, req.headers)) + "\n"

fs.open(file, 'a', (err, fd) => {
if (err) throw err

fs.appendFile(file, data , 'utf8', (err) => {
if (err) throw err

fs.close(fd, (err) => {
if (err) throw err

res.writeHead(200, {'Content-Type': 'text/plain'})
res.end('success')
})
})
})
}).listen(8080, () => {
console.log('<<< [ node server ready ] <<<')
})

ApacheBench 测试
执行 2000 并发,10000 请求的 Http Keep-Alive 压测

1
ab -c2000 -n10000 -k http://[ip:port]/

返回结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
This is ApacheBench, Version 2.3 <$Revision: 1826310 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking [ip] (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests


Server Software:
Server Hostname: [ip]
Server Port: [port]

Document Path: /
Document Length: 7 bytes

Concurrency Level: 2000
Time taken for tests: 25.023 seconds # 总共花费 25 秒左右
Complete requests: 10000
Failed requests: 0
Total transferred: 1080000 bytes
HTML transferred: 70000 bytes
Requests per second: 399.63 [#/sec] (mean) # 平均每秒接受请求个数 400,相当于LR中的每秒事务数
Time per request: 5004.594 [ms] (mean) # 平均每次请求花费 5005 毫秒,相当于LR中的平均事务响应时间
Time per request: 2.502 [ms] (mean, across all concurrent requests) # 每个连接请求实际运行时间的平均值
Transfer rate: 42.15 [Kbytes/sec] received # 平均每秒网络上的流量,可以帮助排除是否存在网络流量过大导致响应时间延长的问题

Connection Times (ms)
min mean[+/-sd] median max
Connect: 1 2 0.5 2 13
Processing: 112 4510 1145.8 4973 5065
Waiting: 22 2509 1425.1 2515 5040
Total: 114 4513 1145.8 4976 5067

Percentage of the requests served within a certain time (ms)
50% 4976
66% 5013
75% 5023
80% 5034
90% 5044
95% 5050
98% 5054
99% 5061
100% 5067 (longest request)


单服务器,多进程(利用 Server 02)

Server 搭建
利用 cluster 模块进行多进程处理。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const cluster = require('cluster')
const http = require('http')
const numCPUs = require('os').cpus().length
const fs = require('fs')

if (cluster.isMaster) {
for (let i = 0; i < numCPUs; i++) {
cluster.fork()
}
} else {
let num = 0
http.createServer((req, res) => {
++num
const file = './log/file.txt'
const data = JSON.stringify(Object.assign({
num,
create_time: new Date()
}, req.headers)) + "\n"

fs.open(file, 'a', (err, fd) => {
if (err) throw err

fs.appendFile(file, data , 'utf8', (err) => {
if (err) throw err

fs.close(fd, (err) => {
if (err) throw err

res.writeHead(200, {'Content-Type': 'text/plain'})
res.end(`${process.pid} success`)
// process.kill(process.pid)
})
})
})
}).listen(8080, () => {
console.log(`<<< [ node server ready by pid:${process.pid} ] <<<`)
})
}

ApacheBench 测试
执行 2000 并发,10000 请求的 Http Keep-Alive 压测

1
ab -c2000 -n10000 -k http://[ip:port]/

返回结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
This is ApacheBench, Version 2.3 <$Revision: 1826310 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking [ip] (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests


Server Software:
Server Hostname: [ip]
Server Port: [port]

Document Path: /
Document Length: 12 bytes

Concurrency Level: 2000
Time taken for tests: 26.649 seconds # 总共花费 27 秒左右
Complete requests: 10000
Failed requests: 0
Keep-Alive requests: 0
Total transferred: 1130000 bytes
HTML transferred: 120000 bytes
Requests per second: 375.26 [#/sec] (mean) # 平均每秒接受请求个数 375
Time per request: 5329.704 [ms] (mean) # 平均每次请求花费 5330 毫秒
Time per request: 2.665 [ms] (mean, across all concurrent requests)
Transfer rate: 41.41 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 1 2 0.8 2 33
Processing: 111 4833 1359.5 5026 6156
Waiting: 23 2615 1544.7 2565 5868
Total: 113 4835 1359.6 5028 6159

Percentage of the requests served within a certain time (ms)
50% 5028
66% 5520
75% 5747
80% 5887
90% 5990
95% 6050
98% 6140
99% 6149
100% 6159 (longest request)


多服务器,单进程

Server 搭建
利用 nginx 进行分发。

ApacheBench 测试
执行 2000 并发,10000 请求的 Http Keep-Alive 压测

1
ab -c2000 -n10000 -k http://[ip:port]/

返回结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
This is ApacheBench, Version 2.3 <$Revision: 1826310 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking [ip] (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests


Server Software: nginx/1.14.2
Server Hostname: [ip]
Server Port: [port]

Document Path: /
Document Length: 7 bytes

Concurrency Level: 2000
Time taken for tests: 8.483 seconds # 总共花费 8 秒左右
Complete requests: 10000
Failed requests: 0
Keep-Alive requests: 0
Total transferred: 1300000 bytes
HTML transferred: 70000 bytes
Requests per second: 1178.82 [#/sec] (mean) # 平均每秒接受请求个数 1178
Time per request: 1696.606 [ms] (mean) # 平均每次请求花费 1696 毫秒
Time per request: 0.848 [ms] (mean, across all concurrent requests)
Transfer rate: 149.66 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 16.6 0 502
Processing: 112 1509 456.6 1581 2099
Waiting: 48 1029 515.5 840 1994
Total: 112 1510 456.8 1581 2099

Percentage of the requests served within a certain time (ms)
50% 1581
66% 1592
75% 1594
80% 1608
90% 2050
95% 2085
98% 2093
99% 2094
100% 2099 (longest request)


多服务器,多进程

Server 搭建
利用 nginx 进行分发。

ApacheBench 测试
执行 2000 并发,10000 请求的 Http Keep-Alive 压测

1
ab -c2000 -n10000 -k http://[ip:port]/

返回结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
This is ApacheBench, Version 2.3 <$Revision: 1826310 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking [ip] (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests


Server Software: nginx/1.14.2
Server Hostname: [ip]
Server Port: [port]

Document Path: /
Document Length: 13 bytes

Concurrency Level: 2000
Time taken for tests: 7.978 seconds # 总共花费 8 秒左右
Complete requests: 10000
Failed requests: 7450
(Connect: 0, Receive: 0, Length: 7450, Exceptions: 0)
Keep-Alive requests: 0
Total transferred: 1352550 bytes
HTML transferred: 122550 bytes
Requests per second: 1253.45 [#/sec] (mean) # 平均每秒接受请求个数 1253
Time per request: 1595.600 [ms] (mean) # 平均每次请求花费 1595 毫秒
Time per request: 0.798 [ms] (mean, across all concurrent requests)
Transfer rate: 165.56 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 15.8 0 502
Processing: 113 1413 468.6 1587 2100
Waiting: 46 952 517.6 860 1983
Total: 113 1414 468.6 1587 2100

Percentage of the requests served within a certain time (ms)
50% 1587
66% 1592
75% 1595
80% 1596
90% 2021
95% 2050
98% 2099
99% 2099
100% 2100 (longest request)

结论
单服务器,多进程并没有比单进程更有效的提升
多服务器(2台),提升效率明显(接近 50% )

基于 Node.js 的微服务框架 Apache Bench (ab) 使用说明
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×