服务器负载太大而影响程序效率也是很常见的,Apache服务器自带有一个叫AB(ApacheBench)的工具,可以对服务器进行负载测试
基本用法:
ab -n 全部请求数 -c 并发数测试url
注:可以将ab.exe 加入系统环境变量;或直接切换置 ab 目录执行。如: C:\Windows\System32> cd C:\xampp\apache\bin
例:C:\xampp\apache\bin>ab -n 1000 -c 50 http://www.abc.com/index.html
更为详细操作
C:\xampp\apache\bin>ab-n 100 -c 10 http://www.abc.com/index.html
This is ApacheBench, Version 2.0.40-dev<> apache-2.0
Copyright 1996 Adam Twiss, Zeus TechnologyLtd, http://www.zeustech.NET/
Copyright 2006 The Apache SoftwareFoundation, http://www.apache.org/
Benchmarking www.abc.com (bepatient).....done
Server Software: Apache
Server Hostname: www.abc.com
Server Port: 80
Document Path: /index.html
Document Length: 17784 bytes #请求文档大小
Concurrency Level: 10 #并发数
Time taken for tests: 11.898681 seconds #全部请求完成耗时
Complete requests: 100 #全部请求数
Failed requests: 0
Write errors: 0
Total transferred: 1805900 bytes #总传输大小
HTML transferred: 1778400 bytes
Requests per second: 8.40 [#/sec] (mean) #每秒请求数(平均)
Time per request: 1189.868 [ms] (mean) #每次并发请求时间(所有并发)
Time per request: 118.987 [ms] (mean, across all concurrentrequests
Transfer rate: 148.17 [Kbytes/sec] received #传输速率
Connection Times (ms) #连接时间
min mean[+/-sd] median(中位值) max
Connect: (#连接) 59 73 11.9 72 132
Processing: (#处理) 86 998538.7 938 2288
Waiting: (#等待) 63 135 106.8 94 663
Total: 155 1072540.6 1004 2362
在一定比例的请求服务时间
50% 1004
66% 1260
75% 1452
80% 1492
90% 1923
95% 2078
98% 2352
99% 2362
100% 2362 (longest request)
整个场景中所有请求的响应情况,在场景中每个请求都有一个响应时间,其中
50%的用户响应时间小于1104毫秒,
80%的用户响应时间小于1492毫秒,
最大的响应时间小于2362毫秒。
备:由于对发请求,cpu实际上并不是同时处理的,而是按照每个请求获得的时间片逐个轮转处理的,所以,基本上第一个Timeper request时间约等于第二个Timeper request时间乘以并发请求数。
其它参数:
**-n ** requests 全部请求数
-c concurrency 并发数
-t timelimit 最传等待回应时间
-p postfile POST数据文件
**-T **content-type POST Content-type
**-v **verbosity Howmuch troubleshooting info to print
-w Print outresults 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 加入cookie, eg.'Apache=1234. (repeatable)
-H attribute 加入http头, eg.'Accept-Encoding: gzip' Inserted after all normalheader lines. (repeatable)
**-A **attribute http验证,分隔传递用户名及密码
**-P **attribute Add Basic Proxy Authentication, theattributes are a colon separated usernameand password.
**-X **proxy:port 代理服务器
-V 查看ab版本
-k Use HTTPKeepAlive feature
-d Do not showpercentiles served table.
**-S ** Do not showconfidence estimators and warnings.
-g filename Output collected data to gnuplot formatfile.
**-e ** filename Output CSV file with percentages served
-h Display usageinformation (this message)