Chinaunix首页 | 论坛 | 博客
  • 博客访问: 371479
  • 博文数量: 114
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1219
  • 用 户 组: 普通用户
  • 注册时间: 2015-02-07 21:23
文章分类

全部博文(114)

文章存档

2018年(1)

2017年(5)

2016年(87)

2015年(21)

我的朋友

分类: 系统运维

2016-06-25 11:10:05


  1. 网站性能压力测试是性能调优过程中必不可少的一环。只有让服务器处在高压情况下才能真正体现出各种设置所暴露的问题。Apache中有个自带的,名为ab的程序,可以对Apache或其它类型的服务器进行网站访问压力测试。

  2. 1. ApacheBench命令原理:

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

  4. ab命令对发出负载的计算机要求很低,既不会占用很高CPU,也不会占用很多内存,但却会给目标服务器造成巨大的负载,其原理类似CC攻击。自己测试使用也须注意,否则一次上太多的负载,可能造成目标服务器因资源耗完,严重时甚至导致死机。
  5. 性能测试工具目前最常见的有以下几种:ab、http_load、webbench、siege

  6. 2.ab的安装
  7. ab的安装非常简单,如果是源码安装apache的话,那就更简单了。apache安装完毕后ab命令存放在apache安装目录的bin目录下。如下:
  8. [root@zsq bin]# pwd
  9. /application/apache/bin
  10. [root@zsq bin]# ll
  11. total 3188
  12. -rwxr-xr-x. 1 root root 68120 Apr 17 10:17 ab
  13. -rwxr-xr-x. 1 root root 3449 Apr 17 10:03 apachectl
  14. 如果apache 是通过yum的RPM包方式安装的话,ab命令默认存放在/usr/bin目录下。如下:
  15. [root@zsq bin]# which ab
  16. /usr/bin/ab

  17. 如果不想安装apache但是又想使用ab命令的话,我们可以直接安装apache的工具包httpd-tools。如下:

  18. yum -y install httpd-tools

  19. 查看ab是否安装成功,可以切换到上述目录下,使用ab –V命令进行检测。如下:同时也会显示相应的版本
  20. [root@zsq bin]# ab -V
  21. This is ApacheBench, Version 2.3 <$Revision: 655654 $>
  22. Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
  23. Licensed to The Apache Software Foundation, http://www.apache.org/

  24. 3、ab参数说明

  25. 有关ab命令的使用,我们可以通过帮助命令进行查看。
  26. [root@zsq bin]# ab --help
  27. ab: wrong number of arguments
  28. Usage: ab [options] [http[s]://]hostname[:port]/path
  29. Options are:
  30.     -n requests Number of requests to perform
  31.     -c concurrency Number of multiple requests to make
  32.     -t timelimit Seconds to max. wait for responses
  33.     -b windowsize Size of TCP send/receive buffer, in bytes
  34.     -p postfile File containing data to POST. Remember also to set -T
  35.     -u putfile File containing data to PUT. Remember also to set -T
  36.     -T content-type Content-type header for POSTing, eg.
  37.                     'application/x-www-form-urlencoded'
  38.                     Default is 'text/plain'
  39.     -v verbosity How much troubleshooting info to print
  40.     -w Print out results in HTML tables
  41.     -i Use HEAD instead of GET
  42.     -x attributes String to insert as table attributes
  43.     -y attributes String to insert as tr attributes
  44.     -z attributes String to insert as td or th attributes
  45.     -C attribute Add cookie, eg. 'Apache=1234. (repeatable)
  46.     -H attribute Add Arbitrary header line, eg. 'Accept-Encoding: gzip'
  47.                     Inserted after all normal header lines. (repeatable)
  48.     -A attribute Add Basic WWW Authentication, the attributes
  49.                     are a colon separated username and password.
  50.     -P attribute Add Basic Proxy Authentication, the attributes
  51.                     are a colon separated username and password.
  52.     -X proxy:port Proxyserver and port number to use
  53.     -V Print version number and exit
  54.     -k Use HTTP KeepAlive feature
  55.     -d Do not show percentiles served table.
  56.     -S Do not show confidence estimators and warnings.
  57.     -g filename Output collected data to gnuplot format file.
  58.     -e filename Output CSV file with percentages served
  59.     -r Don't exit on socket receive errors.
  60.     -h Display usage information (this message)
  61.     -Z ciphersuite Specify SSL/TLS cipher suite (See openssl ciphers)
  62.     -f protocol Specify SSL/TLS protocol (SSL2, SSL3, TLS1, or ALL)

  63. 下面我们对这些参数,进行相关说明。如下:

  64. -n在测试会话中所执行的请求个数。默认时,仅执行一个请求。

  65. -c一次产生的请求个数。默认是一次一个。

  66. -t测试所进行的最大秒数。其内部隐含值是-n 50000,它可以使对服务器的测试限制在一个固定的总时间以内。默认时,没有时间限制。

  67. -p包含了需要POST的数据的文件。

  68. -P对一个中转代理提供BASIC认证信任。用户名和密码由一个:隔开,并以base64编码形式发送。无论服务器是否需要(, 是否发送了401认证需求代码),此字符串都会被发送。

  69. -T POST数据所使用的Content-type头信息。

  70. -v设置显示信息的详细程度-4或更大值会显示头信息,3或更大值可以显示响应代码(404,200等),2或更大值可以显示警告和其他信息。

  71. -V显示版本号并退出。

  72. -w以HTML表的格式输出结果。默认时,它是白色背景的两列宽度的一张表。

  73. -i执行HEAD请求,而不是GET。

  74. -x设置<table>属性的字符串。

  75. -X对请求使用代理服务器。

  76. -y设置<tr>属性的字符串。

  77. -z设置<td>属性的字符串。

  78. -C对请求附加一个Cookie:行。其典型形式是name=value的一个参数对,此参数可以重复。

  79. -H对请求附加额外的头信息。此参数的典型形式是一个有效的头信息行,其中包含了以冒号分隔的字段和值的对(,"Accept-Encoding:zip/zop;8bit")

  80. -A对服务器提供BASIC认证信任。用户名和密码由一个:隔开,并以base64编码形式发送。无论服务器是否需要(,是否发送了401认证需求代码),此字符串都会被发送。

  81. -h显示使用方法。

  82. -d不显示"percentage served within XX [ms] table"的消息(为以前的版本提供支持)

  83. -e产生一个以逗号分隔的(CSV)文件,其中包含了处理每个相应百分比的请求所需要(从1%到100%)的相应百分比的(以微妙为单位)时间。由于这种格式已经“二进制化”,所以比’gnuplot’格式更有用。

  84. -g把所有测试结果写入一个’gnuplot’或者TSV(以Tab分隔的)文件。此文件可以方便地导入到Gnuplot,IDL,Mathematica,Igor甚至Excel中。其中的第一行为标题。

  85. -i执行HEAD请求,而不是GET。

  86. -k启用HTTP KeepAlive功能,即在一个HTTP会话中执行多个请求。默认时,不启用KeepAlive功能。

  87. -q如果处理的请求数大于150,ab每处理大约10%或者100个请求时,会在stderr输出一个进度计数。此-q标记可以抑制这些信息。

  88. 4.ab性能指标
  89. 在进行性能测试过程中有几个指标比较重要:

  90. 1.1、吞吐率(Requests per second)

  91. 服务器并发处理能力的量化描述,单位是reqs/s,指的是在某个并发用户数下单位时间内处理的请求数。某个并发用户数下单位时间内能处理的最大请求数,称之为最大吞吐率。
  92. 记住:吞吐率是基于并发用户数的。这句话代表了两个含义:

  93. a、吞吐率和并发用户数相关

  94. b、不同的并发用户数下,吞吐率一般是不同的

  95. 计算公式:总请求数/处理完成这些请求数所花费的时间,即

  96. Request per second=Complete requests/Time taken for tests

  97. 必须要说明的是,这个数值表示当前机器的整体性能,值越大越好。
  98. 1.2 并发连接数(The number of concurrent connections)

  99. 并发连接数指的是某个时刻服务器所接受的请求数目,简单的讲,就是一个会话。
  100. 1.3 并发用户数(Concurrency Level)

  101. 要注意区分这个概念和并发连接数之间的区别,一个用户可能同时会产生多个会话,也即连接数。在HTTP/1.1下,IE7支持两个并发连接,IE8支持6个并发连接,FireFox3支持4个并发连接,所以相应的,我们的并发用户数就得除以这个基数。
  102. 1.4用户平均请求等待时间(Time per request)

  103. 计算公式:处理完成所有请求数所花费的时间/(总请求数/并发用户数),即:

  104. Time per request=Time taken for tests/(Complete requests/Concurrency Level)

  105. 1.5、服务器平均请求等待时间(Time per request:across all concurrent requests)

  106. 计算公式:处理完成所有请求数所花费的时间/总请求数,即:

  107. Time taken for/testsComplete requests

  108. 可以看到,它是吞吐率的倒数。

  109. 同时,它也等于用户平均请求等待时间/并发用户数,即

  110. Time per request/Concurrency Level

  111. 5.ab实际使用
  112. ab的命令参数比较多,我们经常使用的是-c和-n参数。
  113. ab -c 10 -n 100 http://192.168.88.112/index.php
  114. -c10表示并发用户数为10

  115. -n100表示请求总数为100

  116. http://192.168.88.112/index.php表示请求的目标URL

  117. 这行表示同时处理100个请求并运行10次index.php文件。

  118. [root@zsq bin]# ab -c 10 -n 100 http://192.168.88.112/index.php
  119. This is ApacheBench, Version 2.3 <$Revision: 655654 $>
  120. Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
  121. Licensed to The Apache Software Foundation, http://www.apache.org/

  122. Benchmarking 192.168.88.112 (be patient).....done


  123. Server Software: Apache/2.2.27
  124. Server Hostname: 192.168.88.112
  125. Server Port: 80

  126. Document Path: /index.php
  127. Document Length: 54982 bytes

  128. Concurrency Level: 10
  129. Time taken for tests: 0.328 seconds
  130. Complete requests: 100
  131. Failed requests: 0
  132. Write errors: 0
  133. Total transferred: 5515500 bytes
  134. HTML transferred: 5498200 bytes
  135. Requests per second: 304.73 [#/sec] (mean)
  136. Time per request: 32.816 [ms] (mean)
  137. Time per request: 3.282 [ms] (mean, across all concurrent requests)
  138. Transfer rate: 16413.23 [Kbytes/sec] received

  139. Connection Times (ms)
  140.               min mean[+/-sd] median max
  141. Connect: 0 9 6.9 9 24
  142. Processing: 4 24 8.2 24 51
  143. Waiting: 0 8 6.8 8 27
  144. Total: 16 32 5.8 32 68

  145. Percentage of the requests served within a certain time (ms)
  146.   50% 32
  147.   66% 33
  148.   75% 33
  149.   80% 33
  150.   90% 34
  151.   95% 44
  152.   98% 47
  153.   99% 68
  154.  100% 68 (longest request)

  155.  apache测试出吞吐为Requests per second: 304.73 [#/sec] (mean)
  156.  除此之外还有其他一些信息,需要说明下,如下:

  157. Server Software表示被测试的Web服务器软件名称。

  158. Server Hostname表示请求的URL主机名。

  159. Server Port表示被测试的Web服务器软件的监听端口。

  160. Document Path表示请求的URL中的根绝对路径,通过该文件的后缀名,我们一般可以了解该请求的类型。

  161. Document Length表示HTTP响应数据的正文长度。

  162. Concurrency Level表示并发用户数,这是我们设置的参数之一。

  163. Time taken for tests表示所有这些请求被处理完成所花费的总时间。

  164. Complete requests表示总请求数量,这是我们设置的参数之一。

  165. Failed requests表示失败的请求数量,这里的失败是指请求在连接服务器、发送数据等环节发生异常,以及无响应后超时的情况。如果接收到的HTTP响应数据的头信息中含有2XX以外的状态码,则会在测试结果中显示另一个名为“Non-2xx responses”的统计项,用于统计这部分请求数,这些请求并不算在失败的请求中。

  166. Total transferred表示所有请求的响应数据长度总和,包括每个HTTP响应数据的头信息和正文数据的长度。注意这里不包括HTTP请求数据的长度,仅仅为web服务器流向用户PC的应用层数据总长度。

  167. HTML transferred表示所有请求的响应数据中正文数据的总和,也就是减去了Total transferred中HTTP响应数据中的头信息的长度。

  168. Requests per second吞吐率,计算公式:Complete requests/Time taken for tests

  169. Time per request用户平均请求等待时间,计算公式:Time token for tests/(Complete requests/Concurrency Level)。

  170. Time per requet(across all concurrent request)服务器平均请求等待时间,计算公式:Time taken for tests/Complete requests,正好是吞吐率的倒数。也可以这么统计:Time per request/Concurrency Level。

  171. Transfer rate表示这些请求在单位时间内从服务器获取的数据长度,计算公式:Total trnasferred/ Time taken for tests,这个统计很好的说明服务器的处理能力达到极限时,其出口宽带的需求量。

  172. Percentage of requests served within a certain time(ms)这部分数据用于描述每个请求处理时间的分布情况,比如以上测试,80%的请求处理时间都不超过6ms,这个处理时间是指前面的Time per request,即对于单个用户而言,平均每个请求的处理时间。

  173. 6.其它web如nginx测试一样


阅读(1114) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~