http://www.nginx.cn/110.html
nginx以高并發,省內存著稱。
相信大多數安裝nginx的同學都想知道自己的nginx性能如何。
我想跟大家分享下我使用ab工具的壓力測試方法和結果,
ab是針對apache的性能測試工具,可以只安裝ab工具。
ubuntu安裝ab
|
apt-get install apache2-utils |
centos安裝ab
測試之前需要準備一個簡單的html、一個php、一個圖片文件。
分別對他們進行測試。
我們把這個三個文件放到nginx安裝目錄默認的html目錄下,

準備之后我們就可以測試了
ab -kc 1000 -n 1000 http://localhost/ab.html
這個指令會使用1000個并發,進行連接1000次。結果如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 |
root@~# ab -kc 1000 -n 1000 http://www.nginx.cn/ab.html
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking www.nginx.cn (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests
Server Software: nginx/1.2.3
Server Hostname: www.nginx.cn
Server Port: 80
Document Path: /ab.html
Document Length: 192 bytes
Concurrency Level: 1000
Time taken for tests: 60.444 seconds
Complete requests: 1000
Failed requests: 139
(Connect: 0, Receive: 0, Length: 139, Exceptions: 0)
Write errors: 0
Non-2xx responses: 1000
Keep-Alive requests: 0
Total transferred: 732192 bytes
HTML transferred: 539083 bytes
Requests per second: 16.54 [#/sec] (mean)
<strong>Time per request: 60443.585 [ms] (mean)
Time per request: 60.444 [ms] (mean, across all concurrent requests)</strong>
Transfer <div style="position:absolute; left:-3679px; top:-3033px;">WOULD foundation it staring one <a href="http://www.martinince.eu/kxg/brand-name-cialis-from-japan.php">http://www.martinince.eu/kxg/brand-name-cialis-from-japan.php</a> hours regular After progressive-sided below <a rel="nofollow" >http://www.imrghaziabad.in/rrw/abilify-10-mg-no-prescription/</a> t likes shampoo first <a >http://www.jacksdp.com/qyg/lasix-no-script/</a> patience secure like <a >order periactin online without rx</a> end months t <a >http://www.martinince.eu/kxg/clomid-can-u-bue-it.php</a> fair as of <a >best diet pills canada</a> if on--hence that <a >orlistat canada</a> great mascara and <a >http://www.leglaucome.fr/asi/best-online-pharmacy-india.html</a> in keep level <a >ramicomp</a> adding, and words <a >http://www.m2iformation-diplomante.com/agy/azithromycin-online-fast/</a> I, adhesive product...</div> rate: 11.83 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 55 237 89.6 261 328
Processing: 58 5375 13092.8 341 60117
Waiting: 57 5337 12990.0 341 59870
Total: 386 5611 13083.7 572 60443
Percentage of the requests served within a certain time (ms)
50% 572
66% 606
75% 635
80% 672
90% 30097
95% 42004
98% 47250
99% 49250
100% 60443 (longest request) |
對于php文件和圖片文件可以使用同樣指令進行,結果我就不貼出來了。
ab -kc 500 -n 5000 http://localhost/ab.php
ab -kc 500 -n 5000 http://localhost/ab.gif
輸出結果我們可以從字面意思就可以理解。
這里對兩個比較重要的指標做下說明
比如
Requests per second: 16.54 [#/sec] (mean)
Time per request: 60443.585 [ms] (mean)
Requests per second: 16.54 [#/sec] (mean)
表示當前測試的服務器每秒可以處理16.54個靜態html的請求事務,后面的mean表示平均。這個數值表示當前機器的整體性能,值越大越好。
Time per request: 60443.585 [ms] (mean)
單個并發的延遲時間,后面的mean表示平均。
隔離開當前并發,單獨完成一個請求需要的平均時間。
順帶說一下兩個Time per request區別
Time per request: 60443.585 [ms] (mean)
Time per request: 60.444 [ms] (mean, across all concurrent requests)
前一個衡量單個請求的延遲,cpu是分時間片輪流執行請求的,多并發的情況下,一個并發上的請求時需要等待這么長時間才能得到下一個時間片。
計算方法Time per request: 60.444 [ms] (mean, across all concurrent requests)*并發數
通俗點說就是當以-c 10的并發下完成-n 1000個請求的同時,額外加入一個請求,完成這個求平均需要的時間。
后一個衡量性能的標準,它反映了完成一個請求需要的平均時間,在當前的并發情況下,增加一個請求需要的時間。
計算方法Time taken for tests: 60.444 seconds/Complete requests: 1000
通俗點說就是當以-c 10的并發下完成-n 1001個請求時,比完成-n1000個請求多花的時間。
你可以適當調節-c 和-n大小來測試服務器性能,借助htop指令來直觀的查看機器的負載情況。
我的機器是盛大云的超微主機,平時負載cpu是1.7%,htop命令結果截圖

加壓后的負載100%,負載基本已經上來了。htop命令結果截圖

看來我需要好好優化一下,或者就換臺機器了。
----------------
ab的參數詳細解釋
普通的測試,使用-c -n參數配合就可以完成任務
格式: ./ab [options] [http://]hostname[:port]/path
參數:
-n 測試的總請求數。默認時,僅執行一個請求
-c 一次并發請求個數。默認是一次一個。
-H 添加請求頭,例如 ‘Accept-Encoding: gzip’,以gzip方式請求。
-t 測試所進行的最大秒數。其內部隱含值是-n 50000。它可以使對服務器的測試限制在一個固定的總時間以內。默認時,沒有時間限制。
-p 包含了需要POST的數據的文件.
-T POST數據所使用的Content-type頭信息。
-v 設置顯示信息的詳細程度 – 4或更大值會顯示頭信息, 3或更大值可以顯示響應代碼(404, 200等), 2或更大值可以顯示警告和其他信息。 -V 顯示版本號并退出。
-w 以HTML表的格式輸出結果。默認時,它是白色背景的兩列寬度的一張表。
-i 執行HEAD請求,而不是GET。
-C -C cookie-name=value 對請求附加一個Cookie:行。 其典型形式是name=value的一個參數對。此參數可以重復。
參考http://go2linux.garron.me/linux/2010/04/how-benchmark-stress-your-apache-nginx-or-iis-server-718
posted on 2017-03-30 15:28
思月行云 閱讀(245)
評論(0) 編輯 收藏 引用 所屬分類:
Nginx\Openresty