其他分享
首页 > 其他分享> > 3. 堪比JMeter的.Net压测工具 - Crank 进阶篇 - 认识bombardier

3. 堪比JMeter的.Net压测工具 - Crank 进阶篇 - 认识bombardier

作者:互联网

1. 前言

通过之前的学习,我们已经了解了各参数以及配置的意义,接下来的文章我们分别从bombardier以及wrk入手,进一步了解彼此之间的联系

2. 认识 bombardier

bombardier 是一个 HTTP(S) 基准测试工具。它是用 Go 编程语言编写的,并使用优秀的fasthttp代替 Go 的默认 http 库,因为它具有闪电般的快速性能,详细文档查看

其支持参数:

-c, --connections=125       Maximum number of concurrent connections
-t, --timeout=2s            Socket/request timeout
-l, --latencies             Print latency statistics
-m, --method=GET            Request method
-b, --body=""               Request body
-f, --body-file=""          File to use as request body
-s, --stream                Specify whether to stream body using chunked
                            transfer encoding or to serve it from memory
    --cert=""               Path to the client's TLS Certificate
    --key=""                Path to the client's TLS Certificate Private Key
-k, --insecure              Controls whether a client verifies the server's
                            certificate chain and host name
-H, --header="K: V" ...     HTTP headers to use(can be repeated)
-n, --requests=[pos. int.]  Number of requests
-d, --duration=10s          Duration of test
-r, --rate=[pos. int.]      Rate limit in requests per second
    --fasthttp              Use fasthttp client
    --http1                 Use net/http client with forced HTTP/1.x
    --http2                 Use net/http client with enabled HTTP/2.0
-p, --print=<spec>          Specifies what to output. Comma-separated list of
                            values 'intro' (short: 'i'), 'progress' (short:
                            'p'), 'result' (short: 'r'). Examples:

                              * i,p,r (prints everything)
                              * intro,result (intro & result)
                              * r (result only)
                              * result (same as above)
-q, --no-print              Don't output anything
-o, --format=<spec>         Which format to use to output the result. <spec>
                            is either a name (or its shorthand) of some format
                            understood by bombardier or a path to the
                            user-defined template, which uses Go's
                            text/template syntax, prefixed with 'path:' string
                            (without single quotes), i.e.
                            "path:/some/path/to/your.template" or
                            "path:C:\some\path\to\your.template" in case of
                            Windows. Formats understood by bombardier are:

                              * plain-text (short: pt)
                              * json (short: j)

并且bombardier支持多平台,可以在Windows、Linux、OSX系统上运行,那接下来我们使用bombardier测试一下百度的压测情况

安装(WSL-Ubuntu):

sudo apt install wget
sudo wget https://github.com/codesenberg/bombardier/releases/download/v1.2.5/bombardier-linux-arm64

运行:

./bombardier-linux-arm64  -c 200 -d 1s --insecure -l https://www.baidu.com --print r --format json

asciicast

其中:

3. 了解Microsoft.Crank.Jobs.Bombardier

在Microsoft.Crank.Jobs.Bombardier项目中Program.cs

  1. 根据参数获取-w、-d、-n、-f参数信息
  2. 校验压测时长、请求数等参数信息
  3. 判断当前运行环境是Windows、Linux、OSX,根据环境下载对应的bombardier,并根据传递的
  4. 根据yml参数最后拼装bombardier的原始命令:

bombardier -c 200 -d 1s --insecure -l https://www.baidu.com --print r --format json

  1. 将输出的结果使用追加到stringBuilder上,再赋值给output
  2. 通过JObject.Parse解析指标,最后通过BenchmarksEventSource存储并输出到控制台或数据库、csv、json中

其中

BenchmarksEventSource.Register("bombardier/requests;http/requests", Operations.Max, Operations.Sum, "Requests", "Total number of requests", "n0");
BenchmarksEventSource.Register("bombardier/badresponses;http/requests/badresponses", Operations.Max, Operations.Sum, "Bad responses", "Non-2xx or 3xx responses", "n0");

BenchmarksEventSource.Register("bombardier/latency/mean;http/latency/mean", Operations.Max, Operations.Avg, "Mean latency (us)", "Mean latency (us)", "n0");
BenchmarksEventSource.Register("bombardier/latency/max;http/latency/max", Operations.Max, Operations.Max, "Max latency (us)", "Max latency (us)", "n0");

BenchmarksEventSource.Register("bombardier/rps/mean;http/rps/mean", Operations.Max, Operations.Sum, "Requests/sec", "Requests per second", "n0");
BenchmarksEventSource.Register("bombardier/rps/max;http/rps/max", Operations.Max, Operations.Sum, "Requests/sec (max)", "Max requests per second", "n0");
BenchmarksEventSource.Register("bombardier/throughput;http/throughput", Operations.Max, Operations.Sum, "Read throughput (MB/s)", "Read throughput (MB/s)", "n2");

BenchmarksEventSource.Register("bombardier/raw", Operations.All, Operations.All, "Raw results", "Raw results", "json");

var total =
    document["result"]["req1xx"].Value<long>()
    + document["result"]["req2xx"].Value<long>()
    + document["result"]["req3xx"].Value<long>()
    + document["result"]["req3xx"].Value<long>()
    + document["result"]["req4xx"].Value<long>()
    + document["result"]["req5xx"].Value<long>()
    + document["result"]["others"].Value<long>();

var success = document["result"]["req2xx"].Value<long>() + document["result"]["req3xx"].Value<long>();

BenchmarksEventSource.Measure("bombardier/requests;http/requests", total);
BenchmarksEventSource.Measure("bombardier/badresponses;http/requests/badresponses", total - success);

BenchmarksEventSource.Measure("bombardier/latency/mean;http/latency/mean", document["result"]["latency"]["mean"].Value<double>());
BenchmarksEventSource.Measure("bombardier/latency/max;http/latency/max", document["result"]["latency"]["max"].Value<double>());

BenchmarksEventSource.Measure("bombardier/rps/max;http/rps/max", document["result"]["rps"]["max"].Value<double>());
BenchmarksEventSource.Measure("bombardier/rps/mean;http/rps/mean", document["result"]["rps"]["mean"].Value<double>());

BenchmarksEventSource.Measure("bombardier/raw", output);

var bytesPerSecond = document["result"]["bytesRead"].Value<long>() / document["result"]["timeTakenSeconds"].Value<double>();

// B/s to MB/s
BenchmarksEventSource.Measure("bombardier/throughput", bytesPerSecond / 1024 / 1024);

4. 解读bombardier.yml各参数作用