使用ab和wrk對騰訊云日志服務CLS進行壓力測驗,以此為例對ab和wrk進行說明
ab
ab,全稱是apache benchmark,是apache官方推出的工具,該工具是用來測驗Apache服務器的性能的,查看安裝的apache的服務器能提供的服務能力,每秒可以處理多少次請求,ab 執行時常用的選項如下表:
| 選項 | 作用 |
|---|---|
| -c | 并發數, 一次發送的總請求數,默認是一次發一個請求, |
| -k | 打開keep-alive,在一個HTTP Session中請求多次,默認是關閉的, |
| -n | 請求數, 整個benchmark測驗程序中需要發送的請求次數,默認是一次,默認情況下得到的性能引數沒有代表性, |
| -t | 最大時間,benchmark測驗最長時間,默認沒有限制, |
| -u | 上傳檔案,PUT操作時使用,需要設定-T選項 |
| -T | 設定上傳檔案的Content-Type |
| -p | postfile,指定包含post資料的檔案 |
| -r | 當接收到socket錯誤的時候ab不退出 |
安裝
apt-get install apache2-utils
注意事項
- 觀察測驗工具ab所在機器,以及被測驗的前端機的CPU,記憶體,網路等都不超過最高限度的75%,
- 測驗中可能出現埠不足導致的測驗失敗
需要調整內核引數以支持埠重用,在Linux平臺下需要在/etc/sysctl.conf檔案中添加如下內容
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 30
kernel.printk = 7 4 1 7
然后運行sudo sysctl –p生效
使用示例
ab -c 50 -t 60 -n 300000 -k -T 'application/x-protobuf' -p /tmp/post_data.txt -H 'Host: ap-shanghai.cls.myqcloud.com' -H 'Authorization: q-sign-algorithm=sha1&q-ak=AKIDMfonbuXfqpcFicn3YrzwivMelfNwFWcW&q-sign-time=1517472219;1517493819&q-key-time=1517472219;1517493819&q-header-list=content-type;host&q-url-param-list=&q-signature=4a4ed6ddc8ba1dfea73d2bee62def9dce8b0ca3c' http://ap-shanghai.cls.myqcloud.com/log
/tmp/post_data.txt資料為google protocol buffer格式的資料
結果分析
This is ApacheBench, Version 2.3 <$Revision: 1796539 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, https://www.apache.org/
Benchmarking ap-shanghai.cls.myqcloud.com (be patient)
Completed 30000 requests
Completed 60000 requests
Completed 90000 requests
Completed 120000 requests
Completed 150000 requests
Completed 180000 requests
Completed 210000 requests
Finished 223877 requests
Server Software: openresty
Server Hostname: ap-shanghai.cls.myqcloud.com
Server Port: 80
Document Path: /log
Document Length: 0 bytes
Concurrency Level: 50
Time taken for tests: 60.001 seconds
Complete requests: 223877
Failed requests: 0
Keep-Alive requests: 223027
Total transferred: 38726471 bytes
Total body sent: 108604595
HTML transferred: 0 bytes
Requests per second: 3731.24 [#/sec] (mean)
Time per request: 13.400 [ms] (mean)
Time per request: 0.268 [ms] (mean, across all concurrent requests)
Transfer rate: 630.31 [Kbytes/sec] received
1767.63 kb/s sent
2397.94 kb/s total
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.5 0 34
Processing: 9 13 3.8 13 164
Waiting: 8 13 3.8 13 164
Total: 9 13 3.8 13 164
Percentage of the requests served within a certain time (ms)
50% 13
66% 14
75% 14
80% 14
90% 15
95% 17
98% 22
99% 26
100% 164 (longest request)
從測驗結果,我們可以看到
- 在50個并發請求的情況下,請求60秒,平均每秒可以處理3731次(也就是說,客戶端在這種壓力下,看到的QPS為3731)
- 平均每次請求處理的Latency為13.4ms
- 由于開啟了keep-alive,連接幾乎不耗時間
- 99%的請求都在26ms內完成,最長的請求是164ms
使用騰訊云主機測驗結果如下
This is ApacheBench, Version 2.3 <$Revision: 1706008 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, https://www.apache.org/
Benchmarking ap-shanghai.cls.myqcloud.com (be patient)
Completed 30000 requests
Completed 60000 requests
Completed 90000 requests
Completed 120000 requests
Completed 150000 requests
Completed 180000 requests
Completed 210000 requests
Completed 240000 requests
Completed 270000 requests
Completed 300000 requests
Finished 300000 requests
Server Software: openresty
Server Hostname: ap-shanghai.cls.myqcloud.com
Server Port: 80
Document Path: /log
Document Length: 0 bytes
Concurrency Level: 50
Time taken for tests: 40.095 seconds
Complete requests: 300000
Failed requests: 0
Keep-Alive requests: 298850
Total transferred: 51894250 bytes
Total body sent: 145500000
HTML transferred: 0 bytes
Requests per second: 7482.21 [#/sec] (mean)
Time per request: 6.683 [ms] (mean)
Time per request: 0.134 [ms] (mean, across all concurrent requests)
Transfer rate: 1263.94 [Kbytes/sec] received
3543.82 kb/s sent
4807.77 kb/s total
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.1 0 6
Processing: 4 7 2.9 6 157
Waiting: 4 7 2.9 6 157
Total: 4 7 2.9 6 157
Percentage of the requests served within a certain time (ms)
50% 6
66% 7
75% 7
80% 8
90% 9
95% 10
98% 14
99% 18
100% 157 (longest request)
從結果我們可以看到,QPS是非騰訊云主機的2倍,為7482
wrk
wrk是一個用來做HTTP benchmark測驗的工具,可以產生顯著的壓力,
安裝
apt-get install libssl-dev
git clone https://github.com/wg/wrk.git
cd wrk
make
cp wrk /usr/sbin
使用示例
wrk -c 50 -d 60 -t 5 -s /tmp/wrk_post.lua http://ap-shanghai.cls.myqcloud.com
請求的內容在/tmp/wrk_post.lua中規定,有5個執行緒,開啟的連接有50個,運行60秒
其中/tmp/wrk_post.lua中的內容是
request = function()
mypath = "/tmp/post_data.txt";
local file = io.open(mypath, "r");
assert(file);
local body = file:read("*a"); -- 讀取所有內容
file:close();
wrk.body = body
path = "/log"
wrk.headers["Content-Type"] = "application/x-protobuf"
wrk.headers["Host"] = "ap-shanghai.cls.myqcloud.com"
wrk.headers["Authorization"] = "q-sign-algorithm=sha1&q-ak=AKIDMfonbuXfqpcFicn3YrzwivMelfNwFWcW&q-sign-time=1517472219;1517493819&q-key-time=1517472219;1517493819&q-header-list=content-type;host&q-url-param-list=&q-signature=4a4ed6ddc8ba1dfea73d2bee62def9dce8b0ca3c"
return wrk.format("POST", path)
end
結果分析
Running 1m test @ http://ap-shanghai.cls.myqcloud.com
5 threads and 50 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 15.91ms 25.52ms 880.85ms 98.05%
Req/Sec 745.32 105.11 848.00 94.79%
221561 requests in 1.00m, 36.55MB read
Requests/sec: 3688.17
Transfer/sec: 623.03KB
從測驗結果,我們可以看到
- 在5個并發請求的情況下,開啟50個連接,請求60秒,平均每秒可以處理3688次(也就是說,客戶端在這種壓力下,看到的QPS為3688)
- 平均每次請求處理的Latency為15.91ms
使用騰訊云主機測驗結果如下
Running 1m test @ http://ap-shanghai.cls.myqcloud.com
5 threads and 50 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 6.77ms 3.42ms 90.45ms 94.82%
Req/Sec 1.53k 119.04 1.74k 79.27%
457574 requests in 1.00m, 75.48MB read
Requests/sec: 7623.03
Transfer/sec: 1.26MB
從結果我們可以看到,QPS是非騰訊云主機的2倍,為7623
總結
以上就是用開源的benchmark工具來從客戶端的角度來衡量所能獲取的QPS以及Latency,但從客戶端看到的性能會受到各種因素的影響,例如請求的方式,本機的資源(CPU,記憶體,網路),CLS的網路狀況,CLS的負載等都會影響客戶端看到的性能指標,需要根據實際情況來查看性能瓶頸是來自于CLS還是來自于本機,
參考:
- 使用ab和wrk對OSS進行benchmark測驗
記得幫我點贊哦!
精心整理了計算機各個方向的從入門、進階、實戰的視頻課程和電子書,按照目錄合理分類,總能找到你需要的學習資料,還在等什么?快去關注下載吧!!!

念念不忘,必有回響,小伙伴們幫我點個贊吧,非常感謝,
我是職場亮哥,YY高級軟體工程師、四年作業經驗,拒絕咸魚爭當龍頭的斜杠程式員,
聽我說,進步多,程式人生一把梭
如果有幸能幫到你,請幫我點個【贊】,給個關注,如果能順帶評論給個鼓勵,將不勝感激,
職場亮哥文章串列:更多文章

本人所有文章、回答都與著作權保護平臺有合作,著作權歸職場亮哥所有,未經授權,轉載必究!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/235640.html
標籤:Python
上一篇:Python zip函式及用法
