什么是 Vegeta

Vegeta 是一个用 Go 语言编写的多功能的 HTTP 负载测试工具,它提供了命令行工具和一个开发库。

官网地址:https://github.com/tsenart/ve...

装置 Vegeta

Vegeta 装置非常简单,因为 Go 语言良好的跨平台性,能够间接下载官网的预编译版本后开箱即用。

预编译版本
这里以 Linux 版本为例:

$ wget https://github.com/tsenart/vegeta/releases/download/v7.0.3/vegeta-7.0.3-linux-amd64.tar.gz$ tar xzvf vegeta-7.0.3-linux-amd64.tar.gz$ mv vegeta  /usr/local/bin/

如果你应用的是其它平台,可依据理论状况在官网下载页面下载对应版本解压后就能够应用。

  • macOS

    $ brew install vegeta

Vegeta 用法

$ vegeta --helpUsage: vegeta [global flags] <command> [command flags] global flags:  -cpus int        应用 CPU 的数量 (默认为 4 个)  -profile string        指定在执行期间启用哪个分析器,反对 cpu 和 heap。   -version        打印版本并退出。 attack command:  -body string        指定申请主体文件里的内容。  -cert string        指定用于 HTTPS 申请的 PEM 格局的客户端证书文件。如果 -key 未指定,它会被设置为这个标记的值。  -connections int        指定每个指标主机关上的闲暇连贯的最大数目,默认值为 10000。  -duration duration        指定发送申请到指标主机的时长,用 0 示意永恒。  -header value        指定指标的申请头,能够反复指定多个申请头。  -http2        指定是否向反对的服务器发送 HTTP/2 申请,默认为:true。  -insecure        指定是否疏忽有效的服务器 TLS 证书。  -keepalive        指定是否应用长久链接,默认值为:true。  -key string        指定 HTTPS 申请中应用的 PEM 编码的 SSL 客户端证书私钥文件。  -laddr value        指定要应用的本地 I P地址,默认值为:0.0.0.0。  -lazy        指定是否应用提早模式读取指标。  -output string        指定输入文件的地位,默认为规范输入。  -rate uint        指定每秒钟对指标发送的申请数,默认值为:50。  -redirects int        指定每个申请的重定向的最大次数,默认为 10 次。当值为 -1, 不会遵循重定向但响应标记为胜利。  -root-certs value        指定可信的 TLS 根证书文件,多个的状况下应用逗号分隔。如果未指定,应用零碎默认的 CA 证书。  -targets string        指定指标文件,默认为规范输出。  -timeout duration        指定每个申请的超时工夫,默认值为 30s。  -workers uint        指定初始化过程数量,默认值为 10。report command:  -inputs string      指定报告输出文件,默认为规范输出。  -output string      指定报告输入文件,默认为规范输入。  -reporter string      指定要生成的报告的格局,反对 text,json, plot, hist[buckets]。默认为文本。 dump command:  -dumper string      指定转存文件,反对 json, csv 格局。默认为 json 格局。  -inputs string      指定要转存的输出文件,默认为规范输出,指定多个用逗号分隔。  -output string      指定要转存的输入文件,默认为规范输入。

Vegeta 应用实例

应用规范输出进行压测并生成报告

$ echo "GET http://www.hi-linux.com/img/linux/docker-dry-01.png" | vegeta attack -rate=50 -connections=1 -duration=1s | tee results.bin | vegeta reportRequests      [total, rate]            50, 51.02Duration      [total, attack, wait]    37.41975091s, 979.999689ms, 36.439751221sLatencies     [mean, 50, 95, 99, max]  33.816664905s, 35.021110945s, 37.058121168s, 37.204824958s, 37.319751221sBytes In      [total, mean]            9505000, 190100.00Bytes Out     [total, mean]            0, 0.00Success       [ratio]                  100.00%Status Codes  [code:count]             200:50Error Set:

应用指标文件的内容进行压测

  • 创立 target.txt 文件,内容如下:

    $ vim target.txtGET http://www.hi-linux.com/GET http://www.hi-linux.com/posts/19779.htmlGET http://www.hi-linux.com/img/linux/docker-dry-01.png
  • 应用 Vegeta 进行压测

    $ vegeta attack -targets="target.txt" -rate=10 -duration=2s  > results.bin
  • 自定义指标文件
    除了后面定义的最简略 HTTP GET 申请外,你还能够定义上面这些更灵便更简单的 HTTP 申请:
  1. 自定义申请头

    GET http://user:password@hi-linux.com:80/path/toX-Account-ID: 8675309DELETE http://hi-linux.com:80/path/to/removeConfirmation-Token: 90215Authorization: Token DEADBEEF

    2.自定义申请的主体

    POST http://hi-linux.com:80/things@/path/to/newthing.jsonPATCH http://hi-linux.com:80/thing/71988591@/path/to/thing-71988591.json
  2. 自定义申请头和申请主体

    POST http://hi-linux.com:80/thingsX-Account-ID: 99@/path/to/newthing.json

    生成压测报告

  3. 生成 JSON 格局的压测报告

    $ vegeta report -inputs=results.bin -reporter=json > metrics.json
  4. 生成基于 Dygraphs 的 HTML 5 压测报告

    $ cat results.bin | vegeta report -reporter=plot > plot.html

  • 计算并输入一个基于文本的直方图

    $ cat results.bin | vegeta report -reporter="hist[0,2ms,4ms,6ms]"Bucket         #     %       Histogram[0,     2ms]   6007  32.65%  ########################[2ms,   4ms]   5505  29.92%  ######################[4ms,   6ms]   2117  11.51%  ########[6ms,   +Inf]  4771  25.93%  ###################
  • 生成实时图形压测报告

如果您是 iTerm 用户,能够应用 jaggr 将 Vegeta 与 jplot 整合在一起并在终端上实时绘制压测报告。要实现这个性能你首先须要先装置 jaggr 和 jplot:

  1. 装置 jaggr

    # macOS$ brew install rs/tap/jaggr# 源代码装置$ go get -u github.com/rs/jaggr
  2. 装置 jplot

    # macOS$ brew install rs/tap/jplot# 源代码装置go get -u github.com/rs/jplot

    装置完 jaggr 和 jplot 后,其次你须要在 iTerm 中执行以下命令:

    $ echo 'GET http://www.hi-linux.com' | \ vegeta attack -rate 50 -duration 5m | vegeta dump | \ jaggr @count=rps \       hist\[100,200,300,400,500\]:code \       p25,p50,p95:latency \       sum:bytes_in \       sum:bytes_out | \ jplot rps+code.hist.100+code.hist.200+code.hist.300+code.hist.400+code.hist.500 \       latency.p95+latency.p50+latency.p25 \       bytes_in.sum+bytes_out.sum

分布式压力测试

当进行大规模负载测试时,通常因为受限于 Vegeta 本身机器的性能瓶颈(比方:关上的文件数,内存大小,CPU 和 网络带宽)限度而无奈达到预期后果。 这时分布式的应用 Vegeta 能够很好的解决这个问题,实现相似性能的工具很多,比方功能强大的 Ansible。这里咱们应用 Pdsh 来实现:

$ pdsh -b -w '10.0.1.1,10.0.2.1,10.0.3.1' \    'echo "GET http://target/" | vegeta attack -rate=20000 -duration=60s > result.bin'
Pdsh 的全称是 (Parallel Distributed Shell),Pdsh 可并行的执行对指标主机的操作,很不便的批量执行命令和散发工作。Pdsh 还反对交互模式,当要执行的命令不确定时,可间接进入 Pdsh命令行,十分不便。

实现后面的命令后,咱们就能够通过 Shell 脚本将后果文件收集到的一起供前面生成报表时应用。

$ for machine in "10.0.1.1 10.0.2.1 10.0.3.1"; do    scp $machine:~/result.bin $machine.bin &  done

最初咱们能够通过 vegeta report 命令生成此次压测的报表。vegeta report 命令可一次性读取应用逗号分隔的多个后果文件并生成报告,默认通过工夫戳进行排序。

$ vegeta report -inputs="10.0.1.1.bin,10.0.2.1.bin,10.0.3.1.bin"Requests      [total, rate]         3600000, 60000.00Latencies     [mean, 95, 99, max]   223.340085ms, 326.913687ms, 416.537743ms, 7.788103259sBytes In      [total, mean]         3714690, 3095.57Bytes Out     [total, mean]         0, 0.00Success       [ratio]               100.0%Status Codes  [code:count]          200:3600000Error Set:

其它相干

如果你感觉命令行下应用 Vegeta 比较复杂的话,你还能够应用 Alex 我的项目。Alex 是一个基于 Vegeta Library 和 Boom 封装的压力测试 Web UI,Vegeta 提供稳固的 QPS 压力源,Boom 提供稳固的并发数压力源。

我的项目地址:https://github.com/ireaderlab...

  • Alex 架构图

Alex 效果图

参考文档

  • http://www.google.com
  • http://t.cn/RBqHjIu
  • http://t.cn/zQ104Zp
  • http://t.cn/R5spIPf
原文地址 https://www.hi-linux.com/post...

关注 golang技术实验室
获取更多好文

本文由mdnice多平台公布