{ "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": [ "/// \n", "/// 被测试类:计数器\n", "/// \n", "public class Counter\n", "{\n", " /// \n", " /// 总次数\n", " /// \n", " public static int TotalCounter = 0;\n", "\n", " /// \n", " /// 每方法执行次数\n", " /// \n", " public static readonly int LoopNumber = 100;\n", "\n", " /// \n", " /// 累加方法\n", " /// \n", " public int Increment()\n", " {\n", " for (int i = 1; i <= LoopNumber; i++)\n", " {\n", " ++TotalCounter;\n", " }\n", "\n", " return TotalCounter;\n", " }\n", "\n", " /// \n", " /// 累加方法\n", " /// \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", "/// \n", "/// 基准测试类:CounterBenchmark\n", "/// \n", "public class CounterBenchmark\n", "{\n", " [Benchmark]\n", " public void NonThreadSafe_Test()\n", " {\n", " Counter counter = new Counter();\n", "\n", " List threads = new List()\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 threads = new List()\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(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()\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 }