压力测试

常用的压力测试工具有两种:ab测试jmeter

1.ab测试

介绍

ab测试使用的是apache中自带的ab工具,在windows系统中安装了apache后,在bin文件夹中会出现ab.exeabs.exe。进行http请求测试的时候使用ab.exe没有问题,但是进行https请求测试时会提示SSL not compiled in; no https support,些时使用abs.exe做测试即可。

如果是linux系统没有安装apache,则可以只安装ab工具进行测试即可。

yum -y install httpd-tools

参数介绍

这里只介绍几个主要的参数:

-c 并发数,模拟请求的用户数

-n 请求总数

-p 发送post请求的数据文件,文件中内容以key=value&key1=value1形式拼接参数

-T 设置content-type发送post请求时必须设置,application/x-www-form-urlencoded

更多参数参考以下:

$ ./abs.exe
D:\wamp\bin\apache\apache2.4.35\bin\abs.exe: wrong number of arguments
Usage: D:\wamp\bin\apache\apache2.4.35\bin\abs.exe [options] [http[s]://]hostname[:port]/path
Options are:
    -n requests     Number of requests to perform
    -c concurrency  Number 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)
    -I              Disable TLS Server Name Indication (SNI) extension
    -Z ciphersuite  Specify SSL/TLS cipher suite (See openssl ciphers)
    -f protocol     Specify SSL/TLS protocol
                    (SSL2, TLS1, TLS1.1, TLS1.2 or ALL)

测试示例

GET请求

./abs.exe -c 100 -n 1000 https://www.baidu.com/

POST请求

./abs.exe -c 100 -n 1000 -p "/home/post.txt" -T "application/x-www-form-urlencoded" https://www.baidu.com/

注意:如果只是一个域名后面没有任何参数,则需要在最后面加/,如请求接口地址是https://wwww.baidu.com,则发送请求时需要https://www.baidu.com/

测试结果

$ ./abs.exe -c 10 -n 100 https://www.baidu.com/
This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.baidu.com (be patient).....done


Server Software:        BWS/1.1
Server Hostname:        www.baidu.com
Server Port:            443
SSL/TLS Protocol:       TLSv1.2,ECDHE-RSA-AES128-GCM-SHA256,2048,128
TLS Server Name:        www.baidu.com

Document Path:          /
Document Length:        227 bytes

Concurrency Level:      10
Time taken for tests:   0.736 seconds // 请求总时间
Complete requests:      100 // 完成请求数
Failed requests:        0 // 失败请求数
Total transferred:      89300 bytes
HTML transferred:       22700 bytes
Requests per second:    135.92 [#/sec] (mean) // 每秒的请求数即QPS
Time per request:       73.572 [ms] (mean)
Time per request:       7.357 [ms] (mean, across all concurrent requests)
Transfer rate:          118.53 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       11   24   8.8     21      56
Processing:     4   40  41.0     16     127
Waiting:        4   38  41.5     11     126
Total:         18   64  42.0     40     155

Percentage of the requests served within a certain time (ms)
  50%     40
  66%     71
  75%    110
  80%    114
  90%    132
  95%    137
  98%    145
  99%    155
 100%    155 (longest request)

2.jmeter测试

安装jdk8

jmeter是使用java开发的一款压力测试软件,要正常使用需要安装jdk8。
下载地址:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

开始测试

由于我电脑是windows系统,这里仅以windows为例进行演

打开软件

双击bin目录下的jmeter.bat
压力测试_第1张图片
打开后的界面如下,选项里可以修改显示语言。
压力测试_第2张图片

添加线和组

testplan右键->->添加->线程(用户)->线程组
线程组界面说明:
名称 线程组名称自己随意命名即可

线程数 线程数相当于模拟用户数

Ramp-up准备时长:设置的虚拟用户数需要多长时间全部启动。如果线程数为10,准备时长为2,那么需要2秒钟启动10个线程,也就是每秒钟启动5个线程

循环次数 每个线程发送请求的次数。如果线程数为10,循环次数为100,那么每个线程发送100次请求。总请求数为10*100=1000 。如果勾选了“永远”,那么所有线程会一直发送请求,一到选择停止运行脚本

压力测试_第3张图片
压力测试_第4张图片

添加http请求

线程组右键->添加->取样器->http请求

虽然这里添加的是http请求,但是也可以发送https请求,在端口和协议上分别使用443和https就可以发送https请求
压力测试_第5张图片
压力测试_第6张图片

添加监听器

http请求右键->添加->监听器->察看结果树/汇总报告 其余自己需要的监听器可以按需求添加
压力测试_第7张图片

你可能感兴趣的:(性能测试)