Home  >  Article  >  Backend Development  >  Benchmark testing tool: ab

Benchmark testing tool: ab

WBOY
WBOYOriginal
2016-07-23 08:54:451178browse
Benchmark testing tools (ab)

What is ab?
ab is a command line-based tool that can run on Windows and Linux platforms.
Why choose this tool?
Easy to use, just enter a few command options and get results, and easy to install.

Install ab

win: If there is an apache development environment locally, then ab is in the bin directory under the apache directory by default. You can see it by opening the bin directory
linux: Just yum install httpd-tools and install it

Easy to get started

Type the following code directly (note to switch the directory where ab is located)
-c 10 means the number of concurrency is 10
-n 10 for a total of 100 visits
Followed by the URL to be visited, remember to indispensable http://

  1. D:phpStudyApachebin>ab -c 10 -n 1000 http://example.com/phpinfo.php
Copy the code

Wait for ab to run it yourself and get the following results

  1. #ab tool Description, pay attention to the version number
  2. This is ApacheBench, Version 2.3 <$Revision: 1604373 $>
  3. Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
  4. Licenseed to The Apache Software Foundation, http://www.apache.org/
  5. #Test results, a total of 1000 tests, the access progress is displayed every 100 times, the website visited is example.com
  6. Benchmarking example.com (be patient)
  7. Completed 100 requests
  8. Completed 200 requests
  9. Completed 300 requests
  10. Completed 400 requests
  11. Completed 500 requests
  12. Completed 600 requests
  13. Completed 700 requests
  14. Completed 800 requests
  15. Completed 900 requests
  16. Completed 1000 requests
  17. Finished 1000 requests
  18. #Server information
  19. Server Software: nginx/1.6.2 #Server web software
  20. Server Hostname: example.com #Host address
  21. Server Port: 80 #Access port
  22. #Document information
  23. Document Path: /phpinfo.php #Accessed document
  24. Document Length: 94804 bytes #Return the number of bytes of the result size (including the total number of bytes of all content in html, js, css, images and responses)
  25. #Link information
  26. Concurrency Level: 10 #Concurrency number, -c 10 sets 10 concurrencies
  27. Time taken for tests: 7.498 seconds #Total time taken, unit seconds
  28. Complete requests: 1000 #The sum of the total number of completed requests in the total requests
  29. Failed requests: 219 #The sum of the number of failed requests
  30. (Connect: 0, Receive: 0 , Length: 219, Exceptions: 0)
  31. Non-2xx responses: 9 #The total number of successful requests in the 2xx series that were not received
  32. Total transferred: 94135779 bytes #The total size bytes of the total response distance in the entire total number of requests, including header information
  33. HTML transferred: 93952076 bytes #The total size bytes of the text content in the total number of requests
  34. Requests per second: 133.36 [#/sec] (mean) #The number of concurrency supported per second, here is 133.36 concurrency supported per second
  35. Time per request: 74.984 [ms] (mean) #The total time it takes to complete a request
  36. Time per request: 7.498 [ms] (mean, across all concurrent requests) #The total time it takes to complete one request among all concurrent requests
  37. Transfer rate : 12259.83 [Kbytes/sec] received #Total number of bytes received per second (KB)
  38. #Test time result data, pay attention to the Total item, in the case of 10 concurrency, the minimum time to complete a request (min ) 9 milliseconds, maximum time consuming (max) 146 milliseconds
  39. Connection Times (ms)
  40. min mean[+/-sd] median max
  41. Connect: 0 0 0.7 0 20
  42. Processing: 9 74 22.2 68 146
  43. Waiting: 6 73 22.1 67 143
  44. Total: 9 74 22.2 68 146
  45. #Percentage of completed requests, for example, the first item, 50% 68, means that half of the requests are completed within 68 milliseconds, 95% 129, means that out of 1000 requests, 100% Ninety-eight requests were completed in 129 milliseconds
  46. Percentage of the requests served within a certain time (ms)
  47. 50% 68
  48. 66% 78
  49. 75% 86
  50. 80% 92
  51. 90% 109
  52. 95% 122
  53. 98% 129
  54. 99% 133
  55. 100% 146 (longest request)
Copy code
ab option

Enter the following command to get the ab help document, which explains the usage and the meaning of command options respectively

  1. D:phpStudyApachebin>ab -h
Copy the code

ab帮助文档的输出

  1. Usage: ab [options] [http://]hostname[:port]/path
  2. Options are:
  3. -n requests Number of requests to perform
  4. -c concurrency Number of multiple requests to make at a time
  5. -t timelimit Seconds to max. to spend on benchmarking
  6. This implies -n 50000
  7. -s timeout Seconds to max. wait for each response
  8. Default is 30 seconds
  9. -b windowsize Size of TCP send/receive buffer, in bytes
  10. -B address Address to bind to when making outgoing connections
  11. -p postfile File containing data to POST. Remember also to set -T
  12. -u putfile File containing data to PUT. Remember also to set -T
  13. -T content-type Content-type header to use for POST/PUT data, eg.
  14. 'application/x-www-form-urlencoded'
  15. Default is 'text/plain'
  16. -v verbosity How much troubleshooting info to print
  17. -w Print out results in HTML tables
  18. -i Use HEAD instead of GET
  19. -x attributes String to insert as table attributes
  20. -y attributes String to insert as tr attributes
  21. -z attributes String to insert as td or th attributes
  22. -C attribute Add cookie, eg. 'Apache=1234'. (repeatable)
  23. -H attribute Add Arbitrary header line, eg. 'Accept-Encoding: gzip'
  24. Inserted after all normal header lines. (repeatable)
  25. -A attribute Add Basic WWW Authentication, the attributes
  26. are a colon separated username and password.
  27. -P attribute Add Basic Proxy Authentication, the attributes
  28. are a colon separated username and password.
  29. -X proxy:port Proxyserver and port number to use
  30. -V Print version number and exit
  31. -k Use HTTP KeepAlive feature
  32. -d Do not show percentiles served table.
  33. -S Do not show confidence estimators and warnings.
  34. -q Do not show progress when doing more than 150 requests
  35. -l Accept variable document length (use this for dynamic pages)
  36. -g filename Output collected data to gnuplot format file.
  37. -e filename Output CSV file with percentages served
  38. -r Don't exit on socket receive errors.
  39. -m method Method name
  40. -h Display usage information (this message)
复制代码

英文看不懂,附上中文解释,网上抄的,我没试过

  1. -n 即requests,用于指定压力测试总共的执行次数。
  2. -c 即concurrency,用于指定压力测试的并发数。
  3. -t 即timelimit,等待响应的最大时间(单位:秒)。
  4. -b 即windowsize,TCP发送/接收的缓冲大小(单位:字节)。
  5. -p 即postfile,发送POST请求时需要上传的文件,此外还必须设置-T参数。
  6. -u 即putfile,发送PUT请求时需要上传的文件,此外还必须设置-T参数。
  7. -T 即content-type,用于设置Content-Type请求头信息,例如:application/x-www-form-urlencoded,默认值为text/plain。
  8. -v 即verbosity,指定打印帮助信息的冗余级别。
  9. -w 以HTML表格形式打印结果。
  10. -i 使用HEAD请求代替GET请求。
  11. -x 插入字符串作为table标签的属性。
  12. -y 插入字符串作为tr标签的属性。
  13. -z 插入字符串作为td标签的属性。
  14. -C 添加cookie信息,例如:"Apache=1234"(可以重复该参数选项以添加多个)。
  15. -H 添加任意的请求头,例如:"Accept-Encoding: gzip",请求头将会添加在现有的多个请求头之后(可以重复该参数选项以添加多个)。
  16. -A 添加一个基本的网络认证信息,用户名和密码之间用英文冒号隔开。
  17. -P 添加一个基本的代理认证信息,用户名和密码之间用英文冒号隔开。
  18. -X 指定使用的代理服务器和端口号,例如:"126.10.10.3:88"。
  19. -V 打印版本号并退出。
  20. -k 使用HTTP的KeepAlive特性。
  21. -k 使用HTTP的KeepAlive特性。
  22. -d 不显示百分比。
  23. -S 不显示预估和警告信息。
  24. -g 输出结果信息到gnuplot格式的文件中。
  25. -e 输出结果信息到CSV格式的文件中。
  26. -r 指定接收到错误信息时不退出程序。
  27. -h 显示用法信息,其实就是ab -help。
复制代码

Generally use -n, -c, -t
-n Total number of requests
-c Concurrency count
-t Concurrency count within specified seconds

Let me talk about the usage of

-t. The following means 10 concurrent visits in 20 seconds

  1. D:phpStudyApachebin> ab -c 10 -t 20 http://example.com/phpinfo.php
  2. This is ApacheBench, Version 2.3 <$Revision: 1604373 $>
  3. Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
  4. Licensed to The Apache Software Foundation, http://www.apache. org/
  5. Benchmarking example.com (be patient)
  6. Finished 3011 requests
  7. Server Software: nginx/1.6.2
  8. Server Hostname: example.com
  9. Server Port: 80
  10. Document Path: /phpinfo.php
  11. Document Length: 94804 bytes
  12. Concurrency Level: 10
  13. Time taken for tests: 20.006 seconds
  14. Complete requests: 3011
  15. Failed requests: 495
  16. (Connect: 0, Receive: 0, Length: 495, Exceptions: 0)
  17. Non-2xx responses: 54
  18. Total transferred: 280896464 bytes
  19. HTML transferred: 280344222 bytes
  20. Requests per second: 150.50 [#/sec] (mean)
  21. Time per request: 66.444 [ms] (mean)
  22. Time per request: 6.644 [ms] (mean, across all concurrent requests)
  23. Transfer rate: 13711.44 [Kbytes/sec] received
  24. Connection Times (ms)
  25. min mean[+/-sd] median max
  26. Connect: 0 0 0.5 0 10
  27. Processing: 6 66 17.6 63 203
  28. Waiting: 5 64 17.3 61 202
  29. Total: 6 66 17.6 63 203 % 75
  30. 90% 82
  31. 95% 92
  32. 98% 113
  33. 99% 125
  34. 100% 203 (longest request)
  35. Copy code
  36. Special note: If you visit http://www.example.com/, no specific File, remember to add the last /, be sure to add it! ! ! Otherwise, you may not be able to access it! ! !
Circumstances that may affect benchmark testing
Geolocation and network issues Response file size

Code complexity

Browser parsing

Web server configuration

Geolocation and network issues

If the web server is stored overseas, then we access the overseas website locally. As you can imagine, we pass through the router nodes, server nodes, and then through the optical cable under the Pacific Ocean, and then access the web server. After processing by the web server , continue with the submarine optical cable, and jump back to each server and routing node. This will definitely affect the test results (my suggestion is to conduct ab testing directly on the server)

Response file size

If a 3MB page is sent, the server network card will split the 3MB data into a single small data packet. During the transmission process, if only one data packet is damaged or lost, all data packets will be resent, so the sent packet The smaller the better, and at the same time, the smaller the data transfer, the faster it is transferred to the user's machine.

code complexity

The complexity of the code means complex processing of business logic, as well as file calls, database access, remote API interface calls, etc., which will affect the processing time

Browser parsing

Each browser has its own way of processing js, css, and html. Just think about the difference between IE8 and below and chrome

Web server settings

After the general Web server is installed, simple settings can be used to implement Web services (out of the box). However, such settings do not maximize the performance of the Web server. Senior engineers are required to configure the server to maximize performance. Here are simple and practical Keep-Alive instructions.

The function of Keep-Alive is that the web server opens a specific number of connections and keeps these connections open so that it can quickly process incoming requests, so that it does not open a connection for each incoming request and then process the request. , Reduce the number of processes on the server that process requests, thereby increasing the number of concurrencies.

Use -k in ab for testing, such as the following sentence


#-k, which allows the server to save 5 concurrent connections in an open and allowed state, allowing other requests to be processed quickly and reducing the processing time of creating new requests.
ab -c 5 -t 100 -k http://www.example.com/

Copy code
Testing tools
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn