彻底了解 ab 测试的计算公式
测试公式
[xxh@xxh-vmwarevirtualplatform code]$ ab -n 10 -c 3 http://lum2.test/
This is ApacheBench, Version 2.3 <$Revision: 1874286 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking lum2.test (be patient).....done
Server Software: nginx/1.18.0
Server Hostname: lum2.test
Server Port: 80
Document Path: /
Document Length: 39 bytes
Concurrency Level: 3
Time taken for tests: 0.009 seconds
Complete requests: 10
Failed requests: 0
Total transferred: 2730 bytes
HTML transferred: 390 bytes
Requests per second: 1175.78 [#/sec] (mean)
Time per request: 2.552 [ms] (mean)
Time per request: 0.851 [ms] (mean, across all concurrent requests)
Transfer rate: 313.46 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.1 0 0
Processing: 2 2 0.3 2 3
Waiting: 2 2 0.3 2 3
Total: 2 2 0.3 2 3
Percentage of the requests served within a certain time (ms)
50% 2
66% 2
75% 2
80% 2
90% 3
95% 3
98% 3
99% 3
100% 3 (longest request)
ab -n 10 -c 3 http://lum2.test
公式需要的关键变量
[Complete requests] 请求数(成功的) 10
[Time taken for tests] 花费时间 0.009
[Concurrency Level] 并发数 3
......
qps计算
公式: qps = 请求数 / 花费时间
公式: [Requests per second] = [Complete requests] / [Time taken for tests]
结果: 10 / 0.009 = 1111.11111....
为什么qps不是1175.78? 而是 1111.11….
因为[Time taken for tests]
被四舍五入了。
Time per request (第一个)
公式: tpr(1) = 并发数 * 花费时间 * 1000 / 请求数`
公式: tpr(1) = [Concurrency Level] * [Time taken for tests] * 1000 / [Complete requests] `
结果: 3 * 0.009 * 1000 / 10 = 2.7
(为什么不是2.5? 因为误差 上面已介绍)
Time per request (第二个)
公式: tpr(2) = 花费时间 * 1000 / 请求数`
公式: tpr(2) = [Time taken for tests] * 1000 / [Complete requests]
结果: 0.009 * 1000 / 10 = 0.009
ab源码
变量解释:
done 请求数
timetaken 花费时间
concurrency 并发数
//qps
printf("Requests per second: %.2f [#/sec] (mean)\n",
(double) done / timetaken);
//tpr(1)
printf("Time per request: %.3f [ms] (mean)\n",
(double) concurrency * timetaken * 1000 / done);
//tpr(2)
printf("Time per request: %.3f [ms] (mean, across all concurrent requests)\n",(double) timetaken * 1000 / done);
源码地址: CloudFundoo/ApacheBench-ab
816行
最后来张php框架qps测试图
ab -n 20000 -c 200
本作品采用《CC 协议》,转载必须注明作者和本文链接
本帖由系统于 3年前 自动加精
推荐文章: