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

準(zhǔn)備之后我們就可以測試了
ab -kc 1000 -n 1000 http://localhost/ab.html
這個指令會使用1000個并發(fā),進(jìn)行連接1000次。結(jié)果如下
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文件和圖片文件可以使用同樣指令進(jìn)行,結(jié)果我就不貼出來了。
ab -kc 500 -n 5000 http://localhost/ab.php
ab -kc 500 -n 5000 http://localhost/ab.gif
輸出結(jié)果我們可以從字面意思就可以理解。
這里對兩個比較重要的指標(biāo)做下說明
比如
Requests per second: 16.54 [#/sec] (mean)
Time per request: 60443.585 [ms] (mean)
Requests per second: 16.54 [#/sec] (mean)
表示當(dāng)前測試的服務(wù)器每秒可以處理16.54個靜態(tài)html的請求事務(wù),后面的mean表示平均。這個數(shù)值表示當(dāng)前機(jī)器的整體性能,值越大越好。
Time per request: 60443.585 [ms] (mean)
單個并發(fā)的延遲時間,后面的mean表示平均。
隔離開當(dāng)前并發(fā),單獨(dú)完成一個請求需要的平均時間。
順帶說一下兩個Time per request區(qū)別
Time per request: 60443.585 [ms] (mean)
Time per request: 60.444 [ms] (mean, across all concurrent requests)
前一個衡量單個請求的延遲,cpu是分時間片輪流執(zhí)行請求的,多并發(fā)的情況下,一個并發(fā)上的請求時需要等待這么長時間才能得到下一個時間片。
計(jì)算方法Time per request: 60.444 [ms] (mean, across all concurrent requests)*并發(fā)數(shù)
通俗點(diǎn)說就是當(dāng)以-c 10的并發(fā)下完成-n 1000個請求的同時,額外加入一個請求,完成這個求平均需要的時間。
后一個衡量性能的標(biāo)準(zhǔn),它反映了完成一個請求需要的平均時間,在當(dāng)前的并發(fā)情況下,增加一個請求需要的時間。
計(jì)算方法Time taken for tests: 60.444 seconds/Complete requests: 1000
通俗點(diǎn)說就是當(dāng)以-c 10的并發(fā)下完成-n 1001個請求時,比完成-n1000個請求多花的時間。
你可以適當(dāng)調(diào)節(jié)-c 和-n大小來測試服務(wù)器性能,借助htop指令來直觀的查看機(jī)器的負(fù)載情況。
我的機(jī)器是盛大云的超微主機(jī),平時負(fù)載cpu是1.7%,htop命令結(jié)果截圖

加壓后的負(fù)載100%,負(fù)載基本已經(jīng)上來了。htop命令結(jié)果截圖

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