彻底了解 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

彻底了解 ab 测试的计算公式

彻底了解 ab 测试的计算公式

本作品采用《CC 协议》,转载必须注明作者和本文链接
专心学习不瞎搞
本帖由系统于 2年前 自动加精
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 3

可以给laravel加上laraves :smiley:

3年前 评论
白小二

qps 存在fail 是不是算测试不通过,我每次测试出现fail,服务器都会卡死,阿里云的1核1g学生机 ab -n 100 -c 10 都要卡半个钟

2年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
未填写
文章
87
粉丝
105
喜欢
480
收藏
717
排名:109
访问:8.7 万
私信
所有博文
社区赞助商