测试环境硬件
操作系统:CentOS 7
运行环境:VMware® Workstation 15 Pro
内存:4GB
处理器: 6
主机CPU:Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
测试环境软件
http: beego 1.11.1 go: go version go1.11.5 linux/amd64 压测工具:apache bench(ab, [下载地址](https://sourceforge.net/projects/xampp/files/WAMPP/2.2/wampp-2.2.zip/download)) 测试环境: Win10 家庭中文版
测试脚本
# 主要命令,on Windows PowerShell
$env:A_summary='0x7db880f9b7ebf56ae271497ca98edc4743efec62f7228e77644940cc95d95263'
$env:A_pub='0x0414e567375082ad5a7062511428436218c5ffef6f902e6d0af3e33d1626ed44baf4dca50ac5fc7057ce04d835ba1d9e05472672d8041e6c38e1a39864e91f7315'
./ab -n 10000 -c 60 -T "application/x-www-form-urlencoded" -p C:\Users\dell\Documents\BlockChain_Research\可信计算\压力测试\windows\postfile.txt http://192.168.31.144:8060/v1/tee/
# postfile.txt内容 - 这里读取windows环境变量其实未生效,考虑到数据量不大,没有实质性影响,因此忽略了
ciphertext='A.secret'&summary=$env:A_summary&description='A_description'&owner=$env:A_pub
命令解释:对http://192.168.31.144:8060/v1/tee/ 地址做10000次的请求,60个模拟并发,并附带postfile.txt中的参数做post请求。
命令还可以加上-k参数,表示开启http长连接。
测试结果
短连接:
This is ApacheBench, Version 2.0.40-dev <$Revision: 1.121.2.1 $> apache-2.0
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright (c) 1998-2002 The Apache Software Foundation, http://www.apache.org/
Benchmarking 192.168.31.144 (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
Finished 10000 requests
Server Software: beegoServer:1.11.1
Server Hostname: 192.168.31.144
Server Port: 8060
Document Path: /v1/tee/
Document Length: 121 bytes
Concurrency Level: 60
Time taken for tests: 33.160722 seconds
Complete requests: 10000
Failed requests: 0
Write errors: 0
Total transferred: 2670000 bytes
Total POSTed: 2600000
HTML transferred: 1210000 bytes
Requests per second: 301.56 [#/sec] (mean)
Time per request: 198.964 [ms] (mean)
Time per request: 3.316 [ms] (mean, across all concurrent requests)
Transfer rate: 78.62 [Kbytes/sec] received
76.57 kb/s sent
155.20 kb/s total
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.8 0 19
Processing: 60 197 46.8 195 397
Waiting: 48 195 47.0 194 397
Total: 60 198 46.8 196 400
Percentage of the requests served within a certain time (ms)
50% 196
66% 214
75% 227
80% 236
90% 257
95% 277
98% 304
99% 323
100% 400 (longest request)
- 第一段是介绍ApacheBench 的版本以及证书;
- 第二段是实时监控目前测试了多少个请求;
- 第三段是请求网址服务端的一些基本信息,包括服务端软件版本,主机名和端口;
- 第四段是请求服务端的地址和响应数据的长度;
- 第五段是请求的一些结果信息,以下摘录部分重要信息予以解释:
标题 | 描述 | 结果 | 解释 |
Concurrency Level | 并发级别 | 60 | 相当于有60人同时发起请求 |
Time taken for tests | 测试总共花费时间 | 33.160722 seconds | 秒 |
Complete requests | 全部请求 | 10000 | 次 |
Failed requests | 失败请求 | 0 | 非2xx http code,如404 |
Requests per second | 吞吐量-每秒请求数,即tps | 301.56 [#/sec] (mean) | 平均每秒请求次数 |
Time per request | 每次发出请求到接收到响应花费的时间 | 198.964 [ms] (mean) | 平均每次请求花费毫秒数 |
Time per request | 并发的每个请求平均消耗时间 | 3.316 [ms] (mean, across all concurrent requests) | 平均花费毫秒 |
Transfer rate | 传输效率 | 78.62 [Kbytes/sec] received76.57 kb/s sent155.20 kb/s total | 平均每秒网络上的流量,可以帮助排除是否存在网络流量过大导致响应时间延长的问题 |
- 第六段是网络上消耗的时间的分解;
- 第七段是整个场景中所有请求的响应情况。在场景中每个请求都有一个响应时间,其中 50% 的用户响应时间不超过 196 毫秒,80% 的用户响应时间不超过 236 毫秒,最大的响应时间不超过 400 毫秒 。
长连接:
This is ApacheBench, Version 2.0.40-dev <$Revision: 1.121.2.1 $> apache-2.0
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright (c) 1998-2002 The Apache Software Foundation, http://www.apache.org/
Benchmarking 192.168.31.144 (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
Finished 10000 requests
Server Software: beegoServer:1.11.1
Server Hostname: 192.168.31.144
Server Port: 8060
Document Path: /v1/tee/
Document Length: 121 bytes
Concurrency Level: 60
Time taken for tests: 23.326187 seconds
Complete requests: 10000
Failed requests: 0
Write errors: 0
Keep-Alive requests: 10000
Total transferred: 2910000 bytes
Total POSTed: 2857040
HTML transferred: 1210000 bytes
Requests per second: 428.70 [#/sec] (mean)
Time per request: 139.957 [ms] (mean)
Time per request: 2.333 [ms] (mean, across all concurrent requests)
Transfer rate: 121.79 [Kbytes/sec] received
119.61 kb/s sent
241.44 kb/s total
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 3
Processing: 34 11755 6714.1 11770 23325
Waiting: 17 139 32.8 138 294
Total: 34 11755 6714.1 11770 23325
Percentage of the requests served within a certain time (ms)
50% 11770
66% 15488
75% 17532
80% 18699
90% 21031
95% 22215
98% 22895
99% 23142
100% 23325 (longest request)
格式一致,不再介绍。但有一点值得关注,结果中Requests per second相比较短连接上升了大约42%左右。原因很好理解,因为长连接会减少连接释放和创建的消耗,这里从最后一部分的 Percentage of the requests served within a certain time (ms) 也能很明显的看出来,连接的平均存活时间都大幅度增加。
测试总结
ab工具能很方便的进行一些 http 接口的测试,几个简单的参数就能完成,形式也和常用的 curl 工具类似,上手极快,而且最重要的是,它免费!免费!免费!!!当然,它也有一些不足之处:
- 只能对一个节点做压测,除非你是用代理。
- 只能测试与时间相关的参数,如tps,网络时延等,对内存消耗、cpu使用、协程使用甚至每个函数的消耗时间等都无法监控。
但是,瑕不掩瑜,它仍然是你想测试 tps 的方法中比较简单和快速的一种方法。
接下来,我开始尝试使用go自带的pprof工具对上述未能满足的进一步测试,优化程序,提升性能。