侧边栏壁纸
  • 累计撰写 47 篇文章
  • 累计创建 22 个标签
  • 累计收到 27 条评论

目 录CONTENT

文章目录

Web压力测试

vchopin
2022-05-24 / 0 评论 / 0 点赞 / 284 阅读 / 2,986 字

WEB压力测试

1.什么是压力测试

在项目上线之前,都需要做压力测试,目的是看下我们的网站能抗住多少的压力,能承担多少并发,如果不做压力测试,一旦出现大访问量时,我们的网站容易拒绝服务或者502。压力测试也是为了看当前服务器的配置是否配得上这个项目的需求,如果访问量增大,就需要升级服务器配置。

2.压力测试工具

2.1 webbench

  • 下载并解压。通过命令wget http://soft.vpser.net/test/webbench/webbench-1.5.tar.gz下载webbench源代码
    image-1653371829952
    然后输入 tar -zxvf webbench-1.5.tar.gz解压压缩包。如下:
    image-1653371850752
  • 编译安装。输入make && make install编译并安装,如下:
    image-1653371889523
    在安装的时候可能会出现以下问题:
    image-1653371916436
    需要新建一个文件夹mkdir /usr/local/man
    验证是否安装成功:
    image-1653371975819
  • 实测。使用如下代码对某网站进行压力测试:webbench -c 100 -t 60 http://www.xxxx.com/index,该代码能够在一秒内发起100次并发请求,并持续60秒。如下:
    image-1653372126036
    全部成功,代表这点并发还是可以承受。

2.2 Apache Bench

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

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

  • ab压力测试工具很多服务器已经自带,输入ab即可看到。
  • ab使用方法如下:
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
    -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.
    -h              Display usage information (this message)
    -Z ciphersuite  Specify SSL/TLS cipher suite (See openssl ciphers)
    -f protocol     Specify SSL/TLS protocol
                    (SSL3, TLS1, TLS1.1, TLS1.2 or ALL)

  • ab 简单使用方法:ab -n 4000 -c 1000 http://www.xxxx.com。结果如下
    image-1653372549510

3.总结

当前各种各样的压力测试工具,已经能够满足日常需求了。但是实际上,这些只是用来做页面访问测试,在目前web2.0的时代,服务器除了web页面的交互,往往还承担着视频、文件上传等消耗服务器资源的操作,实际可用并发数往往是小于测试的并发数的。

1、压力及性能测试工作应该放到产品上线之前,而不是上线以后;
2、测试时并发应当由小逐渐加大,比如并发100时观察一下网站负载是多少、打开页面是否流畅,并发200时又是多少、网站打开缓慢时并发是多少、网站打不开时并发又是多少;
3、更详细的进行某个页面测试,如电商网站可以着重测试购物车、推广页面等,因为这些页面占整个网站访问量比重较大。

备注:webbench 做压力及性能测试时,该软件自身也会消耗CPU和内存资源,为了测试准确,建议将 webbench 安装在其他的服务器上,已达到测试数据更加精确。

参考

  1. WEB项目 压力测试总结 https://blog.csdn.net/qq_42559334/article/details/82791637
  2. Web必备性能压力测试工具 https://zhuanlan.zhihu.com/p/38118572
0

评论区