注:这次用的是PHP ,下次试试golang

ab命令 是一个测试你 Apache http 服务器的工具,你可以通过这个工具,指定一个单位时间内向 apache 发出的请求数量来看看你的 Apache 和机器配合的性能如何。

安装命令:

yum -y install httpd-tools

查看版本:

ab -v

测试环境:

aliyun  4h8g  

语言:

PHP 5.6    打印echo 111;

ab -n 10000 -c 1000 http://api.xxx.com/test.php

[root@xxx ~]#  ab -n 10000 -c 1000  http://api.xxx.com/test.php 
# 指定并发请求数为1000,总请求数为10000,对http://api.xxx.com/test.php/进行压力测试
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking api.e-power.vip (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
Completed 10000 requests                               # 这个地方是关注点之一,如果全部完成了,说明服务器能扛住我们所指定的请求量
Finished 10000 requests                                # 我们可以继续增大请求数来进行测试,直到扛不住了就是最大的所能处理的请求了


Server Software:        nginx                          #服务器软件
Server Hostname:        api.xxx.com                    #域名
Server Port:            80                             #请求端口号

Document Path:          /test.php                      #文件路径
Document Length:        162 bytes                      #页面字节数

Concurrency Level:      1000                           #请求的并发数
Time taken for tests:   0.717 seconds                  #总访问时间
Complete requests:      10000                          #请求成功数量
Failed requests:        0                              #请求失败数量(**关注点**,有失败说明差不多了)
Write errors:           0
Non-2xx responses:      10326
Total transferred:      3149430 bytes                  #请求总数据大小(包括header头信息)
HTML transferred:       1672812 bytes                  #html页面实际总字节数
Requests per second:    13954.77 [#/sec] (mean)        #每秒多少请求,这个是非常重要的参数数值,服务器的吞吐量(**关注点**)
                                                       # Requests per second 这个数值表示当前机器的整体性能,值越大越好。
                                                       #吞吐率,计算公式:Complete requests/Time taken for tests  总请求数/处理完成这些请求数所花费的时间               
                                                       ##最关心的指标之一:相当于 LR 中的 每秒事务数/吞吐量——每秒多少请求,这个是非常重要的参数数值,服务器的吞吐量
Time per request:       71.660 [ms] (mean)             #用户平均请求等待时间  
                                                       ##最关心的指标之二:用户平均请求等待时间(平均事务响应时间)
Time per request:       0.072 [ms] (mean, across all concurrent requests) # 服务器平均处理时间,也就是服务器吞吐量的倒数                                                ##最关心的指标之三:服务器平均处理时间,也就是服务器吞吐量的倒数(每个请求实际运行时间的平均值)
Transfer rate:          4291.95 [Kbytes/sec] received  #每秒获取的数据长度
                                                       ##平均每秒网络上的流量,可以帮助排除是否存在网络流量过大导致响应时间延长的问题

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   31   5.5     31      43
Processing:    14   37   8.0     36      56
Waiting:        0   27   7.8     26      45
Total:         41   67   5.5     68      83

Percentage of the requests served within a certain time (ms)
  50%     68                                          #50%用户请求在68ms内返回
  66%     70
  75%     72
  80%     72
  90%     75
  95%     76
  98%     77
  99%     78                                          #50%用户请求在78ms内返回
 100%     83 (longest request)
 
 
 
 

ab -n 500000 -c 20000 http://api.xxx.com/test.php

[root@xxx ~]#  ab -n 500000 -c 20000  http://api.xxx.com/test.php
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking api.e-power.vip (be patient)
Completed 50000 requests
Completed 100000 requests
Completed 150000 requests
Completed 200000 requests
Completed 250000 requests
Completed 300000 requests
Completed 350000 requests
Completed 400000 requests
Completed 450000 requests
Completed 500000 requests
Finished 500000 requests


Server Software:        nginx
Server Hostname:        api.xxx.com
Server Port:            80

Document Path:          /test.php
Document Length:        162 bytes

Concurrency Level:      20000
Time taken for tests:   52.321 seconds
Complete requests:      500000
Failed requests:        0
Write errors:           0
Non-2xx responses:      511989
Total transferred:      156156645 bytes
HTML transferred:       82942218 bytes
Requests per second:    9556.33 [#/sec] (mean)
Time per request:       2092.855 [ms] (mean)
Time per request:       0.105 [ms] (mean, across all concurrent requests)
Transfer rate:          2914.62 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0  932 267.2    873    1604
Processing:   402 1111 355.5   1081    2094
Waiting:        0  839 283.2    838    1641
Total:        837 2043 280.7   2063    2658

Percentage of the requests served within a certain time (ms)
  50%   2063
  66%   2185
  75%   2226
  80%   2299
  90%   2389
  95%   2449
  98%   2562
  99%   2596
 100%   2658 (longest request)


ab(选项)(参数)

选项 
-A:指定连接服务器的基本的认证凭据;
-c:指定一次向服务器发出请求数;
-C:添加cookie; 
-g:将测试结果输出为“gnuolot”文件; 
-h:显示帮助信息;
-H:为请求追加一个额外的头; 
-i:使用“head”请求方式; 
-k:激活HTTP中的“keepAlive”特性; 
-n:指定测试会话使用的请求数; 
-p:指定包含数据的文件; 
-q:不显示进度百分比; 
-T:使用POST数据时,设置内容类型头;
-v:设置详细模式等级;
-w:以HTML表格方式打印结果; 
-x:以表格方式输出时,设置表格的属性; 
-X:使用指定的代理服务器发送请求;
-y:以表格方式输出时,设置表格属性。

选项

\-A auth-username:password  
 #  支持基本的验证证书,用户名和密码之间使用"冒号"                    :  
 # 分隔开,ab将以明文方式传送过去.不管服务器是不是需要  
 # ,也就是说你的服务器需要支持401认证.  

\-c concurrency  
 # 同时向服务器端发送的请求数目,默认状态下是一次 只执行一个http请求.  

\-C cookie-name=value  
 # 添加cookie; line to the request. The argument is typically in the  
 # form of a name=value pair. This field is repeatable.  

\-d    #  Do not display  the  "percentage  served  within  XX  \[ms\]  table".  
 # (legacy support).  

\-e csv-file  
 # Write  a  Comma  separated value (CSV) file which contains for each  
 # percentage (from 1% to 100%) the time (in milli seconds) it took to  
 # serve  that percentage of the requests. This is usually more useful  
 # than the 'gnuplot' file; as the results are already 'binned'.  

\-g gnuplot-file  
 # 将测试结果输出为“gnuolot”文件  
 # Write all measured values out as a 'gnuplot' or TSV  (Tab  separate  
 # values)  file.  This file can easily be imported into packages like  
 # Gnuplot, IDL, Mathematica, Igor or even Excell. The labels  are  on  
 # the first line of the file.  
\-h    # 显示使用说明  
\-H custom-header  
 # 向请求包追加附加的标题字串.此参数应该是有效的标题         行(header  
 # line)形式,通常使用冒号":"来分隔有效配对 (valid  pair)例如  'Accept-  
 # Encoding: zip/zop;8 bit';  

\-i    # 使用一个 http 头(HEAD) 来替换 GET方法.不可以掺入POST 方法  

\-k    #  允许http      KeepAlive      ;也就是说执行多个请求在一个      http  
 # 会话当中,默认是不允许的也就是no KeepAlive啦;)  

\-n requests  
 # 执行一次测试会话的时候所发出的请求数目,默认是执行一个单一的请求  
 # 当然了这样的测试结果也就没什么意义了  

\-p POST-file  
 # 测试程序也就是ab,将向Apache server发送带有HTTP POST 的请求.  

\-P proxy-auth-username:password  
 # 当需要通过代理测试一台 HTTP 服务器的时候而你的代理  
 # 又需要用户名密码验证,这时你可以使用这个选项,同样  
 # 用户名与密码之间使用冒号":"分隔开,ab将之以明文的方式  
 # 发送出去,当然,前提是你的代理是处于407认证状态的  

\-q    # 不显示进度百分比  
 #  When processing more than 150 requests, ab outputs a progress count  
 # on  stderr  every  10% or 100 requests or so. The -q flag will sup‐  
 # press these messages.  

\-s    #  When compiled in (ab -h will show you) use the SSL protected  https  
 # rather  than  the  http  protocol. This feature is experimental and  
 # very rudimentary. You probably do not want to use it.  

\-S    #  Do not display the median and standard deviation values,  nor  dis‐  
 # play  the  warning/error  messages  when the average and median are  
 # more than one or two times the standard deviation  apart.  And  de‐  
 # fault to the min/avg/max values. (legacy support).  

\-t timelimit  
 #  设置测试的时间的长短,使用这个选项ab将自动设置  
 # 测试请求会话数目为50000,然后以你设置的时间为  
 # 固定周期.默认状态下是没有时限的,也就是直到完成  
 # 你所设置的请求数目为止.  

\-T content-type  
 # 内容类型标头,使用在POST数据的时候.  

\-v verbosity  
 # 设置冗余级别,4级打印出每个请求标头的详细信息,  
 # 3级打印出回应代码(例如,404,200),2级打印出警告 信息和指示消息  

\-V    # 显示版本号并且退出  
\-w    # 打印输出结果到HTML表中. 默认的表是两列n行白底黑框  

\-x <table>-attributes  
 # 使用字串来描述表的属性,该属性字串应该插入到<table 这里 >  

\-X proxy\[:port\]  
 # Use a proxy server for the requests.  

\-y <tr>-attributes  
 # 用于生成html表格每行的属性名 (<tr>)  

\-z <td>-attributes  
 # 用于生成html表格每列的属性名 (<td>)

特伦姝
44 声望0 粉丝

不务正业的搬运工,分享一些技术指北。。。