httprouter與 fasthttp 的性能對比
阿新 • • 發佈:2018-10-08
mean code ans rtai 性能對比 ota class 優化 17.
關於協議:
本打算接入層使用gRPC,雖然基於HTTP 2.0 效率比較高,而且使用protobuf 能進行高效的序列化。但是本次系統需要和
JAVA進行對接,考慮到gRPC對JAVA的支持性不是很好,資源並不多,可能在踩坑上花過多的時間,所以綜合考慮還是使用restful 進行對接
後續如果有機會還是會考慮使用gRPC。所以下面對幾個go的http框架進行簡單的對比
- 1
- 2
- 3
測試工具:
ApacheBench
- 1
參數:
ab -n 6000 -c 300 http://url:port
可能機器原因只能給到這麽大的參數了
- 1
- 2
- 3
測試環境:
型號:imac macos 10.13
內存:8
cpu核心數:4
為了模擬網絡請求處理時間,所以在處理的Handle function 中加入了 time.Sleep(200 *time.Millisecond)
- 1
- 2
- 3
- 4
測試對象:
httprouter:本打算使用gin 框架進行測試,但是fasthttp是一個http的包 使用 gin使用的httprouter 進行測試,優勢在於實現了restful 風格的地址,使用前綴樹實現了路由查找,使用了go 的標準包 http包 開啟一個服務 fasthttp:優勢在於自己實現了http服務,內部大量的池化,並不是每個請求都和http包一樣開啟一個goroutine, 同是也使用[]byte 操作去 代替string 操作,減少了內存的分配。
- 1
- 2
- 3
- 4
- 5
測試數據:
1、fasthttp
Server Software: fasthttp Server Hostname: 127.0.0.1 Server Port: 8083 Document Path: /index Document Length: 3 bytes Concurrency Level: 300 Time taken for tests: 4.371 seconds Complete requests: 6000 Failed requests: 0 Total transferred: 936000 bytes HTML transferred: 18000 bytes Requests per second: 1372.81 [#/sec] (mean) Time per request: 218.530 [ms] (mean) Time per request: 0.728 [ms] (mean, across all concurrent requests) Transfer rate: 209.14 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 6 4.5 7 26 Processing: 200 209 5.2 208 223 Waiting: 200 206 4.1 206 221 Total: 201 215 8.5 217 246 Percentage of the requests served within a certain time (ms) 50% 217 66% 221 75% 222 80% 223 90% 225 95% 227 98% 231 99% 235 100% 246 (longest request)
- 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
2、httprouter
Server Software:
Server Hostname: 127.0.0.1
Server Port: 8081
Document Path: /index
Document Length: 9 bytes
Concurrency Level: 300
Time taken for tests: 4.343 seconds
Complete requests: 6000
Failed requests: 0
Total transferred: 750000 bytes
HTML transferred: 54000 bytes
Requests per second: 1381.60 [#/sec] (mean)
Time per request: 217.140 [ms] (mean)
Time per request: 0.724 [ms] (mean, across all concurrent requests)
Transfer rate: 168.65 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 5 4.1 4 19
Processing: 200 209 6.0 207 227
Waiting: 200 206 3.9 205 227
Total: 201 214 9.4 211 243
Percentage of the requests served within a certain time (ms)
50% 211
66% 220
75% 223
80% 224
90% 227
95% 229
98% 233
99% 236
100% 243 (longest request)
- 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
httprouter 並發情況下內存的使用情況:
初始化
ID COMMAND %CPU TIME #TH #WQ #POR MEM PURG CMPR PGRP PPID STATE BOOSTS
20746 httprouter 0.0 00:00.01 7 0 25 1720K 0B 0B 20746 1532 sleeping *0[1]
- 1
- 2
- 3
ab -n 6000 -c 100 http://127.0.0.1:8081/index
PID COMMAND %CPU TIME #TH #WQ #POR MEM PURG CMPR PGRP PPID STATE BOOSTS
20746 httprouter 0.0 00:02.05 20 0 38 7400K 0B 0B 20746 1532 sleeping *0[1]
- 1
- 2
- 3
- 4
ab -n 6000 -c 200 http://127.0.0.1:8081/index
ID COMMAND %CPU TIME #TH #WQ #POR MEM PURG CMPRS PGRP PPID STATE
20746 httprouter 0.0 00:04.14 20 0 38 9508K 0B 880K 20746 1532 sleeping
- 1
- 2
- 3
- 4
ab -n 6000 -c 300 http://127.0.0.1:8081/index
PID COMMAND %CPU TIME #TH #WQ #POR MEM PURG CMPRS PGRP PPID STATE
20746 httprouter 0.0 00:05.51 82 0 100 13M 0B 856K 20746 1532 sleeping
- 1
- 2
- 3
- 4
fasthttp 並發情況下內存的使用情況:
初始化
PID COMMAND %CPU TIME #TH #WQ #POR MEM PURG CMPR PGRP PPID STATE BOOSTS
20455 fasthttp 0.0 00:00.01 7 0 25 1704K 0B 0B 20455 1532 sleeping *0[1]
- 1
- 2
- 3
ab -n 6000 -c 100 http://127.0.0.1:8080/index
PID COMMAND %CPU TIME #TH #WQ #POR MEM PURG CMPR PGRP PPID STATE BOOSTS
20455 fasthttp 1.9 00:01.61 29 0 47+ 5964K+ 0B 0B 20455 1532 sleeping *0[1]
- 1
- 2
- 3
- 4
- 5
ab -n 6000 -c 200 http://127.0.0.1:8080/index
PID COMMAND %CPU TIME #TH #WQ #POR MEM PURG CMPR PGRP PPID STATE BOOSTS
20455 fasthttp 0.0 00:02.99 29 0 47 7136K 0B 0B 20455 1532 sleeping *0[1]
- 1
- 2
- 3
- 4
ab -n 6000 -c 300 http://127.0.0.1:8080/index
PID COMMAND %CPU TIME #TH #WQ #POR MEM PURG CMPR PGRP PPID STATE BOOSTS
20455 fasthttp 0.0 00:04.18 49 0 67 8508K 0B 0B 20455 1532 sleeping *0[1]
- 1
- 2
- 3
- 4
可能是因為程序只是簡單的返回了 hello world ! 所以內存增長的並不明顯,
但是httprouter 在並發到300的時候 內存飆升(一開始以為自己弄錯了,測了幾次還是這樣的數據)可能是http沒有做任何優化的原因
fasthttp做了優化,並發越大的情況下越能體現優勢,所以個人更傾向於fasthttp
httprouter與 fasthttp 的性能對比