mcpField
V
Vegeta

Vegeta

官方Agent-ready
github.com开发者工具25.2kvegeta36 周前更新
代码仓库访问官网

关于 Vegeta

Vegeta 是一个高性能的 HTTP 负载测试工具,允许设置恒定的请求速率对目标服务进行压力测试。支持管道式的命令组合,可以灵活定义攻击参数、运行测试并生成详细的性能报告。

主要功能

  1. 01设置恒定请求速率进行持续负载测试
  2. 02通过管道组合多个命令阶段
  3. 03生成详细的性能报告和延迟分析

适用场景

  1. 01评估 API 服务器在高并发下的性能和稳定性
  2. 02在部署前验证应用的负载承载能力
  3. 03自动化性能基准测试工作流

安装

选择你的系统或包管理器,复制命令到终端运行。

$
brew install vegeta

安装完成后运行vegeta --version确认安装成功。

命令示例

常用命令按使用顺序排列。

01
echo "GET https://example.com" | vegeta attack | vegeta report

详细介绍

Vegeta Build Status Go Report Card PkgGoDev Gitter Donate

Vegeta 是一款多功能 HTTP 负载测试工具,旨在以恒定的请求速率对 HTTP 服务进行压力测试。它的战力超过 9000!

Vegeta

功能

  • 可作为命令行工具和 Go 库使用。
  • CLI 采用 UNIX 可组合性设计。
  • 避免恼人的协调遗漏。
  • 提供丰富的报告功能。
  • 易于用于分布式负载测试。
  • 易于安装和运行(静态二进制文件、包管理器等)。

安装

预编译可执行文件

可在此处获取。

macOS

你可以使用 Homebrew 安装 Vegeta:

$ brew update && brew install vegeta

也可以使用 MacPorts:

$ port install vegeta
Arch Linux
$ pacman -S vegeta
FreeBSD

在 FreeBSD 上,你可以使用内置的包管理器安装 Vegeta,因为已有可用的 Vegeta 软件包。

$ pkg install vegeta
源码
git clone https://github.com/tsenart/vegeta
cd vegeta
make vegeta
mv vegeta ~/bin # Or elsewhere, up to you.

版本管理

库和 CLI 均采用 SemVer v2.0.0 进行版本管理。

在 v8.0.0 之后,这两个组件 分别进行版本管理,以便更好地隔离各自的破坏性变更。

CLI 版本使用 cli/vMAJOR.MINOR.PATCH 标记,并发布在 GitHub releases 页面上。 至于库,新版本同时使用 lib/vMAJOR.MINOR.PATCH 和 vMAJOR.MINOR.PATCH 标记。 后一个标记是与 go mod 兼容所必需的。

参与贡献

请参阅 CONTRIBUTING.md。

使用手册

Usage: vegeta [global flags] <command> [command flags]

global flags:
  -cpus int
    	Number of CPUs to use (default = number of cpus)
  -profile string
    	Enable profiling of [cpu, heap]
  -version
    	Print version and exit

attack command:
  -body string
    	Requests body file
  -cert string
    	TLS client PEM encoded certificate file
  -chunked
    	Send body with chunked transfer encoding
  -connect-to value
    	A mapping of (ip|host):port to use instead of a target URL's (ip|host):port. Can be repeated multiple times.
    	Identical src:port with different dst:port will round-robin over the different dst:port pairs.
    	Example: google.com:80:localhost:6060
  -connections int
    	Max open idle connections per target host (default 10000)
  -dns-ttl value
    	Cache DNS lookups for the given duration [-1 = disabled, 0 = forever] (default 0s)
  -duration duration
    	Duration of the test [0 = forever]
  -format string
    	Targets format [http, json] (default "http")
  -h2c
    	Send HTTP/2 requests without TLS encryption
  -header value
    	Request header
  -http2
    	Send HTTP/2 requests when supported by the server (default true)
  -insecure
    	Ignore invalid server TLS certificates
  -keepalive
    	Use persistent connections (default true)
  -key string
    	TLS client PEM encoded private key file
  -laddr value
    	Local IP address (default 0.0.0.0)
  -lazy
    	Read targets lazily
  -max-body value
    	Maximum number of bytes to capture from response bodies. [-1 = no limit] (default -1)
  -max-connections int
    	Max connections per target host
  -max-workers uint
    	Maximum number of workers (default 18446744073709551615)
  -name string
    	Attack name
  -output string
    	Output file (default "stdout")
  -prometheus-addr string
    	Prometheus exporter listen address [empty = disabled]. Example: 0.0.0.0:8880
  -proxy-header value
    	Proxy CONNECT header
  -rate value
    	Number of requests per time unit [0 = infinity] (default 50/1s)
  -redirects int
    	Number of redirects to follow. -1 will not follow but marks as success (default 10)
  -resolvers value
    	List of addresses (ip:port) to use for DNS resolution. Disables use of local system DNS. (comma separated list)
  -root-certs value
    	TLS root certificate files (comma separated list)
  -session-tickets
    	Enable TLS session resumption using session tickets
  -targets string
    	Targets file (default "stdin")
  -timeout duration
    	Requests timeout (default 30s)
  -unix-socket string
    	Connect over a unix socket. This overrides the host address in target URLs
  -workers uint
    	Initial number of workers (default 10)

encode command:
  -output string
    	Output file (default "stdout")
  -to string
    	Output encoding [csv, gob, json] (default "json")

plot command:
  -output string
    	Output file (default "stdout")
  -threshold int
    	Threshold of data points above which series are downsampled. (default 4000)
  -title string
    	Title and header of the resulting HTML page (default "Vegeta Plot")

report command:
  -buckets string
    	Histogram buckets, e.g.: "[0,1ms,10ms]"
  -every duration
    	Report interval
  -output string
    	Output file (default "stdout")
  -type string
    	Report type to generate [text, json, hist[buckets], hdrplot] (default "text")

examples:
  echo "GET http://localhost/" | vegeta attack -duration=5s | tee results.bin | vegeta report
  vegeta report -type=json results.bin > metrics.json
  cat results.bin | vegeta plot > plot.html
  cat results.bin | vegeta report -type="hist[0,100ms,200ms,300ms]"
-cpus

指定内部使用的 CPU 数量。 默认值为系统可用的 CPU 数量。

-profile

指定执行期间要启用的分析器。支持 cpu 和 heap 分析。默认不启用。

-version

打印版本并退出。

attack 命令
-body

指定一个文件,其内容将作为每个请求的正文,除非针对每个攻击目标进行覆盖;请参阅 -targets。

-cert

指定用于 HTTPS 请求的 PEM 编码 TLS 客户端证书文件。 如果未指定 -key,则其值将设为此标志的值。

-chunked

指定是否使用分块传输编码发送请求正文。

-connections

指定每个目标主机的最大空闲开放连接数。

-dns-ttl

指定 DNS 查询结果的缓存时长。值为零表示永久缓存。 负值表示完全禁用缓存。

-max-connections

指定每个目标主机的最大连接数。

-duration

指定向目标发送请求的持续时间。 内部并发结构的设置会将此值作为变量。 由于响应延迟,测试的实际运行时间可能长于指定时间。 使用 0 表示无限攻击。

-format

指定要解码的目标格式。

json format

JSON 格式便于与动态生成目标的程序集成。 每个目标都是独占一行的 JSON 对象。method 和 url 字段为必填项。 如果存在 body 字段,则必须使用 base64 编码。生成的 JSON Schema 详细定义了该格式。

jq -ncM '{method: "GET", url: "http://goku", body: "Punch!" | @base64, header: {"Content-Type": ["text/plain"]}}' |
  vegeta attack -format=json -rate=100 | vegeta encode
http format

http 格式与 RFC 2616 中定义的纯文本 HTTP 消息格式几乎相同,但 不支持内联 HTTP 正文,只支持引用作为请求正文加载和使用的文件(如下例所示)。

虽然也可以由其他程序生成此格式的目标,但它最初是为了让用户手动编写目标,以满足简单的使用场景。

以下是 http 格式的有效目标文件示例:

简单目标
GET http://goku:9090/path/to/dragon?item=ball
GET http://user:password@goku:9090/path/to
HEAD http://goku:9090/path/to/success
带自定义标头的目标
GET http://user:password@goku:9090/path/to
X-Account-ID: 8675309

DELETE http://goku:9090/path/to/remove
Confirmation-Token: 90215
Authorization: Token DEADBEEF
带自定义正文的目标
POST http://goku:9090/things
@/path/to/newthing.json

PATCH http://goku:9090/thing/71988591
@/path/to/thing-71988591.json
带自定义正文和标头的目标
POST http://goku:9090/things
X-Account-ID: 99
@/path/to/newthing.json
添加注释

以 # 开头的行会被忽略。

# get a dragon ball
GET http://goku:9090/path/to/dragon?item=ball
# specify a test account
X-Account-ID: 99
-h2c

指定通过不使用 TLS 加密的 TCP 发送 HTTP2 请求。

-header

指定应用于所有已定义目标的请求标头,参见 -targets。 可以重复此标志以指定任意数量的标头。

-http2

指定是否为支持 HTTP/2 的服务启用 HTTP/2 请求。

-insecure

指定是否忽略无效的服务 TLS 证书。

-keepalive

指定是否在 HTTP 请求之间复用 TCP 连接。

-key

指定用于 HTTPS 请求的 PEM 编码 TLS 客户端证书私钥文件。

-laddr

指定要使用的本地 IP 地址。

-lazy

指定是否延迟读取输入目标,而不是一次性读取。 这样可以将目标流式传入攻击命令,并减少内存占用。 代价是每次向目标发送请求时都会增加延迟。

-max-body

指定从每个响应正文中捕获的最大字节数。剩余未读取的字节会被完整读取后丢弃。 设为 -1 表示不限制。它可以解析以下形式的值:

  • "10 MB" -> 10MB
  • "10240 g" -> 10TB
  • "2000" -> 2000B
  • "1tB" -> 1TB
  • "5 peta" -> 5PB
  • "28 kilobytes" -> 28KB
  • "1 gigabyte" -> 1GB
-name

指定要记录在响应中的攻击名称。

-output

指定写入二进制结果的输出文件。 该文件用于传递给 report 命令作为输入。默认为标准输出。

-rate

指定向目标发送请求的速率和时间单位。由于垃圾回收等因素,实际请求速率可能略有变化,但总体上应非常接近指定值。 如果未指定时间单位,则使用 1s。

-rate 为 0 或 infinity 时,vegeta 会以尽可能快的速度发送请求。 与 -max-workers 配合使用,可模拟一组固定数量的并发用户按顺序发送请求(即等待收到响应后再发送下一个请求)。

将 -max-workers 设为很大的数,同时将 -rate=0,可能导致 vegeta 消耗过多资源并崩溃。请谨慎使用。

-redirects

指定每个请求最多跟随的重定向次数。默认为 10。值为 -1 时,不跟随重定向,但会将响应标记为成功。

-resolvers

指定用于名称解析的自定义 DNS 解析器地址,替代操作系统配置的解析器。仅适用于非 Windows 系统。

-root-certs

指定受信任的 TLS 根 CA 证书文件,以逗号分隔的列表形式提供。 如果未指定,则使用系统默认 CA 证书。

-session-tickets

指定是否支持使用会话票据恢复 TLS 会话。

-targets

指定要读取目标的文件,默认从标准输入读取。 有关不同目标格式的信息,请参阅 -format 部分。

-timeout

指定每个请求的超时时间。值为 0 时禁用超时。

-workers

指定攻击中使用的初始工作线程数。如果需要,为了维持请求的速率,实际工作线程数会增加,除非增加后超过 -max-workers。

-max-workers

指定攻击中使用的最大工作线程数。可用于控制攻击的并发级别。

report 命令
Usage: vegeta report [options] [<file>...]

Outputs a report of attack results.

Arguments:
  <file>  A file with vegeta attack results encoded with one of
          the supported encodings (gob | json | csv) [default: stdin]

Options:
  --type    Which report type to generate (text | json | hist[buckets] | hdrplot).
            [default: text]

  --buckets Histogram buckets, e.g.: '[0,1ms,10ms]'

  --every   Write the report to --output at every given interval (e.g 100ms)
            The default of 0 means the report will only be written after
            all results have been processed. [default: 0]

  --output  Output file [default: stdout]

Examples:
  echo "GET http://:80" | vegeta attack -rate=10/s > results.gob
  echo "GET http://:80" | vegeta attack -rate=100/s | vegeta encode > results.json
  vegeta report results.*
report -type=text
Requests      [total, rate, throughput] 1200, 120.00, 65.87
Duration      [total, attack, wait]     10.094965987s, 9.949883921s, 145.082066ms
Latencies     [min, mean, 50, 95, 99, max]  90.438129ms, 113.172398ms, 108.272568ms, 140.18235ms, 247.771566ms, 264.815246ms
Bytes In      [total, mean]             3714690, 3095.57
Bytes Out     [total, mean]             0, 0.00
Success       [ratio]                   55.42%
Status Codes  [code:count]              0:535  200:665
Error Set:
Get http://localhost:6060: dial tcp 127.0.0.1:6060: connection refused
Get http://localhost:6060: read tcp 127.0.0.1:6060: connection reset by peer
Get http://localhost:6060: dial tcp 127.0.0.1:6060: connection reset by peer
Get http://localhost:6060: write tcp 127.0.0.1:6060: broken pipe
Get http://localhost:6060: net/http: transport closed before response was received
Get http://localhost:6060: http: can't write HTTP request on broken connection

Requests 行显示:

  • 发出的请求总数(total)。
  • 在 attack 期间维持的实际请求速率(rate)。
  • 在 total 期间成功请求的吞吐量(throughput)。

Duration 行显示:

  • 发出所有请求所用的 attack 时间(total - wait)
  • 等待最后一个已发出请求响应所用的 wait 时间(total - attack)
  • 攻击总用时(total)(attack + wait)

延迟是读取请求响应所用的时间(包括响应正文中的 -max-body 字节)。

  • min 是一次攻击中所有请求的最小延迟。
  • mean 是一次攻击中所有请求延迟的算术平均值。
  • 50、90、95、99 分别是一次攻击中所有请求延迟的第 50、90、95 和 99 百分位数。若想进一步了解这些指标为何有用,推荐阅读 @tylertreat 的这篇文章。
  • max 是一次攻击中所有请求的最大延迟。

Bytes In 和 Bytes Out 行显示:

  • 随请求或响应正文发送(出站)或接收(入站)的字节总数(total)。
  • 随请求或响应正文发送(出站)或接收(入站)的平均字节数(mean)。

Success 比率显示响应未出错且状态码在 200 和 400 之间(不含 400)的请求所占百分比。

Status Codes 行显示状态码直方图。状态码 0 表示请求发送失败。

Error Set 显示所有已发出请求返回的错误集合(不重复)。其中包括收到非成功响应状态码的请求。

report -type=json

所有时长类字段的单位均为纳秒。

{
  "latencies": {
    "total": 237119463,
    "mean": 2371194,
    "50th": 2854306,
    "90th": 3228223,
    "95th": 3478629,
    "99th": 3530000,
    "max": 3660505,
    "min": 1949582
  },
  "buckets": {
    "0": 9952,
    "1000000": 40,
    "2000000": 6,
    "3000000": 0,
    "4000000": 0,
    "5000000": 2
  },
  "bytes_in": {
    "total": 606700,
    "mean": 6067
  },
  "bytes_out": {
    "total": 0,
    "mean": 0
  },
  "earliest": "2015-09-19T14:45:50.645818631+02:00",
  "latest": "2015-09-19T14:45:51.635818575+02:00",
  "end": "2015-09-19T14:45:51.639325797+02:00",
  "duration": 989999944,
  "wait": 3507222,
  "requests": 100,
  "rate": 101.01010672380401,
  "throughput": 101.00012489812,
  "success": 1,
  "status_codes": {
    "200": 100
  },
  "errors": []
}

在 buckets 字段中,每个键都是一个纳秒值,表示某个桶的下界。 上界由下一个更大的桶隐式确定。 上界不包含在内。 最高的桶是溢出桶,没有上界。 值表示落入相应桶中的请求数。 如果未提供 -buckets 参数,则省略 buckets 字段。

report -type=hist

为给定的桶计算并输出基于文本的直方图。 每个桶的上界均不包含在内。

cat results.bin | vegeta report -type='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%  ###################
report -type=hdrplot

以可通过 https://hdrhistogram.github.io/HdrHistogram/plotFiles.html 绘图的格式输出结果。

Value(ms)  Percentile  TotalCount  1/(1-Percentile)
0.076715   0.000000    0           1.000000
0.439370   0.100000    200         1.111111
0.480836   0.200000    400         1.250000
0.495559   0.300000    599         1.428571
0.505101   0.400000    799         1.666667
0.513059   0.500000    999         2.000000
0.516664   0.550000    1099        2.222222
0.520455   0.600000    1199        2.500000
0.525008   0.650000    1299        2.857143
0.530174   0.700000    1399        3.333333
0.534891   0.750000    1499        4.000000
0.537572   0.775000    1548        4.444444
0.540340   0.800000    1598        5.000000
0.543763   0.825000    1648        5.714286
0.547164   0.850000    1698        6.666667
0.551432   0.875000    1748        8.000000
0.553444   0.887500    1773        8.888889
0.555774   0.900000    1798        10.000000
0.558454   0.912500    1823        11.428571
0.562123   0.925000    1848        13.333333
0.565563   0.937500    1873        16.000000
0.567831   0.943750    1886        17.777778
0.570617   0.950000    1898        20.000000
0.574522   0.956250    1911        22.857143
0.579046   0.962500    1923        26.666667
0.584426   0.968750    1936        32.000000
0.586695   0.971875    1942        35.555556
0.590451   0.975000    1948        40.000000
0.597543   0.978125    1954        45.714286
0.605637   0.981250    1961        53.333333
0.613564   0.984375    1967        64.000000
0.620393   0.985938    1970        71.113640
0.629121   0.987500    1973        80.000000
0.638060   0.989062    1976        91.424392
0.648085   0.990625    1979        106.666667
0.659689   0.992188    1982        128.008193
0.665870   0.992969    1984        142.227279
0.672985   0.993750    1986        160.000000
0.680101   0.994531    1987        182.848784
0.687810   0.995313    1989        213.356091
0.695729   0.996094    1990        256.016385
0.730641   0.996484    1991        284.414107
0.785516   0.996875    1992        320.000000
0.840392   0.997266    1993        365.764448
1.009646   0.997656    1993        426.621160
1.347020   0.998047    1994        512.032770
1.515276   0.998242    1994        568.828214
1.683532   0.998437    1995        639.795266
1.887487   0.998633    1995        731.528895
2.106249   0.998828    1996        853.242321
2.325011   0.999023    1996        1023.541453
2.434952   0.999121    1996        1137.656428
2.544894   0.999219    1996        1280.409731
2.589510   0.999316    1997        1461.988304
2.605192   0.999414    1997        1706.484642
2.620873   0.999512    1997        2049.180328
2.628713   0.999561    1997        2277.904328
2.636394   0.999609    1997        2557.544757
2.644234   0.999658    1997        2923.976608
2.652075   0.999707    1997        3412.969283
2.658916   0.999756    1998        4098.360656
2.658916   0.999780    1998        4545.454545
2.658916   0.999805    1998        5128.205128
2.658916   0.999829    1998        5847.953216
2.658916   0.999854    1998        6849.315068
2.658916   0.999878    1998        8196.721311
2.658916   0.999890    1998        9090.909091
2.658916   0.999902    1998        10204.081633
2.658916   0.999915    1998        11764.705882
2.658916   0.999927    1998        13698.630137
2.658916   0.999939    1998        16393.442623
2.658916   0.999945    1998        18181.818182
2.658916   0.999951    1998        20408.163265
2.658916   0.999957    1998        23255.813953
2.658916   0.999963    1998        27027.027027
2.658916   0.999969    1998        32258.064516
2.658916   0.999973    1998        37037.037037
2.658916   0.999976    1998        41666.666667
2.658916   0.999979    1998        47619.047619
2.658916   0.999982    1998        55555.555556
2.658916   0.999985    1998        66666.666667
2.658916   0.999986    1998        71428.571429
2.658916   0.999988    1998        83333.333333
2.658916   0.999989    1998        90909.090909
2.658916   0.999991    1998        111111.111111
2.658916   0.999992    1998        125000.000000
2.658916   0.999993    1998        142857.142858
2.658916   0.999994    1998        166666.666668
2.658916   0.999995    1998        199999.999999
2.658916   0.999996    1998        250000.000000
2.658916   0.999997    1998        333333.333336
2.658916   0.999998    1998        500000.000013
2.658916   0.999999    1998        999999.999971
2.658916   1.000000    1998        10000000.000000
encode 命令
Usage: vegeta encode [options] [<file>...]

Encodes vegeta attack results from one encoding to another.
The supported encodings are Gob (binary), CSV and JSON.
Each input file may have a different encoding which is detected
automatically.

The CSV encoder doesn't write a header. The columns written by it are:

  1. Unix timestamp in nanoseconds since epoch
  2. HTTP status code
  3. Request latency in nanoseconds
  4. Bytes out
  5. Bytes in
  6. Error
  7. Base64 encoded response body
  8. Attack name
  9. Sequence number of request
  10. Method
  11. URL
  12. Base64 encoded response headers

Arguments:
  <file>  A file with vegeta attack results encoded with one of
          the supported encodings (gob | json | csv) [default: stdin]

Options:
  --to      Output encoding (gob | json | csv) [default: json]
  --output  Output file [default: stdout]

Examples:
  echo "GET http://:80" | vegeta attack -rate=1/s > results.gob
  cat results.gob | vegeta encode | jq -c 'del(.body)' | vegeta encode -to gob
plot 命令

绘图

Usage: vegeta plot [options] [<file>...]

Outputs an HTML time series plot of request latencies over time.
The X axis represents elapsed time in seconds from the beginning
of the earliest attack in all input files. The Y axis represents
request latency in milliseconds.

Click and drag to select a region to zoom into. Double click to zoom out.
Choose a different number on the bottom left corner input field
to change the moving average window size (in data points).

Arguments:
  <file>  A file output by running vegeta attack [default: stdin]

Options:
  --title      Title and header of the resulting HTML page.
               [default: Vegeta Plot]
  --threshold  Threshold of data points to downsample series to.
               Series with less than --threshold number of data
               points are not downsampled. [default: 4000]

Examples:
  echo "GET http://:80" | vegeta attack -name=50qps -rate=50 -duration=5s > results.50qps.bin
  cat results.50qps.bin | vegeta plot > plot.50qps.html
  echo "GET http://:80" | vegeta attack -name=100qps -rate=100 -duration=5s > results.100qps.bin
  vegeta plot results.50qps.bin results.100qps.bin > plot.html

用法:生成目标

除了接受静态目标列表外,Vegeta 还可以与其他程序配合使用,以流式方式生成目标。下面是一个示例,使用 jq 工具在正文中生成递增的 ID。

jq -ncM 'while(true; .+1) | {method: "POST", url: "http://:6060", body: {id: .} | @base64 }' | \
  vegeta attack -rate=50/s -lazy -format=json -duration=30s | \
  tee results.bin | \
  vegeta report

用法:分布式攻击

如果 Vegeta 因打开文件数、内存、CPU 或网络带宽等机器限制而无法进行负载测试,那么使用分布式方式运行 Vegeta 是个不错的选择。

假设目标攻击速率为每秒 60k 个请求,并且我们有 3 台安装了 vegeta 的机器。

请在每台机器上使用 ulimit 命令,将用户的打开文件描述符和进程数限制设置为较高的值。

现在可以开始攻击了。我们只需将预期速率除以机器数量,并在每次攻击中使用这个数值。这里我们将使用 pdsh 进行编排。

$ PDSH_RCMD_TYPE=ssh 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'

上一条命令完成后,我们可以收集结果文件,用于生成报告。

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

report 命令接受多个结果文件。 生成报告前,它会读取这些文件并按时间戳排序。

vegeta report *.bin

在分布式测试中收集结果的另一种方式,是使用内置的 Prometheus 导出器,并配置 Prometheus 服务来获取所有 Vegeta 实例的测试结果。更多详情和完整示例,请参阅 attack 选项“prometheus-addr”和“Prometheus 支持”一节。

用法:实时分析

如果你是 iTerm 的用户,可以通过 jaggr 将 vegeta 与 jplot 集成,在终端中实时绘制 vegeta 报告:

echo 'GET http://localhost:8080' | \
    vegeta attack -rate 5000 -duration 10m | vegeta encode | \
    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

README 配图

用法:库

该库的版本遵循 SemVer v2.0.0。 从 lib/v9.0.0 起,库和 CLI 采用独立的版本号,以便更好地隔离各个组件中的破坏性变更。

如需了解 Git 标签命名方案以及与 go mod 的兼容性详情,请参阅版本控制。

package main

import (
  "fmt"
  "time"

  vegeta "github.com/tsenart/vegeta/v12/lib"
)

func main() {
  rate := vegeta.Rate{Freq: 100, Per: time.Second}
  duration := 4 * time.Second
  targeter := vegeta.NewStaticTargeter(vegeta.Target{
    Method: "GET",
    URL:    "http://localhost:9100/",
  })
  attacker := vegeta.NewAttacker()

  var metrics vegeta.Metrics
  for res := range attacker.Attack(targeter, rate, duration, "Big Bang!") {
    metrics.Add(res)
  }
  metrics.Close()

  fmt.Printf("99th percentile: %s\n", metrics.Latencies.P99)
}
限制

支持的 rate 有一个上限,具体取决于所使用的 机器。 你可能会受 CPU 限制(不太可能)、内存限制(更有可能),或者 达到系统资源限制;应针对进程执行情况调整这些限制。对我们来说,重要的限制是文件描述符 和进程数。在 UNIX 系统上,你可以获取和设置用户当前的 软限制值。

$ ulimit -n # file descriptors
2560
$ ulimit -u # processes / threads
709

只需将新的数值作为参数传入即可更改限制。

Prometheus 支持

Vegeta 内置了 Prometheus 导出器,可在攻击期间启用,以便将任意 Prometheus 实例指向 Vegeta 攻击进程并监控攻击指标。

要在命令行中启用 Prometheus 导出器,请设置“prometheus-addr”标志。

Prometheus HTTP 端点仅在攻击期间可用,并会在攻击结束后立即关闭。

公开的指标如下:

  • request_bytes_in - 按“url”、“method”和“status”统计从目标服务接收的字节数
  • request_bytes_out - 按“url”、“method”和“status”统计发送到目标服务的字节数
  • request_seconds - 按“url”、“method”和“status”统计请求延迟的直方图和计数器
  • request_fail_count - 按“url”、“method”、“status”和“message”统计失败请求数
README 配图

请查看文件 lib/prom/grafana.json,其中包含此 Grafana 示例仪表板的源文件。

局限性
  1. Prometheus 会从正在运行的 vegeta 攻击进程中抓取指标,并在自己的服务器上为样本分配时间戳。这意味着结果时间戳并不准确(也就是说,记录的是抓取时间,而非结果时间)。
  2. 需要在带外配置 Prometheus 来抓取 vegeta。真麻烦!
  3. 由于 vegeta 攻击进程与 Prometheus 服务器之间没有协调,攻击进程会在 Prometheus 来得及抓取最新观测结果之前结束。

为什么不使用 pushgateway?请参阅此评论。

有一个问题正在跟踪解决所有这些局限性的正确方案,也就是远程写入集成。

许可证

请参阅 LICENSE。

捐赠

如果你使用并喜爱 Vegeta,请考虑向 1MDmKC51ve7Upxt75KoNM6x1qdXHFK6iW2 捐赠一些 Satoshi。如果你希望以赞助者身份出现在名单中,请告诉我!

捐赠比特币

评论

支持 Markdown