nginx负载均衡(三)----使用ab软件进行压力测试

一、ab

1.ab是什么?

ab是apachebench命令的缩写。ab是apache自带的压力测试工具。ab非常实用,它不仅可以对apache服务器进行网站访问压力测试,也可以对或其它类型的服务器进行压力测试。比如nginx、tomcat、IIS等

2.ab的原理

ab的原理:ab命令会创建多个并发访问线程,模拟多个访问者同时对某一URL地址进行访问。它的测试目标是基于URL的,因此,它既可以用来测试apache的负载压力,也可以测试nginx、lighthttp、tomcat、IIS等其它Web服务器的压力。

ab命令对发出负载的计算机要求很低,它既不会占用很高CPU,也不会占用很多内存。但却会给目标服务器造成巨大的负载,其原理类似CC攻击。自己测试使用也需要注意,否则一次上太多的负载。可能造成目标服务器资源耗完,严重时甚至导致死机。

二、压力测试

对整个系统进行测试 -->看整个服务器集群能承受多少的访问量–>跟服务器的硬件和软件配置有关系
因素:cpu、网络带宽、磁盘IO
指标:QPS/TPS/IOPS/吞吐量
1.QPS

  • QPS Query Per Second,既每秒请求、查询次数
  • nginx的web服务–>发http请求的数量

2.TPS

  • Transcantion Per Second,既每秒事务数

3.IOPS

  • IOPS 是指单位时间内系统能处理的I/O请求数量,一般以每秒处理的I/O请求数量为单位,I/O请求通常为读或写数据操作请求。
  • 看数据库或者磁盘–>值越大越好

优化

  • 硬件层面–>提升硬件
  • 软件层面–>os层面–>nginx负载均衡–>后端的nginx配置

三、使用ab做压力测试

1.安装ab

[root@ab-test ~]#  yum -y install httpd-tools

2.查看ab版本

也可以查看ab是否安装成功

[root@ab-test ~]# ab -V
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

3.ab帮助文档查看参数

[root@ab-test ~]# ab -h
Usage: ab [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)
    -E certfile     Specify optional client certificate chain and private key

4.常用参数

-n: 总的请求个数
-c: 一次并发的请求数
-t: 持续的时间,默认没有限制
​-k:它会增加请求头Connection: Keep-Alive,相当于开启了HTTP长连接。 这样做一方面可以降低测试服务器动态端口被耗尽的风险,另一方面也有助于给目标服务器更大的压力,测试出更接近极限的结果
说明:ab使用的是HTTP/1.0,缺省开启的是短链接,用-k参数则可以打开长连接

5.ab测试发送请求

一次并发100个请求,一共10000个请求

[root@ab-test ~]# ab  -c 100 -n 10000 -k  http://192.168.0.109/index.html
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.0.112 (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/1.21.1
Server Hostname:        192.168.0.112
Server Port:            80

Document Path:          /index.html
Document Length:        20 bytes

Concurrency Level:      100
Time taken for tests:   2.485 seconds
Complete requests:      10000
Failed requests:        0
Keep-Alive requests:    10000
Total transferred:      2560000 bytes
HTML transferred:       200000 bytes
Requests per second:    4024.91 [#/sec] (mean)
Time per request:       24.845 [ms] (mean)
Time per request:       0.248 [ms] (mean, across all concurrent requests)
Transfer rate:          1006.23 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.9      0      13
Processing:     3   24  11.4     26      99
Waiting:        3   24  11.4     26      99
Total:          3   24  11.6     26     105

Percentage of the requests served within a certain time (ms)
  50%     26
  66%     29
  75%     31
  80%     32
  90%     42
  95%     46
  98%     48
  99%     51
 100%    105 (longest request)

6.监控负载均衡器和nginx服务器

监控负载均衡器和nginx服务器查看CPU使用情况、内存使用情况等。

[root@LB ~]#dstat -a -m

7.ab测试输出说明

  • Document Length: # 请求的页面大小
  • Concurrency Level: # 每次的并发请求数
  • Time taken for tests: # 测试总共耗时
  • Complete requests: # 完成的请求总数
  • Failed requests: # 失败的请求数量
  • Write errors: # 错误的请求数量
  • Total transferred: # 总共传输数据量
  • Requests per second 吞吐率(reqs/s):服务器每秒能够处理的请求数量
  • Time per request 平均请求处理时间,它的两个值是:

第一行为每次并发请求的耗时,

第二行为每个请求的耗时

第一行值为第二行的值乘上并发请求数量。

可以尝试将并发数改为20,

这样就会看到第一行是第二行的20倍

  • Transfer rate 表示吞吐量(BPS):即:每秒从服务器获取的数据的长度
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值