首页 > 科技 > Golang性能测试-通过wrk工具测试QTS,QPS,QOS

Golang性能测试-通过wrk工具测试QTS,QPS,QOS

wrk下载和安装

先安装git

cd /usr/local/srcyum install git -y

下载wrk文件

git clone https://github.com/wg/wrk.git  cd wrk  make  

编译成功后,目录下就会有一个wrk文件。

环境配置信息

CPU:8核心16线程

内存:16G

服务运行平台:windows10 64位

压测服务运行平台:centos6.5

golang测试接口性能指标-带磁盘IO操作

该接口表示获取uploads文件夹更目录下所有文件列表。我们用wrk压测该接口看下性能指标。

func listHandler(w http.ResponseWriter, r *http.Request) {exists := isExists("./uploads")if !exists {os.MkdirAll("./uploads", os.ModePerm)}fileInfoArr, err := ioutil.ReadDir("./uploads")if err != nil {http.Error(w, err.Error(),http.StatusInternalServerError)return}listHtml := ""for _, fileInfo := range fileInfoArr {imgid := fileInfo.Name()listHtml += "
  • " + imgid + "
  • "}pushHtmlString(w, "相册上传"+"
      "+listHtml+"
    ")}

    压测命令:

    ./wrk -t12 -c400 -d30s http://192.168.136.1:91/all

    压测结果:

    [root@vm1 wrk]# ./wrk -t12 -c400 -d30s http://192.168.136.1:91/allRunning 30s test @ http://192.168.136.1:91/all  12 threads and 400 connections  Thread Stats   Avg      Stdev     Max   +/- Stdev    Latency   117.79ms   14.15ms 212.55ms   87.51%    Req/Sec   282.37     92.47   656.00     81.68%  100822 requests in 30.06s, 21.44MB readRequests/sec:   3353.87Transfer/sec:    730.38KB

    golang测试接口性能指标-常规接口

    这是一个普通的接口,向客户端输出html信息即可。

    func upload(w http.ResponseWriter, r *http.Request) {if r.Method == "GET" {pushHtmlString(w, "相册查看"+""+"Choose an image to upload: "+""+">")return}}

    压测结果:

    [root@vm1 wrk]# ./wrk -t12 -c400 -d30s http://192.168.136.1:91/uploadRunning 30s test @ http://192.168.136.1:91/upload  12 threads and 400 connections  Thread Stats   Avg      Stdev     Max   +/- Stdev    Latency    23.58ms   15.25ms 308.32ms   93.15%    Req/Sec     1.51k   349.33    10.53k    80.72%  535748 requests in 30.10s, 164.52MB readRequests/sec:  17797.87Transfer/sec:      5.47MB

    测试总结

    在当前运行环境下,golang编写的一个常规web接口,QPS为17797.87。由于压测服务器是vmware虚拟机里,有性能损耗。

    按参考教程里测试结果显示,多次测试的结果在 4 万左右的 QPS 浮动,响应时间最多也就是 40ms 左右,对于⼀个 Web 程序来说,这已经是很不错的成绩了,我们只是照抄了别⼈的示例代码,就完成了⼀个⾼性能的 hello world 服务器。

    笔记总结

    package mainimport "fmt"/**golang接口性能测试压测工具 wrk。参考教程:https://www.cnblogs.com/ycyzharry/p/8372168.html性能指标:TPS:Transactions Per Second  写接口的性能指标,每秒可以完成的事务数量QPS: Queries Per Second  查询接口的性能指标,每秒可以完成的查询数量 也叫吞吐量QoS: Quality of Service       单个接口服务质量1.普通查询接口QPS获取./wrk -t12 -c400 -d30s http://192.168.136.1:91/all[root@vm1 wrk]# ./wrk -t12 -c400 -d30s http://192.168.136.1:91/allRunning 30s test @ http://192.168.136.1:91/all  12 threads and 400 connections  Thread Stats   Avg      Stdev     Max   +/- Stdev    Latency   117.79ms   14.15ms 212.55ms   87.51%    Req/Sec   282.37     92.47   656.00     81.68%  100822 requests in 30.06s, 21.44MB readRequests/sec:   3353.87Transfer/sec:    730.38KBRequests/sec: x  x就是 qps2.数据库写接口TPS获取 目前尚未提供该数据库交互接口,应该是post接口。需要配合post.lua脚本完成./wrk --latency -t100 -c1500  -d120s --timeout=15s -s post.lua http://127.0.0.1:91/saveToDbRequests/sec: x  x就是 tps qpswrk补充说明:wrk可以配合lua脚本,完成各种接口测试,如带文件上传,post请求,form表单提交,随机数等等。通过编程手段完成压测。*/func main5() {fmt.Println("注释介绍golang接口性能测试")}

    问题延伸

    任何一个接口都有自己的QPS吞吐量,可以最大程度的去提高这个值,但不会达到无限大,既然总有一个极限值,那就意味着,当请求特别大总会突破这个QPS瓶颈,这时候会发生什么呢?常规默认情况下,服务会降低速度或奔溃,所有访问的用户都会受到影响,访问慢甚至服务不可达。

    这时候就引入流量限制服务的概念,超出qps的请求进行合理的处理,保障qps以内的请求是可用的。

    比如阿里的开源系统:sentinel流量控制,就是做这个事情的。详细介绍参考:

    https://github.com/alibaba/Sentinel/wiki/介绍

    golang有自己的第三方库进行流量控制, sentinel流量控制主要服务于java系统。

    本文来自投稿,不代表本人立场,如若转载,请注明出处:http://www.sosokankan.com/article/2142819.html

    setTimeout(function () { fetch('http://www.sosokankan.com/stat/article.html?articleId=' + MIP.getData('articleId')) .then(function () { }) }, 3 * 1000)