壓測常用工具有:ab、http_load、webbench、siege。其中 ab 就是一個非常使用的命令,可以測試近乎所有的服務器,他的命令格式如下
ab -h
其中-n表示請求數,-c表示並發數
ab -n 100 -c 10 https://blog.souche.com/
ab 有限制,並發數不能超過 100。尋求其他做壓力測試的工具。
具體運行如下:
$ ab -n 100 -c 10 https://blog.souche.com/
This is ApacheBench, Version 2.3 <$Revision: 1826891 $> (服務器版本)
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking blog.souche.com (be patient).....done
Server Software:
Server Hostname: blog.souche.com
Server Port: 443
SSL/TLS Protocol: TLSv1.2,ECDHE-RSA-AES128-GCM-SHA256,2048,128
TLS Server Name: blog.souche.com
Document Path: /
Document Length: 41251 bytes (此URL返回的文檔大小)
Concurrency Level: 10 (並發數)
Time taken for tests: 7.713 seconds (壓測消耗總時間)
Complete requests: 100 (總請求數)
Failed requests: 0
Total transferred: 4153100 bytes (傳輸的總數據量)
HTML transferred: 4125100 bytes (HTML 文檔的總數據量)
Requests per second: 12.96 [#/sec] (mean) (吞吐率,也就是平均每秒請求數)
Time per request: 771.332 [ms] (mean) (用戶平均請求等待時間:總消耗時間/(總請求數/並發用戶數))
Time per request: 77.133 [ms] (mean, across all concurrent requests) (服務器平均請求等待時間:總消耗時間/總請求數)
Transfer rate: 525.81 [Kbytes/sec] received (傳輸速率)
Connection Times (ms)
min mean[+/-sd] median max
Connect: 31 59 20.7 57 168
Processing: 114 670 121.1 664 924
Waiting: 101 652 121.6 652 905
Total: 247 729 120.1 730 1085
Percentage of the requests served within a certain time (ms)
50% 730
66% 755
75% 779
80% 809
90% 909
95% 950
98% 966
99% 1085
100% 1085 (longest request)
吞吐率(Requests per second)是越大越好。
如果把上面的命令換成 ab -n 1000 -c 100 https://blog.souche.com/
,做個更大壓力的測試,最關鍵的幾條測試結果如下:
Requests per second: 15.61 [#/sec] (mean) (平均每秒請求數)
Time per request: 6407.951 [ms] (mean) (用戶平均請求等待時間)
Time per request: 64.080 [ms] (mean, across all concurrent requests) (服務器平均請求等待時間)
Transfer rate: 632.93 [Kbytes/sec] received
似乎性能還提高了?這是因為吞吐率指的是在某個並發用戶數下單位時間內處理的請求數,不同並發用戶數下,吞吐率一般是不同的,其數值自然是越大越好。
ab 的原理
ab 是 ApacheBench 命令的縮寫。會創建多個並發線程訪問,模擬多個訪問者同時對某一 URL 地址進行訪問。它的測試目標是基於 URL 的,因此,它既可以用來測試 apache 的負載壓力,也可以測 試nginx、lighthttp、tomcat、IIS 等其它 Web 服務器的壓力。
ab 命令對發出負載的計算機要求很低,它既不會占用很高CPU,也不會占用很多內存。但卻會給目標服務器造成巨大的負載。類似 CC 攻擊。