当前位置:首页 > 通信资讯 > 正文

.net crank性能测试入门(.net crank性能测试入门)

.net crank性能测试入门(.net crank性能测试入门)

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

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

安装Crank

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

  1. dotnettoolupdateMicrosoft.Crank.Controller--version"0.2.0-*"--global
  2. dotnettoolupdateMicrosoft.Crank.Agent--version"0.2.0-*"--global

需要.Net SDK 5.0环境

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

  1. PSC:\Users\Stack\Desktop>crank
  2. CrankBenchmarksController
  3. TheCrankcontrollerorchestratesbenchmarkjobsonCrankagents.
  4. Usage:Crank[command][options]
  5. Options:
  6. -?|-h|--helpShowhelpinformation
  7. -c|--configConfigurationfileorurl
  8. -s|--scenarioScenariotoexecute
  9. -j|--jobNameofjobtodefine
  10. --profileProfilename
  11. --scriptExecuteanamedscriptavailableintheconfigurationfiles.Canbeusedmultipletimes.
  12. -j|--jsonSavestheresultsasjsoninthespecifiedfile.
  13. --csvSavestheresultsascsvinthespecifiedfile.
  14. --compareAnoptionalfilenametocomparetheresultsto.Canbeusedmultipletimes.
  15. --variableVariable
  16. --sqlConnectionstringoftheSQLServerDatabasetostoreresultsin
  17. --tableTablenameoftheSQLDatabasetostoreresultsin
  18. --sessionAlogicalidentifiertogrouprelatedjobs.
  19. --descriptionAstringdescribingthejob.
  20. -p|--propertySomecustomkey/valuethatwillbeaddedtotheresults,.e.g.--propertyarch=arm--property
  21. os=linux

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

  1. PSC:\Users\Stack\Desktop>crank-agent
  2. Hostingenvironment:Production
  3. Contentrootpath: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. Nowlisteningon: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. jobs:
  10. bombardier:
  11. source:
  12. repository:https://github.com/dotnet/crank.git
  13. branchOrCommit:main
  14. project:src/Microsoft.Crank.Jobs.Bombardier/Microsoft.Crank.Jobs.Bombardier.csproj
  15. sourceKey:bombardier
  16. noBuild:true
  17. readyStateText:BombardierClient
  18. waitForExit:true
  19. variables:
  20. connections:256#设置连接数
  21. warmup:15#设置测试预热次数
  22. duration:15#设置测试时间
  23. requests:0#设置测试请求实例数
  24. rate:0#设置每秒请求频率
  25. transport:fasthttp#|http1|http2设置使用golang的fasthttp库发送http请求
  26. serverScheme:http
  27. serverAddress:localhost
  28. serverPort:5000
  29. path:
  30. bodyFile:#pathorurlforafiletouseasthebodycontent
  31. verb:#GETwhennothingisspecified
  32. customHeaders:[]#listofheaderswiththeformat:'<name1>:<value1>',e.g.['content-type:application/json']
  33. arguments:"-c{{connections}}-w{{warmup}}-d{{duration}}-n{{requests}}--insecure-l{%ifrate!=0%}--rate{{rate}}{%endif%}{%iftransport%}--{{transport}}{%endif%}{{headers[presetHeaders]}}{%forhincustomHeaders%}{%assigns=h|split:':'%}--header\"{{s[0]}}:{{s[1]|strip}}\"{%endfor%}{%ifserverUri==blankorserverUri==empty%}{{serverScheme}}://{{serverAddress}}:{{serverPort}}{{path}}{%else%}{{serverUri}}:{{serverPort}}{{path}}{%endif%}{%ifbodyFile!=blankandbodyFile!=empty%}-f{{bodyFile}}{%endif%}{%ifverb!=blankandverb!=empty%}-m{{verb}}{%endif%}"
  34. onConfigure:
  35. #-job.timeout=Number(job.variables.duration)+Number(job.variables.warmup)+10;
  36. server:
  37. source:#指定需要测试的项目,本文直接使用本地路径
  38. localFolder:.
  39. project:crank_demo.csproj
  40. readyStateText:Applicationstarted.
  41. #source:指定测试项目的远程仓库地址,并通过branchOrCommit指定分支
  42. #repository:https://github.com/dotnet/crank
  43. #branchOrCommit:main
  44. #project:samples/hello/hello.csproj
  45. #readyStateText:Applicationstarted.
  46. scenarios:#配置基准测试场景
  47. crank_demo:#定义方案名,执行crank命令时指定该名称
  48. application:
  49. job:server#指定测试项目为上面定义的server
  50. load:
  51. job:bombardier#指定测试工具bombardier
  52. variables:
  53. serverPort:5000#配置http服务端口
  54. path:/#配置http服务地址
  55. profiles:
  56. local:
  57. variables:
  58. serverAddress:localhost
  59. jobs:
  60. application:
  61. endpoints:
  62. -http://localhost:5010
  63. load:
  64. endpoints:
  65. -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]Creatingcustomglobal.json
  5. [09:29:05.266]Desktop:6.0.0(Current)
  6. [09:29:05.266]InstallingSDK'6.0.100'...

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

  1. crank-agent--dotnethome'C:\ProgramFiles\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. |CPUUsage(%)|56|
  4. |Coresusage(%)|447|
  5. |WorkingSet(MB)|140|
  6. |PrivateMemory(MB)|157|
  7. |BuildTime(ms)|7,232|
  8. |StartTime(ms)|501|
  9. |PublishedSize(KB)|91,292|
  10. |.NETCoreSDKVersion|6.0.100|
  11. |ASP.NETCoreVersion|6.0.0+ae1a6cb|
  12. |.NETRuntimeVersion|6.0.0+4822e3c|
  13. |load||
  14. |---------------------|--------------|
  15. |CPUUsage(%)|46|
  16. |Coresusage(%)|370|
  17. |WorkingSet(MB)|29|
  18. |PrivateMemory(MB)|30|
  19. |BuildTime(ms)|11,891|
  20. |StartTime(ms)|226|
  21. |PublishedSize(KB)|68,228|
  22. |.NETCoreSDKVersion|3.1.415|
  23. |ASP.NETCoreVersion|3.1.21+458d974|
  24. |.NETRuntimeVersion|3.1.21+df8abc0|
  25. |FirstRequest(ms)|185|
  26. |Requests|912,005|
  27. |Badresponses|0|
  28. |Meanlatency(us)|4,207|
  29. |Maxlatency(us)|138,999|
  30. |Requests/sec|60,305|
  31. |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

原文链接:https://mp.weixin.qq.com/s/gkgTvC2q3ovGh7o2TpO6NA

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。