.Net Crank性能测试入门

开发 后端
Crank 是微软新出的一个性能测试框架,集成了多种基准测试工具,如bombardier、wrk等。Crank通过统一的配置,可以转换成不同基准测试工具命令进行测试。可参考Bombardier Job实现。

[[437057]]

Crank 是微软新出的一个性能测试框架,集成了多种基准测试工具,如bombardier、wrk等。

Crank通过统一的配置,可以转换成不同基准测试工具命令进行测试。可参考Bombardier Job实现。

安装Crank

运行如下两个命令分别安装Crank的cli(Controller)和Agent。

  1. dotnet tool update Microsoft.Crank.Controller --version "0.2.0-*" --global 
  2.  
  3. dotnet tool update Microsoft.Crank.Agent--version "0.2.0-*" --global 

需要.Net SDK 5.0环境

安装完成后执行命令crank,会打印出如下可以配置的参数和介绍,或者github查看相关参数介绍。

  1. PS C:\Users\Stack\Desktop> crank 
  2. Crank Benchmarks Controller 
  3.  
  4. The Crank controller orchestrates benchmark jobs on Crank agents. 
  5.  
  6. Usage: Crank [command] [options] 
  7.  
  8. Options: 
  9.   -?|-h|--help         Show help information 
  10.   -c|--config          Configuration file or url 
  11.   -s|--scenario        Scenario to execute 
  12.   -j|--job             Name of job to define 
  13.   --profile            Profile name 
  14.   --script             Execute a named script available in the configuration files. Can be used multiple times. 
  15.   -j|--json            Saves the results as json in the specified file. 
  16.   --csv                Saves the results as csv in the specified file. 
  17.   --compare            An optional filename to compare the results to. Can be used multiple times. 
  18.   --variable           Variable 
  19.   --sql                Connection string of the SQL Server Database to store results in 
  20.   --table              Table name of the SQL Database to store results in 
  21.   --session            A logical identifier to group related jobs. 
  22.   --description        A string describing the job. 
  23.   -p|--property        Some custom key/value that will be added to the results, .e.g. --property arch=arm --property 
  24.                        os=linux 

执行crank-agent,启动基准测试所需的代理服务。github查看相关参数介绍。

  1. PS C:\Users\Stack\Desktop> crank-agent 
  2. Hosting environment: Production 
  3. Content root path: C:\Users\Stack\.dotnet\tools\.store\microsoft.crank.agent\0.2.0-alpha.21567.1\microsoft.crank.agent\0.2.0-alpha.21567.1\tools\net5.0\any
  4. Now listening on: http://[::]:5010 

创建Crank配置文件

配置文件参考官方hello.benchmarks.yml示例

示例文件中引入了bombardier.yml,由于大环境对githubusercontent.com域名不太友好,可以考虑将bombardier.yml下载到本地,imports引入本地路径或者直接将文件内容加入到新建的配置文件。

  1. imports: 
  2.   -https://raw.githubusercontent.com/dotnet/crank/main/src/Microsoft.Crank.Jobs.Bombardier/bombardier.yml 

生产中使用推荐imports文件的方式,crank配置文件中做少量配置就可以完成基准测试,并且可以引入不同的Microsoft.Crank.Jobs.XXX/XXX.yml,基于不同的基准测试工具进行测试。

其他测试工具配置文件都在https://github.com/dotnet/crank/blob/main/src/Microsoft.Crank.XXX下。

  1. variables: 
  2.   headers: 
  3.     none: '' 
  4.     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"' 
  5.     html: '--header "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" --header "Connection: keep-alive"' 
  6.     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"' 
  7.     connectionclose: '--header "Connection: close"' 
  8.   presetHeaders: none 
  9.  
  10. jobs: 
  11.   bombardier: 
  12.     source: 
  13.       repository: https://github.com/dotnet/crank.git 
  14.       branchOrCommit: main 
  15.       project: src/Microsoft.Crank.Jobs.Bombardier/Microsoft.Crank.Jobs.Bombardier.csproj 
  16.       sourceKey: bombardier 
  17.       noBuild: true 
  18.     readyStateText: Bombardier Client 
  19.     waitForExit: true 
  20.     variables: 
  21.       connections: 256 #设置连接数 
  22.       warmup: 15 #设置测试预热次数 
  23.       duration: 15 #设置测试时间 
  24.       requests: 0 #设置测试请求实例数 
  25.       rate: 0 #设置每秒请求频率 
  26.       transport: fasthttp # | http1 | http2  设置使用golang的fasthttp库发送http请求 
  27.       serverScheme: http 
  28.       serverAddress: localhost 
  29.       serverPort: 5000 
  30.       path:  
  31.       bodyFile: # path or url for a file to use as the body content 
  32.       verb: # GET when nothing is specified 
  33.       customHeaders: [ ] # list of headers with the format: '<name1>: <value1>', e.g. [ 'content-type: application/json' ] 
  34.     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 %}" 
  35.     onConfigure:  
  36.       # - job.timeout = Number(job.variables.duration) + Number(job.variables.warmup) + 10; 
  37.  
  38.   server: 
  39.     source: #指定需要测试的项目,本文直接使用本地路径 
  40.       localFolder: . 
  41.       project: crank_demo.csproj 
  42.     readyStateText: Application started. 
  43.     # source: 指定测试项目的远程仓库地址,并通过branchOrCommit指定分支 
  44.     #   repository: https://github.com/dotnet/crank 
  45.     #   branchOrCommit: main 
  46.     #   project: samples/hello/hello.csproj 
  47.     # readyStateText: Application started. 
  48.  
  49. scenarios: #配置基准测试场景 
  50.   crank_demo: #定义方案名,执行crank命令时指定该名称 
  51.     application:  
  52.       job: server # 指定测试项目为上面定义的server 
  53.     load
  54.       job: bombardier # 指定测试工具bombardier 
  55.       variables: 
  56.         serverPort: 5000 #配置http服务端口 
  57.         path: / #配置http服务地址 
  58.  
  59. profiles: 
  60.   local
  61.     variables: 
  62.       serverAddress: localhost 
  63.     jobs:  
  64.       application: 
  65.         endpoints:  
  66.           - http://localhost:5010 
  67.       load
  68.         endpoints:  
  69.           - http://localhost:5010 

启动Crank-Agent

启动agent后执行crank,会有一个如下安装sdk的动作

  1. [09:29:05.261] Runtime: 6.0.0 (Current
  2. [09:29:05.262] SDK: 6.0.100 (Current
  3. [09:29:05.263] ASP.NET: 6.0.0 (Current
  4. [09:29:05.265] Creating custom global.json 
  5. [09:29:05.266] Desktop: 6.0.0 (Current
  6. [09:29:05.266] Installing SDK '6.0.100' ... 

所以启动agent时多指定一个dotnethome参数,避免重复安装sdk。

  1. crank-agent --dotnethome 'C:\Program Files\dotnet' 

启动Crank

crank --config .\demo.benchmarks.yml --scenario crank_demo --profile local

--scenario crank_demo: 指定定义的测试场景

--profile local :设置输出结果到本地,即控制台输出

可以通过参数指定结果输出到本地json文件(--output results.json )或者数据库(--sql [connection-string] --table [table-name])

结果输出

这里省略压测期间的日志输出,直接列出结果。

  1. | application           |               | 
  2. --------------------- | ------------- | 
  3. | CPU Usage (%)         | 56            | 
  4. | Cores usage (%)       | 447           | 
  5. | Working Set (MB)      | 140           | 
  6. | Private Memory (MB)   | 157           | 
  7. | Build Time (ms)       | 7,232         | 
  8. | Start Time (ms)       | 501           | 
  9. | Published Size (KB)   | 91,292        | 
  10. | .NET Core SDK Version | 6.0.100       | 
  11. | ASP.NET Core Version  | 6.0.0+ae1a6cb | 
  12. | .NET Runtime Version  | 6.0.0+4822e3c | 
  13.  
  14.  
  15. load                  |                |  
  16. --------------------- | -------------- | 
  17. | CPU Usage (%)         | 46             |  
  18. | Cores usage (%)       | 370            |  
  19. | Working Set (MB)      | 29             | 
  20. | Private Memory (MB)   | 30             | 
  21. | Build Time (ms)       | 11,891         | 
  22. | Start Time (ms)       | 226            | 
  23. | Published Size (KB)   | 68,228         | 
  24. | .NET Core SDK Version | 3.1.415        | 
  25. | ASP.NET Core Version  | 3.1.21+458d974 | 
  26. | .NET Runtime Version  | 3.1.21+df8abc0 | 
  27. First Request (ms)    | 185            | 
  28. | Requests              | 912,005        | 
  29. | Bad responses         | 0              | 
  30. | Mean latency (us)     | 4,207          | 
  31. Max latency (us)      | 138,999        | 
  32. | Requests/sec          | 60,305         | 
  33. | Requests/sec (max)    | 128,523        | 

更多

crank readme: https://github.com/dotnet/crank/blob/main/docs/README.md

Benchmarks crank为Benchmarks重构版本

 

本文示例代码: https://github.com/MayueCif/crank_demo

 

责任编辑:武晓燕 来源: 分布式应用运行时
相关推荐

2022-11-24 09:01:26

HTTPHertz架构

2011-07-19 09:35:46

.Net

2021-05-07 07:27:51

Python测试工具

2009-12-30 11:03:26

ADO.Net性能

2010-07-13 09:10:26

.NETMonoJava

2023-08-31 08:36:52

.NET性能测试开源

2009-11-12 09:25:21

ADO.NET连接池

2011-03-15 16:34:36

Iptables性能

2009-09-18 13:05:59

.NET CLR

2020-05-18 07:00:00

性能测试压力测试负载测试

2011-06-08 16:59:04

性能测试载测试压力测试

2010-12-31 13:30:12

2011-12-15 09:55:47

javanio

2009-09-22 17:41:07

Hibernate性能

2019-08-21 10:53:29

.NET性能优化

2009-09-16 09:37:47

ASP.NET和PHP

2014-07-28 09:52:14

PythonPython性能

2009-11-16 11:28:07

Oracle性能测试

2010-04-12 10:54:13

Oracle性能测试

2013-05-08 09:31:32

MangoDB
点赞
收藏

51CTO技术栈公众号