关于c#:3-堪比JMeter的Net压测工具-Crank-进阶篇-认识bombardier

42次阅读

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

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

其中:

  • req1xx 代表 http 响应码为 1 **
  • req2xx 代表 http 响应码为 2 **
  • req3xx 代表 http 响应码为 3 **
  • req4xx 代表 http 响应码为 4 **
  • req5xx 代表 http 响应码为 5 **
  • result.rps.mean 代表每秒申请数
  • result.rps.max 代表每秒最大申请数
  • result.latency.mean 代表每毫秒提早
  • result.latency.max 代表每毫秒最大提早

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

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

其中

  • 申请总数 = req1xx + req2xx + req3xx + req4xx + req5xx + others
  • 胜利申请数 = req2xx + req3xx
  • 失败申请数 = 申请总数 – 胜利申请数
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 各参数作用

  • connections: 最大并发连接数,默认: 256
  • warmup: 预热工夫,默认 15s,与执行 duration 相似,而并非压测次数

    • 当 warmup > 0 时,会先预热 warmup 秒后再执行一次压测,第二次的压测才是最初返回的后果
    • 当 warmup = 0 时,不进行预热,间接开始压测
  • duration: 测试时长,单位: s
  • requests: 申请数
  • rate: 每秒申请数限度
  • transport: 传输方式。默认: fasthttp、反对 fasthttp、http1、http2 三种
  • presetHeaders: 预设 header,依据全局参数 headers,自选其一即可,抉择 json,那申请的 header 即为: –header “Accept: application/json,text/html;q=0.9,application/xhtml+xml;q=0.9,application/xml;q=0.8,/;q=0.7″ –header “Connection: keep-alive”
  • customHeaders: 自定义 headers,如果预设 headers 中没有须要的 header,则通过重写 customHeaders,以实现自定义 header 的目标
  • serverUri: 自定义 url,如果此参数存在,则申请地址为: {serverUri}:{serverPort}{path}
  • serverPort: 服务端口
  • serverScheme: 服务的 Scheme,默认 http、反对 http、https 两种
  • serverAddress: 服务地址、不蕴含 http、例如: www.baidu.com,如果 serverUri 存在,此配置有效,如果不存在,申请格局为: {serverScheme}://{serverAddress}:{serverPort}{path}
  • path: 服务接口地址,不蕴含域,例如: /api/check/healthy
  • bodyFile: body 内容,仅在非 Get 申请时应用,反对近程门路与本地绝对路径(Agent 服务的相对地址,非 Controller 端的相对地址)
  • verb: 申请形式: 默认 GET、反对 POST、PUT、DELETE、PATCH、GET

5. 总结

劣势:

  • 跨平台
  • 用法简略
  • 应用 go 语言开发、性能高

劣势:

  • 不反对动静参数
  • 不反对多个接口同时压测

bombardier.yml 的存在是为 Microsoft.Crank.Jobs.Bombardier 提供配置参数,Microsoft.Crank.Jobs.Bombardier 通过调用开源我的项目 bombardier 实现压测,并将压测后果通过 BenchmarksEventSource 存储并输入到控制台或数据库、csv、json 中

源码地址:https://github.com/doddgu/cra…

开源地址

MASA.BuildingBlocks:https://github.com/masastack/…

MASA.Contrib:https://github.com/masastack/…

MASA.Utils:https://github.com/masastack/…

MASA.EShop:https://github.com/masalabs/M…

MASA.Blazor:https://github.com/BlazorComp…

如果你对咱们的 MASA Framework 感兴趣,无论是代码奉献、应用、提 Issue,欢送分割咱们

正文完
 0