关于协议:
本打算接入层使用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内存:8cpu核心数: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: ???????fasthttpServer Hostname: ???????127.0.0.1Server Port: ???????????8083Document Path: ?????????/indexDocument Length: ???????3 bytesConcurrency Level: ?????300Time taken for tests: ??4.371 secondsComplete requests: ?????6000Failed requests: ???????0Total transferred: ?????936000 bytesHTML transferred: ??????18000 bytesRequests 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] receivedConnection Times (ms) ?????????????min ?mean[+/-sd] median ??maxConnect: ???????0 ???6 ??4.5 ?????7 ?????26Processing: ??200 ?209 ??5.2 ???208 ????223Waiting: ?????200 ?206 ??4.1 ???206 ????221Total: ???????201 ?215 ??8.5 ???217 ????246Percentage 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.1Server Port: ???????????8081Document Path: ?????????/indexDocument Length: ???????9 bytesConcurrency Level: ?????300Time taken for tests: ??4.343 secondsComplete requests: ?????6000Failed requests: ???????0Total transferred: ?????750000 bytesHTML transferred: ??????54000 bytesRequests 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] receivedConnection Times (ms) ?????????????min ?mean[+/-sd] median ??maxConnect: ???????0 ???5 ??4.1 ?????4 ?????19Processing: ??200 ?209 ??6.0 ???207 ????227Waiting: ?????200 ?206 ??3.9 ???205 ????227Total: ???????201 ?214 ??9.4 ???211 ????243Percentage 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 ???BOOSTS20746 ?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/indexPID ???COMMAND ?????%CPU TIME ????#TH ?#WQ ?#POR MEM ???PURG CMPR PGRP ?PPID STATE ???BOOSTS20746 ?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/indexID ???COMMAND ?????%CPU TIME ????#TH ?#WQ ?#POR MEM ???PURG CMPRS ?PGRP ?PPID STATE20746 ?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/indexPID ???COMMAND ?????%CPU TIME ????#TH ?#WQ ?#POR MEM ???PURG CMPRS ?PGRP ?PPID STATE20746 ?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 ???BOOSTS20455 ?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/indexPID ???COMMAND ?????%CPU TIME ????#TH ?#WQ ?#POR MEM ???PURG CMPR PGRP ?PPID STATE ???BOOSTS20455 ?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/indexPID ???COMMAND ?????%CPU TIME ????#TH ?#WQ ?#POR MEM ???PURG CMPR PGRP ?PPID STATE ???BOOSTS20455 ?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/indexPID ???COMMAND ?????%CPU TIME ????#TH ?#WQ ?#POR MEM ???PURG CMPR PGRP ?PPID STATE ???BOOSTS20455 ?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 的性能对比
原文地址:https://www.cnblogs.com/sunsky303/p/9754634.html