橙子0012 发表于 2018-2-28 14:19:07

吞吐率和压力测试

吞吐率指的是单位时间内服务器处理的请求数,通常使用 reqs/s (服务器每秒处理的请求数量)来
表示。
吞吐率只描述了服务器在实际运行期间单位时间内处理的请求数,而我们更加关心的是服务器并发
处理能力的上限,即单位时间内服务器能够处理的最大请求数(即最大吞吐率)。但在测试时,很
难调动足够多的人测试服务器的最大吞吐率。所以,需要使用某些方法模拟足够数目的并发用户数,
这种方法称为“压力测试”。
压力测试的工具很多,如LoadRunner、JMeter和ab等。由于LoadRunner和JMeter使用起来过于复杂,
故这里我们介绍一种比较简单的压力测试工具ab。
ab(Apache Bench)是Apache附带的一个压力测试软件。它操作起来比较简单,功能基本能够满足
我们的要求。
打开cmd,切换至Apache的bin目录(如:我的是 E:\xampp\apache\bin )
cd E:\xampp\apache\bin
ab -h          // 打印出ab工具的使用信息
Usage: ab hostname[:port]/path
Options are:
    -n requests   Number of requests to perform
    -c concurrencyNumber of multiple requests to make at a time
    -t timelimit    Seconds to max. to spend on benchmarking
                  This implies -n 50000
    -s timeout      Seconds to max. wait for each response
                  Default is 30 seconds
    -b windowsize   Size of TCP send/receive buffer, in bytes
    -B address      Address to bind to when making outgoing connections
    -p postfile   File containing data to POST. Remember also to set -T
    -u putfile      File containing data to PUT. Remember also to set -T
    -T content-type Content-type header to use for POST/PUT data, eg.
                  'application/x-www-form-urlencoded'
                  Default is 'text/plain'
    -v verbosity    How much troubleshooting info to print
    -w            Print out results in HTML tables
    -i            Use HEAD instead of GET
    -x attributes   String to insert as table attributes
    -y attributes   String to insert as tr attributes
    -z attributes   String to insert as td or th attributes
    -C attribute    Add cookie, eg. 'Apache=1234'. (repeatable)
    -H attribute    Add Arbitrary header line, eg. 'Accept-Encoding: gzip'
                  Inserted after all normal header lines. (repeatable)
    -A attribute    Add Basic WWW Authentication, the attributes
                  are a colon separated username and password.
    -P attribute    Add Basic Proxy Authentication, the attributes
                  are a colon separated username and password.
    -X proxy:port   Proxyserver and port number to use
    -V            Print version number and exit
    -k            Use HTTP KeepAlive feature
    -d            Do not show percentiles served table.
    -S            Do not show confidence estimators and warnings.
    -q            Do not show progress when doing more than 150 requests
    -l            Accept variable document length (use this for dynamic pages)
    -g filename   Output collected data to gnuplot format file.
    -e filename   Output CSV file with percentages served
    -r            Don't exit on socket receive errors.
    -m method       Method name
    -h            Display usage information (this message)ab的选项比较多,常用的有以下几个:
n   在测试会话中执行的请求的数目,默认执行一个请求。
c   要创建的并发用户数,默认创建一个用户数。
t   等待Web服务器响应的最大时间(单位:秒),默认没有时间限制
k   使用Keep Alive(长连接)特性
c   对请求附加一个Cookie,形式为name=value

下面使用 ab 进行一次压力测试:
ab -n1000 -c10 demo.com/index.php
输出内容如下:
This is ApacheBench, Version 2.3 <$Revision: 1604373 [        DISCUZ_CODE_7        ]gt;
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking demo.com (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests

Server Software:      Apache/2.4.10
Server Hostname:      demo.com
Server Port:            80

Document Path:          /index.php
Document Length:      36 bytes

Concurrency Level:      10
Time taken for tests:   0.452 seconds
Complete requests:      1000
Failed requests:      0
Total transferred:      254000 bytes
HTML transferred:       36000 bytes
Requests per second:    2210.43 [#/sec] (mean)
Time per request:       4.524 (mean)
Time per request:       0.452 (mean, across all concurrent requests)
Transfer rate:          548.29 received

Connection Times (ms)
            minmean[+/-sd] median   max
Connect:      0    0   1.8      0      16
Processing:   0    4   7.0      0      31
Waiting:      0    4   6.8      0      31
Total:          0    4   7.1      0      31

Percentage of the requests served within a certain time (ms)
50%      0
66%      0
75%   16
80%   16
90%   16
95%   16
98%   16
99%   16
100%   31 (longest request)从测试结果可以看出,吞吐率为2210.43reqs/s。
测试结果中,我们通常会关注以下几个内容:
Server Software:被测试的Web服务器软件的名称和版本。
Server Hostname:请求URL中的主机名称。
Server Port:被测试的Web服务器的监听端口。
Document Path:请求的URL的绝对路径。
Document Length:HTTP响应数据的正文的长度。
Concurrency Level:并发的用户数,设置的 c 参数。
Time taken for tests:所有请求处理完毕所花费的总时间。
Complete requests:总请求数,设置的 n 参数。
Failed requests:失败的请求数。
Total transferred:所有请求的响应数据长度总和。
HTML transferred:所有请求的响应数据中正文数据的长度总和。
Requests per second:Web服务器的吞吐率,等于Complete requests/Time taken for tests。
Time per request:用户平均每个请求的等待时间。
Transfer rate:这些请求在单位时间内从服务器取得的数据长度,等于Total transferred/Time taken for tests。

梦想家 发表于 2018-2-28 15:57:50

:handshake

Miss_love 发表于 2020-12-30 09:49:42

支持下
页: [1]
查看完整版本: 吞吐率和压力测试