关于python:使用ab和wrk对腾讯CLS进行benchmark测试

5次阅读

共计 5565 个字符,预计需要花费 14 分钟才能阅读完成。

应用 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 不退出

<!–more–>

装置

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 还是来自于本机。


参考:

  1. 应用 ab 和 wrk 对 OSS 进行 benchmark 测试

记得帮我点赞哦!

精心整顿了计算机各个方向的从入门、进阶、实战的视频课程和电子书,依照目录正当分类,总能找到你须要的学习材料,还在等什么?快去关注下载吧!!!

朝思暮想,必有回响,小伙伴们帮我点个赞吧,非常感谢。

我是职场亮哥,YY 高级软件工程师、四年工作教训,回绝咸鱼争当龙头的斜杠程序员。

听我说,提高多,程序人生一把梭

如果有幸能帮到你,请帮我点个【赞】,给个关注,如果能顺带评论给个激励,将不胜感激。

职场亮哥文章列表:更多文章

自己所有文章、答复都与版权保护平台有单干,著作权归职场亮哥所有,未经受权,转载必究!

正文完
 0