From 0cf03db2510b1b9ed301df2c73c838667dda47c4 Mon Sep 17 00:00:00 2001 From: bicijinlian Date: Tue, 4 Apr 2023 00:03:37 +0800 Subject: [PATCH] =?UTF-8?q?add:=20=E6=B7=BB=E5=8A=A0=E9=A1=B9=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...BenchMarkDotnetStudy.BenchmarkStudy.csproj | 18 ++ .../CounterTest.cs | 301 ++++++++++++++++++ .../CounterTest2.cs | Bin 0 -> 10422 bytes .../CounterTest3.cs | Bin 0 -> 3070 bytes .../Program.cs | 18 ++ .../BenchMarkDotnetStudy.CApp.csproj | 18 ++ BenchMarkDotnetStudy.CApp/Program.cs | 10 + .../BenchMarkDotnetStudy.Core.csproj | 8 + BenchMarkDotnetStudy.Core/Counter.cs | 47 +++ BenchMarkDotnetStudy.sln | 37 +++ 10 files changed, 457 insertions(+) create mode 100644 BenchMarkDotnetStudy.BenchmarkStudy/BenchMarkDotnetStudy.BenchmarkStudy.csproj create mode 100644 BenchMarkDotnetStudy.BenchmarkStudy/CounterTest.cs create mode 100644 BenchMarkDotnetStudy.BenchmarkStudy/CounterTest2.cs create mode 100644 BenchMarkDotnetStudy.BenchmarkStudy/CounterTest3.cs create mode 100644 BenchMarkDotnetStudy.BenchmarkStudy/Program.cs create mode 100644 BenchMarkDotnetStudy.CApp/BenchMarkDotnetStudy.CApp.csproj create mode 100644 BenchMarkDotnetStudy.CApp/Program.cs create mode 100644 BenchMarkDotnetStudy.Core/BenchMarkDotnetStudy.Core.csproj create mode 100644 BenchMarkDotnetStudy.Core/Counter.cs create mode 100644 BenchMarkDotnetStudy.sln diff --git a/BenchMarkDotnetStudy.BenchmarkStudy/BenchMarkDotnetStudy.BenchmarkStudy.csproj b/BenchMarkDotnetStudy.BenchmarkStudy/BenchMarkDotnetStudy.BenchmarkStudy.csproj new file mode 100644 index 0000000..6115072 --- /dev/null +++ b/BenchMarkDotnetStudy.BenchmarkStudy/BenchMarkDotnetStudy.BenchmarkStudy.csproj @@ -0,0 +1,18 @@ + + + + Exe + net7.0 + enable + enable + + + + + + + + + + + diff --git a/BenchMarkDotnetStudy.BenchmarkStudy/CounterTest.cs b/BenchMarkDotnetStudy.BenchmarkStudy/CounterTest.cs new file mode 100644 index 0000000..8795fa9 --- /dev/null +++ b/BenchMarkDotnetStudy.BenchmarkStudy/CounterTest.cs @@ -0,0 +1,301 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using BenchmarkDotNet; +using BenchmarkDotNet.Attributes; + +using BenchMarkDotnetStudy.Core; + +namespace BenchMarkDotnetStudy.BenchmarkStudy +{ + /// + /// Counter 基准测试 + /// + public class CounterTest + { + [Benchmark] + public void Thread1_Test() + { + Counter counter = new Counter(); + counter.Increment(); + } + + [Benchmark] + public void Thread2_Test() + { + Counter counter = new Counter(); + + List threads = new List() + { + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + }; + + + threads.ForEach(t => t.Start()); + threads.ForEach(t => t.Join()); + } + + [Benchmark] + public void Thread4_Test() + { + Counter counter = new Counter(); + + List threads = new List() + { + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + }; + + + threads.ForEach(t => t.Start()); + threads.ForEach(t => t.Join()); + } + + [Benchmark] + public void Thread8_Test() + { + Counter counter = new Counter(); + + List threads = new List() + { + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + + }; + + + threads.ForEach(t => t.Start()); + threads.ForEach(t => t.Join()); + } + + [Benchmark] + public void Thread16_Test() + { + Counter counter = new Counter(); + + List threads = new List() + { + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + }; + + + threads.ForEach(t => t.Start()); + threads.ForEach(t => t.Join()); + } + + [Benchmark] + public void Thread32_Test() + { + Counter counter = new Counter(); + + List threads = new List() + { + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + new Thread(() => counter.Increment() ), + }; + + + threads.ForEach(t => t.Start()); + threads.ForEach(t => t.Join()); + } + + [Benchmark] + public void Thread1_Test2() + { + Counter counter = new Counter(); + + counter.IncrementWithInterlocked(); + } + + [Benchmark] + public void Thread2_Test2() + { + Counter counter = new Counter(); + + List threads = new List() + { + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + }; + + + threads.ForEach(t => t.Start()); + threads.ForEach(t => t.Join()); + } + + [Benchmark] + public void Thread4_Test2() + { + Counter counter = new Counter(); + + List threads = new List() + { + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + + }; + + + threads.ForEach(t => t.Start()); + threads.ForEach(t => t.Join()); + } + + [Benchmark] + public void Thread8_Test2() + { + Counter counter = new Counter(); + + List threads = new List() + { + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + + }; + + + threads.ForEach(t => t.Start()); + threads.ForEach(t => t.Join()); + } + + [Benchmark] + public void Thread16_Test2() + { + Counter counter = new Counter(); + + List threads = new List() + { + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + }; + + + threads.ForEach(t => t.Start()); + threads.ForEach(t => t.Join()); + } + + [Benchmark] + public void Thread32_Test2() + { + Counter counter = new Counter(); + + List threads = new List() + { + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + new Thread(() => counter.IncrementWithInterlocked() ), + }; + + + threads.ForEach(t => t.Start()); + threads.ForEach(t => t.Join()); + } + } +} diff --git a/BenchMarkDotnetStudy.BenchmarkStudy/CounterTest2.cs b/BenchMarkDotnetStudy.BenchmarkStudy/CounterTest2.cs new file mode 100644 index 0000000000000000000000000000000000000000..ff37711b9819a592c91cd48f40eddf9085f21ca2 GIT binary patch literal 10422 zcmeHM-D=c86#lLSU%;y%UbG-7)Jto@fBXe;VJ}3AO+$Av8|!XXEoFTHpU4;R1xjC~ z7hYJuGvhJIbT*;5dLf69ot@e9pYsiwZ_j?rF~b-o_L1Nv4mqokV}hHwj7wPdYl^#g zhk{p*j2dIkO6sO~$d!`!lxtb9m8Z`4`(C*LEq=0LVdq#)Jd~{ z-O^LZw)YRrYs}}7u%qfOyFs5jT=D+bupYG(k3{{170ZGO-l9+GiI3;<7Jqe5sJ(C$ zlu32AT9I$lTGhxzs@n9lOlw(=>a{ypmC}5gwOoVPmMA2)b`?@QVhu8fZs~jJFE720 z>(QF4CmqeBRnjCgXaexEZd@+-oA2>0X9lzOICTj6*y+m9V@P`cBqTJDNm z*Z|?ugkQ@U+mLaM8Ced`mgTf`4Oj6sKPiqs@9dxESI!2MmJEu37x5?hqhM7yvAFz# zb~VN@>xDaI$0GlmjLN2~Tg+=W&tRO2XF)H@sSI7OjU3zJ8N7nMl93h7$$kBM#>L{P zS;f3~(C+J0#Jq3wsZAFggc S{69F+5EGtn)Kx$8pFaVNU!qq4 literal 0 HcmV?d00001 diff --git a/BenchMarkDotnetStudy.BenchmarkStudy/CounterTest3.cs b/BenchMarkDotnetStudy.BenchmarkStudy/CounterTest3.cs new file mode 100644 index 0000000000000000000000000000000000000000..c9c8fb988402592d71a96537c745e0710626ab43 GIT binary patch literal 3070 zcmeHJ%Wl&^6g_JsexNH0u}CH4qIQ8qOQk{q@dy$$QWjNJC63it*d`=Sp{l|U@J0QB z{s8K)uwX$s_fDpMWMZol?E)dob?muw&$;J5rkB5`m>@!mBY4=u3Ewh=NU({!xPxV@ zKAzzdVqPHv?uhu7a<7k<{F3q>^IOp9<*j0WZnW#r<5%J|vFcz*w z7$0g@{dqk+!b7a$pv3!ZDfcXf?WV+gwk$K4z|WY2RZXr+lAiL{*lH-O;pX zL`C{kP4t&i1O*?5XtwjjQRg>WmY;NulF2v})SaWdnxx%Da@I5oFO_IZ1VvqDZ> z)nFNw)%Wp2JL$Pj#UZ21uC&NFNoCH-`C#IX=x;sOF_*b2RV!z% zoWmjcb;&IvyAbA74e9MZ?%^!Fh<|)LJo*`~Ty|(JQN)%0HI;W-`6_3p+kQsBmS;8c znO1{~{Y`fMx@zY_t?!5by>I?ox^C}xwbq6@70h`ZvsMBxzli_dh`8Vc< MyC3aW)1Bt;A1r3Sf&c&j literal 0 HcmV?d00001 diff --git a/BenchMarkDotnetStudy.BenchmarkStudy/Program.cs b/BenchMarkDotnetStudy.BenchmarkStudy/Program.cs new file mode 100644 index 0000000..5c63bf6 --- /dev/null +++ b/BenchMarkDotnetStudy.BenchmarkStudy/Program.cs @@ -0,0 +1,18 @@ +using BenchmarkDotNet.Running; + +namespace BenchMarkDotnetStudy.BenchmarkStudy +{ + internal class Program + { + static void Main(string[] args) + { + Console.WriteLine("Benchmark 测试!"); + + var summary = BenchmarkRunner.Run(); + + Console.WriteLine("Benchmark 测试结束:"); + + Console.WriteLine($"总耗时:{summary.AllRuntimes}"); + } + } +} \ No newline at end of file diff --git a/BenchMarkDotnetStudy.CApp/BenchMarkDotnetStudy.CApp.csproj b/BenchMarkDotnetStudy.CApp/BenchMarkDotnetStudy.CApp.csproj new file mode 100644 index 0000000..0dc0fc7 --- /dev/null +++ b/BenchMarkDotnetStudy.CApp/BenchMarkDotnetStudy.CApp.csproj @@ -0,0 +1,18 @@ + + + + Exe + net7.0 + enable + enable + + + + + + + + + + + diff --git a/BenchMarkDotnetStudy.CApp/Program.cs b/BenchMarkDotnetStudy.CApp/Program.cs new file mode 100644 index 0000000..93ef4c4 --- /dev/null +++ b/BenchMarkDotnetStudy.CApp/Program.cs @@ -0,0 +1,10 @@ +namespace BenchMarkDotnetStudy.CApp +{ + internal class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello, World!"); + } + } +} \ No newline at end of file diff --git a/BenchMarkDotnetStudy.Core/BenchMarkDotnetStudy.Core.csproj b/BenchMarkDotnetStudy.Core/BenchMarkDotnetStudy.Core.csproj new file mode 100644 index 0000000..8ef8970 --- /dev/null +++ b/BenchMarkDotnetStudy.Core/BenchMarkDotnetStudy.Core.csproj @@ -0,0 +1,8 @@ + + + + netstandard2.1 + enable + + + diff --git a/BenchMarkDotnetStudy.Core/Counter.cs b/BenchMarkDotnetStudy.Core/Counter.cs new file mode 100644 index 0000000..40d4d90 --- /dev/null +++ b/BenchMarkDotnetStudy.Core/Counter.cs @@ -0,0 +1,47 @@ +using System; +using System.Threading; + +namespace BenchMarkDotnetStudy.Core +{ + /// + /// 计数器 + /// + public class Counter + { + /// + /// 总次数 + /// + public static int TotalCounter = 0; + + /// + /// 每方法执行次数 + /// + public static readonly int LoopNumber = 1000; + + /// + /// 累加方法 + /// + public int Increment() + { + for (int i = 1; i <= LoopNumber; i++) + { + ++TotalCounter; + } + + return TotalCounter; + } + + /// + /// 累加方法 + /// + public int IncrementWithInterlocked() + { + for (int i = 1; i <= LoopNumber; i++) + { + Interlocked.Increment(ref TotalCounter); + } + + return TotalCounter; + } + } +} diff --git a/BenchMarkDotnetStudy.sln b/BenchMarkDotnetStudy.sln new file mode 100644 index 0000000..2c9bb37 --- /dev/null +++ b/BenchMarkDotnetStudy.sln @@ -0,0 +1,37 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.33516.290 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BenchMarkDotnetStudy.Core", "BenchMarkDotnetStudy.Core\BenchMarkDotnetStudy.Core.csproj", "{8C37BE95-8908-4038-BF56-F0F1A5F4C52C}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BenchMarkDotnetStudy.CApp", "BenchMarkDotnetStudy.CApp\BenchMarkDotnetStudy.CApp.csproj", "{D5D6E7DF-82AF-4F78-BB60-A0BE378B97E4}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BenchMarkDotnetStudy.BenchmarkStudy", "BenchMarkDotnetStudy.BenchmarkStudy\BenchMarkDotnetStudy.BenchmarkStudy.csproj", "{AFA892FB-C52A-443D-B0F0-3E458DDCDC7F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {8C37BE95-8908-4038-BF56-F0F1A5F4C52C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8C37BE95-8908-4038-BF56-F0F1A5F4C52C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8C37BE95-8908-4038-BF56-F0F1A5F4C52C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8C37BE95-8908-4038-BF56-F0F1A5F4C52C}.Release|Any CPU.Build.0 = Release|Any CPU + {D5D6E7DF-82AF-4F78-BB60-A0BE378B97E4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D5D6E7DF-82AF-4F78-BB60-A0BE378B97E4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D5D6E7DF-82AF-4F78-BB60-A0BE378B97E4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D5D6E7DF-82AF-4F78-BB60-A0BE378B97E4}.Release|Any CPU.Build.0 = Release|Any CPU + {AFA892FB-C52A-443D-B0F0-3E458DDCDC7F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AFA892FB-C52A-443D-B0F0-3E458DDCDC7F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AFA892FB-C52A-443D-B0F0-3E458DDCDC7F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AFA892FB-C52A-443D-B0F0-3E458DDCDC7F}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {0AF1E897-2AFD-4DEC-8D48-D24CDF486479} + EndGlobalSection +EndGlobal