$ ab -c 10 -n 200 http://www.abc.ccc/ 模擬 10人的點擊,總共做 200 次 reqests(點200次網頁) 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 www.abc.ccc (be patient) Completed 100 requests Completed 200 requests Finished 200 requests Server Software: Apache/2.4.39 Server Hostname: www.abc.ccc Server Port: 80 Document Path: / Document Length: 19994 bytes 網頁的內容長度 Concurrency Level: 10 模擬同時間 10個人連線 Time taken for tests: 9.975 seconds 壓力測試所花費時間 Complete requests: 200 成功的request數目 Failed requests: 0 失敗的request數目 Total transferred: 4210400 bytes 全部資料傳輸(包含 http header 資料) HTML transferred: 3998800 bytes 全部資料傳輸中,HTML 部分的總長度 Requests per second: 20.05 [#/sec] (mean) Web Server 每秒幾個回應 Time per request: 498.732 [ms] (mean) Web Server 每個回應平均花費多久 ms Time per request: 49.873 [ms] (mean, across all concurrent requests) Transfer rate: 412.22 [Kbytes/sec] received Web Server 傳輸速度(每秒幾Kb) Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 1.0 0 9 Processing: 78 479 299.1 425 2073 Waiting: 75 478 299.2 424 2071 Total: 78 479 299.5 425 2082 Percentage of the requests served within a certain time (ms) 50% 425 意思是指:有 50% 的requests 都在 425ms 內回應 66% 534 75% 617 80% 662 90% 800 95% 1013 98% 1424 99% 2054 意思是指:有 99% 的 requests 都在 2054ms 內回應 100% 2082 (longest request) 以上測試是 web server 與測試者(ab) 不在相同的網路 (實際做壓力測試時,web server 應該與測試者在同一個網段比較好) 若是在同一個網路環境,大致就會像這樣: Percentage of the requests served within a certain time (ms) 50% 20 66% 20 75% 21 80% 21 90% 21 95% 22 98% 23 99% 29 100% 31 (longest request)
特別說明:
Time per request 有兩個,計算方式不同:
Time per request: 498.732 [ms] (mean)
Time per request: 49.873 [ms] (mean, across all concurrent requests)
第一個 Time per request
公式 = sum(所有request時間) / request數目
公式 = sum(request 1 的時間 + request 2 的時間 + ....) / 200
舉個簡單一點的例子,總共發出 4 個 request
$ ab -c 2 -n 4 http://www.abc.ccc/
4個 request 各花費的時間:
request 1 花費 0.1
request 2 花費 0.2
request 3 花費 0.3
request 4 花費 0.4
Time per request = (0.1 + 0.2 + 0.3 + 0.4) / 4 = 0.25
第二個 Time per request
公式 = 整個測試花費的總時間 / Complete requests
公式 = Time taken for tests / Complete requests
本例 = 9.975 seconds / 200 = 0.049875
另外,網站出現 404 / 500 之類的錯誤
並不會被列為 Failed requests,仍舊算為 Complete requests
官方文件:
ab - Apache HTTP server benchmarking tool
ab is a tool for benchmarking your Apache Hypertext Transfer Protocol (HTTP) server. It is designed to give you an impression of how your current Apache installation performs. This especially shows you how many requests per second your Apache installation is capable of serving.
另外
連線數( -c ) 超過 1024 時,應該會出現
socket: Too many open files (24) 的錯誤
例如:
$ ab -c 1300 -n 4000 http://www.abc.ccc/
會出現 socket: Too many open files (24) 的錯誤
用這個指令查看目前的限制:
$ ulimit -n
1024
$ ulimit -a <--有更多的資料
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 29222
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1300
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 4096
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
修改限制 (注意!這是臨時性的修改,登出後 就恢復原值)
$ ulimit -n 1300
想要永久性修改,要這樣
$ echo "* soft nofile 65536" >>/etc/security/limits.conf
$ echo "* hard nofile 65536" >>/etc/security/limits.conf