You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
BenchMarkDotnetStudy/Docs/在笔记中使用基准测试.ipynb

239 lines
6.4 KiB
Plaintext

{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"在多语言笔记中使用 BenchmarkDotnet 基准测试\n",
"========================================="
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## 全局准备工作,就先执行此单元\n",
"+ 设置包源和引用 Nuget 包\n",
"+ 引入命名空间\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"#i \"nuget:https://api.nuget.org/v3/index.json\"\n",
"#r \"nuget:BenchmarkDotNet,*-*\"\n",
"\n",
"using System;\n",
"using System.Collections.Generic;\n",
"using System.Linq;\n",
"using System.Text;\n",
"using System.Threading;\n",
"using System.Threading.Channels;\n",
"using System.Threading.Tasks;\n",
"\n",
"using BenchmarkDotNet;\n",
"using BenchmarkDotNet.Attributes;\n",
"using BenchmarkDotNet.Running;\n",
"using BenchmarkDotNet.Jobs;\n",
"using BenchmarkDotNet.Diagnosers;\n",
"using BenchmarkDotNet.Configs;"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## 方法一:在笔记本内部,使用基准测试"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"/// <summary>\n",
"/// 被测试类:计数器\n",
"/// </summary>\n",
"public class Counter\n",
"{\n",
" /// <summary>\n",
" /// 总次数\n",
" /// </summary>\n",
" public static int TotalCounter = 0;\n",
"\n",
" /// <summary>\n",
" /// 每方法执行次数\n",
" /// </summary>\n",
" public static readonly int LoopNumber = 100;\n",
"\n",
" /// <summary>\n",
" /// 累加方法\n",
" /// </summary>\n",
" public int Increment()\n",
" {\n",
" for (int i = 1; i <= LoopNumber; i++)\n",
" {\n",
" ++TotalCounter;\n",
" }\n",
"\n",
" return TotalCounter;\n",
" }\n",
"\n",
" /// <summary>\n",
" /// 累加方法\n",
" /// </summary>\n",
" public int IncrementWithInterlocked()\n",
" {\n",
" for (int i = 1; i <= LoopNumber; i++)\n",
" {\n",
" Interlocked.Increment(ref TotalCounter);\n",
" }\n",
"\n",
" return TotalCounter;\n",
" }\n",
"}\n",
"\n",
"/// <summary>\n",
"/// 基准测试类CounterBenchmark\n",
"/// </summary>\n",
"public class CounterBenchmark\n",
"{\n",
" [Benchmark]\n",
" public void NonThreadSafe_Test()\n",
" {\n",
" Counter counter = new Counter();\n",
"\n",
" List<Thread> threads = new List<Thread>()\n",
" {\n",
" new Thread(() => counter.Increment() ),\n",
" new Thread(() => counter.Increment() ),\n",
" };\n",
"\n",
"\n",
" threads.ForEach(t => t.Start());\n",
" threads.ForEach(t => t.Join());\n",
"\n",
" //Console.WriteLine($\"方法结束时TotalCounter = {Counter.TotalCounter}\");\n",
" }\n",
"\n",
" [Benchmark]\n",
" public void ThreadSafe_Test()\n",
" {\n",
" Counter counter = new Counter();\n",
"\n",
" List<Thread> threads = new List<Thread>()\n",
" {\n",
" new Thread(() => counter.IncrementWithInterlocked() ),\n",
" new Thread(() => counter.IncrementWithInterlocked() ),\n",
" };\n",
"\n",
" threads.ForEach(t => t.Start());\n",
" threads.ForEach(t => t.Join());\n",
" }\n",
"}\n",
"\n",
"//执行基准测试\n",
"{\n",
" //设置基准测试参数:注意参数名与参数值是两个参数而不是一个\n",
" var benchmarkRunArgs = new string[] {\"--filter\",\"*CounterBenchmark.ThreadSafe_Test\"};\n",
"\n",
" //出错方法:此种方式执行出错,暂时没有找到解决方法\n",
" //var summary = BenchmarkRunner.Run<CounterBenchmark>(null, benchmarkRunArgs);\n",
" \n",
" //可行方法:注意参数配置方法\n",
" BenchmarkSwitcher.FromAssembly(typeof(CounterBenchmark).Assembly).Run(benchmarkRunArgs, new DebugInProcessConfig());\n",
"}\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## 方法二Powershell 直接执行实有的其测试项目"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"需要根据项目本身的运行方法来定:关键是Notebook中不能弹出输入框输入指定的数据如果不弹窗输入都是可以的\n",
"+ BenchmarkRunner.Run<CounterBenchmark>()\n",
"+ BenchmarkSwitcher.FromAssembly(Assembly).Run() 这种命令行后面要有过滤参数,否则不能弹窗输入的情况下,不能运行。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "pwsh"
},
"polyglot_notebook": {
"kernelName": "pwsh"
}
},
"outputs": [],
"source": [
"# 注意参数 -- 后面的参数传递给应用程序即应用程序Main方法的args参数而不是 dotnet run 命令本身\n",
"dotnet run -c Release --project \"..\\BenchMarkDotnetStudy.BenchmarkStudy\\BenchMarkDotnetStudy.BenchmarkStudy.csproj\" -- --filter *CounterTest3.Thread2_Test"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"当前程序内核程序集信息"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".NET (C#)",
"language": "C#",
"name": ".net-csharp"
},
"language_info": {
"name": "polyglot-notebook"
},
"orig_nbformat": 4,
"polyglot_notebook": {
"kernelInfo": {
"defaultKernelName": "csharp",
"items": [
{
"aliases": [],
"name": "csharp"
}
]
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}