目錄
- 堪比JMeter的.Net壓測工具 - Crank 入門篇
- 堪比JMeter的.Net壓測工具 - Crank 進階篇 - 認識yml
- 堪比JMeter的.Net壓測工具 - Crank 進階篇 - 認識bombardier
- 堪比JMeter的.Net壓測工具 - Crank 進階篇 - 認識wrk、wrk2
- 堪比JMeter的.Net壓測工具 - Crank 實戰篇 - 接口以及場景壓測
- 堪比JMeter的.Net壓測工具 - Crank 實戰篇 - 收集診斷跟蹤信息與如何分析瓶頸
- 堪比JMeter的.Net壓測工具 - Crank 總結篇 - crank帶來了什么
1. 前言
入門篇我們已經成功運行hello.benchmarks.yml並輸出測試結果,本篇我們就hello.benchmarks.yml、以及運行的shell腳本詳細解讀下其中的含義
2. 剖析hello.benchmarks.yml
2.1. job
- 在hello.benchmarks.yml中我們定義了一個新的job: server,並指定了倉庫信息 ( 遠程倉庫 ):
repository:https://github.com/doddgu/crank.git # 倉庫源
branchOrCommit:sample # 分支
project: samples/hello/hello.csproj # 項目
- 並通過import導入了bombardier.yml,其中定義了另外一個job: bombardier,並指定了倉庫信息 ( 遠程倉庫 ):
repository: https://github.com/doddgu/crank.git
branchOrCommit: sample
project: src/Microsoft.Crank.Jobs.Bombardier/Microsoft.Crank.Jobs.Bombardier.csproj
sourceKey: bombardier # 服務器使用它來重用相同的源文件夾。
noBuild: true # 定義 SourceKey時,指示是否仍應進行構建
2.2. scenarios
- 在hello.benchmarks.yml中我們定義了場景: hello,並為此場景指定了兩個任務:
- 第一個任務名為application,使用的是項目server
- 第二個任務名為load,並指定使用項目bombardier
2.3. variables 變量
-
在bombardier.yml中
- 定義了全局變量: headers、presetHeaders(預設header)
- 定義了局部變量: connections、warmup、duration、requests、rate、transport、serverScheme等等
-
在hello.benchmarks.yml中為load定義了局部變量serverPort、path
2.4. profiles 配置
- 在hello.benchmarks.yml中我們
- 定義了配置local
- 並指定了局部變量: serverAddress = localhost
- 為任務application、load指定了部署作業的endpoint 是 http://localhost:5010 (指定執行任務的Agent地址)
2.5. arguments 參數
- 在bombardier.yml中與variables同級的配置: arguments,此參數是在啟動job后傳遞的參數,其中定義的全局參數、局部參數信息也都是為構建完整的參數做准備,bombardier真實的參數信息是:
-c {{connections}} -w {{warmup}} -d {{duration}} -n {{requests}} --insecure -l {% if rate != 0 %} --rate {{ rate }} {% endif %} {% if transport %} --{{ transport}} {% endif %} {{headers[presetHeaders]}} {% for h in customHeaders %}{% assign s = h | split : ':' %}--header \"{{ s[0] }}: {{ s[1] | strip }}\" {% endfor %} {% if serverUri == blank or serverUri == empty %} {{serverScheme}}://{{serverAddress}}:{{serverPort}}{{path}} {% else %} {{serverUri}}:{{serverPort}}{{path}} {% endif %} {% if bodyFile != blank and bodyFile != empty %} -f {{bodyFile}} {% endif %} {% if verb != blank and verb != empty %} -m {{verb}} {% endif %}
3. 改造hello.benchmarks.yml
改造hello.benchmarks.yml,不考慮重用,最原始的代碼如下
variables:
headers:
none: ''
plaintext: '--header "Accept: text/plain,text/html;q=0.9,application/xhtml+xml;q=0.9,application/xml;q=0.8,*/*;q=0.7" --header "Connection: keep-alive"'
html: '--header "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" --header "Connection: keep-alive"'
json: '--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"'
connectionclose: '--header "Connection: close"'
presetHeaders: none
jobs:
bombardier:
source:
repository: https://github.com/dotnet/crank.git
branchOrCommit: main
project: src/Microsoft.Crank.Jobs.Bombardier/Microsoft.Crank.Jobs.Bombardier.csproj
sourceKey: bombardier
noBuild: true
readyStateText: Bombardier Client
waitForExit: true
variables:
connections: 256
warmup: 15
duration: 15
requests: 0
rate: 0
transport: fasthttp # | http1 | http2
serverScheme: http
serverAddress: localhost
serverPort: 5000
path:
bodyFile: # path or url for a file to use as the body content
verb: # GET when nothing is specified
customHeaders: [ ] # list of headers with the format: '<name1>: <value1>', e.g. [ 'content-type: application/json' ]
arguments: "-c {{connections}} -w {{warmup}} -d {{duration}} -n {{requests}} --insecure -l {% if rate != 0 %} --rate {{ rate }} {% endif %} {% if transport %} --{{ transport}} {% endif %} {{headers[presetHeaders]}} {% for h in customHeaders %}{% assign s = h | split : ':' %}--header \"{{ s[0] }}: {{ s[1] | strip }}\" {% endfor %} {% if serverUri == blank or serverUri == empty %} {{serverScheme}}://{{serverAddress}}:{{serverPort}}{{path}} {% else %} {{serverUri}}:{{serverPort}}{{path}} {% endif %} {% if bodyFile != blank and bodyFile != empty %} -f {{bodyFile}} {% endif %} {% if verb != blank and verb != empty %} -m {{verb}} {% endif %}"
onConfigure:
# - job.timeout = Number(job.variables.duration) + Number(job.variables.warmup) + 10;
endpoints:
- http://localhost:5010
server:
source:
repository: https://github.com/doddgu/crank
branchOrCommit: sample
project: samples/hello/hello.csproj
readyStateText: Application started.
endpoints:
- http://localhost:5010
scenarios:
hello:
application:
job: server
load:
job: bombardier
variables:
serverPort: 5000
path: /
4. 解讀crank shell
之前我們通過shell執行:
-
crank --config hello.benchmarks.yml --scenario hello --profile local --load.framework net5.0 --application.framework net5.0
- 其中crank 是固定的、代表:Crank Controller
- --config:固定的配置,指執行哪個yml配置,每次僅能指定一個yml配置
- --scenario:固定的配置,設置場景是hello
- --profile:固定的配置,非必選,可多次設置,指定當前Crank命令申請的配置是local,使用local下配置的所有信息
- --load.framework: 格式:<任務名>.framework,為任務load指定framework的運行框架版本是net5.0、--application.framework同理
-
改造hello.benchmarks.yml,因為移除了profile,所以執行: crank --config hello.yml --scenario hello --load.framework net5.0 --application.framework net5.0 即可
5. 疑問
- 為什么啟動crank時要增加--application.framework net5.0?
- 安裝crank要求必須有net5.0的環境,所以指定net5.0不需要再單獨安裝框架環境
- 為什么啟動crank時不指定framework時默認是netcore3.1呢?
- 使用記事本打開hello.csproj、Microsoft.Crank.Jobs.Bombardier.csproj 即可了解
- scenarios節點下application以及load兩個節點名稱可以更換嗎?只能有兩個節點?
- 節點名稱不固定,可以更換,也沒有限制必須是兩個節點,具體多少個節點根據自己的需要來
6. 結尾
通過上面的學習,我們也已經完全的了解了各配置的作用,但對bombardier.yml與開源項目bombardier存在什么樣的聯系還不清楚,以及為什么叫做bombardier.yml而不是其他名字,並且是如何實現性能指標的輸出,接下來就會講到bombardier.yml與開源項目bombardier的關系,以及wrk.yml與wrk的關系
源碼地址:https://github.com/doddgu/crank/tree/sample
開源地址
MASA.BuildingBlocks:https://github.com/masastack/MASA.BuildingBlocks
MASA.Contrib:https://github.com/masastack/MASA.Contrib
MASA.Utils:https://github.com/masastack/MASA.Utils
MASA.EShop:https://github.com/masalabs/MASA.EShop
MASA.Blazor:https://github.com/BlazorComponent/MASA.Blazor
如果你對我們的 MASA Framework 感興趣,無論是代碼貢獻、使用、提 Issue,歡迎聯系我們