使用ab和wrk對騰訊CLS進(jìn)行benchmark測試

使用ab和wrk對騰訊云日志服務(wù)CLS進(jìn)行壓力測試,以此為例對ab和wrk進(jìn)行說明

ab

ab,全稱是apache benchmark,是apache官方推出的工具。該工具是用來測試Apache服務(wù)器的性能的。查看安裝的apache的服務(wù)器能提供的服務(wù)能力,每秒可以處理多少次請求。ab 執(zhí)行時常用的選項(xiàng)如下表:

選項(xiàng) 作用
-c 并發(fā)數(shù), 一次發(fā)送的總請求數(shù),默認(rèn)是一次發(fā)一個請求。
-k 打開keep-alive,在一個HTTP Session中請求多次。默認(rèn)是關(guān)閉的。
-n 請求數(shù), 整個benchmark測試過程中需要發(fā)送的請求次數(shù)。默認(rèn)是一次,默認(rèn)情況下得到的性能參數(shù)沒有代表性。
-t 最大時間,benchmark測試最長時間,默認(rèn)沒有限制。
-u 上傳文件,PUT操作時使用,需要設(shè)置-T選項(xiàng)
-T 設(shè)置上傳文件的Content-Type
-p postfile,指定包含post數(shù)據(jù)的文件
-r 當(dāng)接收到socket錯誤的時候ab不退出

安裝

apt-get install apache2-utils

注意事項(xiàng)

  • 觀察測試工具ab所在機(jī)器,以及被測試的前端機(jī)的CPU,內(nèi)存,網(wǎng)絡(luò)等都不超過最高限度的75%。
  • 測試中可能出現(xiàn)端口不足導(dǎo)致的測試失敗

需要調(diào)整內(nèi)核參數(shù)以支持端口重用,在Linux平臺下需要在/etc/sysctl.conf文件中添加如下內(nèi)容

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

然后運(yùn)行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數(shù)據(jù)為google protocol buffer格式的數(shù)據(jù)

結(jié)果分析

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)

從測試結(jié)果,我們可以看到

  • 在50個并發(fā)請求的情況下,請求60秒,平均每秒可以處理3731次(也就是說,客戶端在這種壓力下,看到的QPS為3731)
  • 平均每次請求處理的Latency為13.4ms
  • 由于開啟了keep-alive,連接幾乎不耗時間
  • 99%的請求都在26ms內(nèi)完成,最長的請求是164ms

使用騰訊云主機(jī)測試結(jié)果如下

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)

從結(jié)果我們可以看到,QPS是非騰訊云主機(jī)的2倍,為7482

wrk

wrk是一個用來做HTTP benchmark測試的工具??梢援a(chǎn)生顯著的壓力。

安裝

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

請求的內(nèi)容在/tmp/wrk_post.lua中規(guī)定,有5個線程,開啟的連接有50個,運(yùn)行60秒

其中/tmp/wrk_post.lua中的內(nèi)容是

request = function()
  mypath = "/tmp/post_data.txt";
  local file = io.open(mypath, "r");
  assert(file);
  local body = file:read("*a");      -- 讀取所有內(nèi)容
  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

結(jié)果分析

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

從測試結(jié)果,我們可以看到

  • 在5個并發(fā)請求的情況下,開啟50個連接,請求60秒,平均每秒可以處理3688次(也就是說,客戶端在這種壓力下,看到的QPS為3688)
  • 平均每次請求處理的Latency為15.91ms

使用騰訊云主機(jī)測試結(jié)果如下

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

從結(jié)果我們可以看到,QPS是非騰訊云主機(jī)的2倍,為7623

總結(jié)

以上就是用開源的benchmark工具來從客戶端的角度來衡量所能獲取的QPS以及Latency。但從客戶端看到的性能會受到各種因素的影響,例如請求的方式,本機(jī)的資源(CPU,內(nèi)存,網(wǎng)絡(luò)),CLS的網(wǎng)絡(luò)狀況,CLS的負(fù)載等都會影響客戶端看到的性能指標(biāo)。需要根據(jù)實(shí)際情況來查看性能瓶頸是來自于CLS還是來自于本機(jī)。


參考:

  1. 使用ab和wrk對OSS進(jìn)行benchmark測試

念念不忘,必有回響,小伙伴們幫我點(diǎn)個贊吧,非常感謝。

我是職場亮哥,YY高級軟件工程師、四年工作經(jīng)驗(yàn),拒絕咸魚爭當(dāng)龍頭的斜杠程序員。

聽我說,進(jìn)步多,程序人生一把梭

如果有幸能幫到你,請幫我點(diǎn)個【贊】,給個關(guān)注,如果能順帶評論給個鼓勵,將不勝感激。

職場亮哥文章列表:更多文章

本人所有文章、回答都與版權(quán)保護(hù)平臺有合作,著作權(quán)歸職場亮哥所有,未經(jīng)授權(quán),轉(zhuǎn)載必究!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容