diff --git a/BenchmarkMultithreading/BenchmarkMultithreading.sln b/BenchmarkMultithreading/BenchmarkMultithreading.sln index c9c674f..2d0e79e 100644 --- a/BenchmarkMultithreading/BenchmarkMultithreading.sln +++ b/BenchmarkMultithreading/BenchmarkMultithreading.sln @@ -4,6 +4,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00 VisualStudioVersion = 14.0.25123.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BenchmarkMultithreadingConsole", "BenchmarkMultithreadingConsole\BenchmarkMultithreadingConsole.csproj", "{2706D524-D2D2-411B-86C3-07EBAAF97E93}" + ProjectSection(ProjectDependencies) = postProject + {9F098A77-241D-4E01-9B3D-29FE857E1511} = {9F098A77-241D-4E01-9B3D-29FE857E1511} + EndProjectSection +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BenchmarkMultithreading", "BenchmarkMultithreading\BenchmarkMultithreading.csproj", "{9F098A77-241D-4E01-9B3D-29FE857E1511}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -15,6 +20,10 @@ Global {2706D524-D2D2-411B-86C3-07EBAAF97E93}.Debug|Any CPU.Build.0 = Debug|Any CPU {2706D524-D2D2-411B-86C3-07EBAAF97E93}.Release|Any CPU.ActiveCfg = Release|Any CPU {2706D524-D2D2-411B-86C3-07EBAAF97E93}.Release|Any CPU.Build.0 = Release|Any CPU + {9F098A77-241D-4E01-9B3D-29FE857E1511}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9F098A77-241D-4E01-9B3D-29FE857E1511}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9F098A77-241D-4E01-9B3D-29FE857E1511}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9F098A77-241D-4E01-9B3D-29FE857E1511}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/BenchmarkMultithreading/BenchmarkMultithreading/BenchmarkContainer.cs b/BenchmarkMultithreading/BenchmarkMultithreading/BenchmarkContainer.cs new file mode 100644 index 0000000..c816da3 --- /dev/null +++ b/BenchmarkMultithreading/BenchmarkMultithreading/BenchmarkContainer.cs @@ -0,0 +1,44 @@ +using BenchmarkDotNet.Attributes; +using BenchmarkMultithreading.Benchmarks; + +namespace BenchmarkMultithreading +{ + public class BenchmarkContainer + { + #region [.private fields] + private ComparsionCollectionList testList; + private ComparsionCollectionConcurrent testConcurrent; + #endregion + + #region public methods + [Setup] + public void Setup() + { + testList = new ComparsionCollectionList(); + testConcurrent = new ComparsionCollectionConcurrent(); + } + + [Benchmark] + public void Lock() + { + + } + + [Benchmark] + public void MonitorEnter() + { + + } + [Benchmark] + public void ComparsionCollectionList() + { + testList.Run(); + } + [Benchmark] + public void ComparsionCollectionConcurrent() + { + testConcurrent.Run(); + } + #endregion + } +} diff --git a/BenchmarkMultithreading/BenchmarkMultithreading/BenchmarkMultithreading.csproj b/BenchmarkMultithreading/BenchmarkMultithreading/BenchmarkMultithreading.csproj new file mode 100644 index 0000000..9231a4d --- /dev/null +++ b/BenchmarkMultithreading/BenchmarkMultithreading/BenchmarkMultithreading.csproj @@ -0,0 +1,70 @@ + + + + + Debug + AnyCPU + {9F098A77-241D-4E01-9B3D-29FE857E1511} + Library + Properties + BenchmarkMultithreading + BenchmarkMultithreading + v4.5.2 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\BenchmarkDotNet.0.9.7\lib\net40\BenchmarkDotNet.dll + True + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/BenchmarkMultithreading/BenchmarkMultithreading/Benchmarks/ComparsionCollectionConcurrent.cs b/BenchmarkMultithreading/BenchmarkMultithreading/Benchmarks/ComparsionCollectionConcurrent.cs new file mode 100644 index 0000000..4211b4c --- /dev/null +++ b/BenchmarkMultithreading/BenchmarkMultithreading/Benchmarks/ComparsionCollectionConcurrent.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Concurrent; +using System.Threading; + +namespace BenchmarkMultithreading.Benchmarks +{ + class ComparsionCollectionConcurrent : IBenchmarkable, IComparsion + { + #region [.inits] + const int NEW_ROWS_NUMBER = 5000; + const int THREADS_NUMBER = 10; + const int INIT_ROWS_NUMBER = 10000; + + private ConcurrentBag safeSource = new ConcurrentBag(); + private ulong changesSumConcurrent = 0; + + Thread[] threadsConcurrent = new Thread[THREADS_NUMBER]; + #endregion + + public void changeSource() + { + for (int i = 0; i < NEW_ROWS_NUMBER; i++) + { + safeSource.Add("new value"); + changesSumConcurrent++; + } + } + + public void threadsInit() + { + for (int i = 0; i < THREADS_NUMBER; i++) + { + ThreadStart t2s = new ThreadStart(changeSource); + Thread t2 = new Thread(t2s); + threadsConcurrent[i] = t2; + } + } + + public void fillSource() + { + for (int i = 0; i < INIT_ROWS_NUMBER; i++) + { + var str = "default" + i; + safeSource.Add(str); + } + } + + public void Run() + { + threadsInit(); + fillSource(); + //Console.WriteLine("Number of elements at collection: {0}", safeSource.Count); + + //start threads + for (int i = 0; i < THREADS_NUMBER; i++) + { + threadsConcurrent[i].Start(); + } + } + + public void Setup() + { + } + } +} \ No newline at end of file diff --git a/BenchmarkMultithreading/BenchmarkMultithreading/Benchmarks/ComparsionCollectionList.cs b/BenchmarkMultithreading/BenchmarkMultithreading/Benchmarks/ComparsionCollectionList.cs new file mode 100644 index 0000000..f395fd1 --- /dev/null +++ b/BenchmarkMultithreading/BenchmarkMultithreading/Benchmarks/ComparsionCollectionList.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections.Generic; +using System.Threading; + +namespace BenchmarkMultithreading.Benchmarks +{ + class ComparsionCollectionList : IBenchmarkable, IComparsion + { + + #region [.inits] + const int NEW_ROWS_NUMBER = 5000; + const int THREADS_NUMBER = 10; + const int INIT_ROWS_NUMBER = 10000; + + private List unsafeSource = new List(); + private object SyncRoot = new object(); + private ulong changesSum = 0; + + Thread[] threadsLock = new Thread[THREADS_NUMBER]; + #endregion + + public void changeSource() + { + for (int i = 0; i < NEW_ROWS_NUMBER; i++) + { + lock (SyncRoot) + { + unsafeSource.Add("new value"); + changesSum++; + } + } + } + + public void threadsInit() + { + for (int i = 0; i < THREADS_NUMBER; i++) + { + ThreadStart t1s = new ThreadStart(changeSource); + Thread t1 = new Thread(t1s); + threadsLock[i] = t1; + } + } + + public void fillSource() + { + for (int i = 0; i < INIT_ROWS_NUMBER; i++) + { + var str = "default" + i; + unsafeSource.Add(str); + } + } + + public void Run() + { + threadsInit(); + fillSource(); + //Console.WriteLine("Number of elements at collection: {0}", unsafeSource.Count); + + //start threads + for (int i = 0; i < THREADS_NUMBER; i++) + { + threadsLock[i].Start(); + } + } + + public void Setup() + { + } + } +} diff --git a/BenchmarkMultithreading/BenchmarkMultithreading/Benchmarks/Lock.cs b/BenchmarkMultithreading/BenchmarkMultithreading/Benchmarks/Lock.cs new file mode 100644 index 0000000..0634659 --- /dev/null +++ b/BenchmarkMultithreading/BenchmarkMultithreading/Benchmarks/Lock.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BenchmarkMultithreading.Benchmarks +{ + class Lock : IBenchmarkable + { + #region IBenchmarkable + public void Run() + { + throw new NotImplementedException(); + } + + public void Setup() + { + throw new NotImplementedException(); + } + #endregion + } +} diff --git a/BenchmarkMultithreading/BenchmarkMultithreading/Benchmarks/MonitorEnter.cs b/BenchmarkMultithreading/BenchmarkMultithreading/Benchmarks/MonitorEnter.cs new file mode 100644 index 0000000..9792313 --- /dev/null +++ b/BenchmarkMultithreading/BenchmarkMultithreading/Benchmarks/MonitorEnter.cs @@ -0,0 +1,58 @@ +using BenchmarkMultithreading.TestDomainObjects; +using System.Threading; + +namespace BenchmarkMultithreading.Benchmarks +{ + class MonitorEnter : IBenchmarkable + { + #region private fields + private Credit credits; + + public int ThreadCount { get { return 4; } } + + public int WritesCount { get { return 10; } } + #endregion + + #region IBenchmarkable + public void Run() + { + for (int i = 0; i < ThreadCount; i++) + { + Thread thread = new Thread( + new ThreadStart(() => + { + for (int j = 0; j < WritesCount; j++) + { + + } + Monitor.Enter(credits); + try + { + Client client = + new Client() + { + Name = "Kozlov Ivan Petrovich" + }; + credits.clients.Add(client); + } + finally + { + Monitor.Exit(credits); + } + })); + thread.Start(); + } + } + + public void Setup() + { + credits = new Credit() + { + Security = 810, + Amount = 1000000 + }; + } + #endregion + + } +} diff --git a/BenchmarkMultithreading/BenchmarkMultithreading/IBenchmarkable.cs b/BenchmarkMultithreading/BenchmarkMultithreading/IBenchmarkable.cs new file mode 100644 index 0000000..efdfa01 --- /dev/null +++ b/BenchmarkMultithreading/BenchmarkMultithreading/IBenchmarkable.cs @@ -0,0 +1,8 @@ +namespace BenchmarkMultithreading +{ + internal interface IBenchmarkable + { + void Run(); + void Setup(); + } +} \ No newline at end of file diff --git a/BenchmarkMultithreading/BenchmarkMultithreading/IComparsion.cs b/BenchmarkMultithreading/BenchmarkMultithreading/IComparsion.cs new file mode 100644 index 0000000..4a1e146 --- /dev/null +++ b/BenchmarkMultithreading/BenchmarkMultithreading/IComparsion.cs @@ -0,0 +1,11 @@ +using System; + +namespace BenchmarkMultithreading +{ + internal interface IComparsion + { + void fillSource(); + void changeSource(); + void threadsInit(); + } +} diff --git a/BenchmarkMultithreading/BenchmarkMultithreading/Properties/AssemblyInfo.cs b/BenchmarkMultithreading/BenchmarkMultithreading/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..1378edf --- /dev/null +++ b/BenchmarkMultithreading/BenchmarkMultithreading/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("BenchmarkMultithreading")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("BenchmarkMultithreading")] +[assembly: AssemblyCopyright("Copyright © 2016")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("9f098a77-241d-4e01-9b3d-29fe857e1511")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/BenchmarkMultithreading/BenchmarkMultithreading/TestDomainObjects/Client.cs b/BenchmarkMultithreading/BenchmarkMultithreading/TestDomainObjects/Client.cs new file mode 100644 index 0000000..be3ea62 --- /dev/null +++ b/BenchmarkMultithreading/BenchmarkMultithreading/TestDomainObjects/Client.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BenchmarkMultithreading.TestDomainObjects +{ + internal class Client + { + public string Name { get; set; } + } +} diff --git a/BenchmarkMultithreading/BenchmarkMultithreading/TestDomainObjects/Credit.cs b/BenchmarkMultithreading/BenchmarkMultithreading/TestDomainObjects/Credit.cs new file mode 100644 index 0000000..78d045a --- /dev/null +++ b/BenchmarkMultithreading/BenchmarkMultithreading/TestDomainObjects/Credit.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BenchmarkMultithreading.TestDomainObjects +{ + internal class Credit + { + public int Amount { get; set; } + public short Security { get; set; } + public List clients { get; set; } = new List(); + } +} diff --git a/BenchmarkMultithreading/BenchmarkMultithreading/packages.config b/BenchmarkMultithreading/BenchmarkMultithreading/packages.config new file mode 100644 index 0000000..b237384 --- /dev/null +++ b/BenchmarkMultithreading/BenchmarkMultithreading/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/BenchmarkMultithreading/BenchmarkMultithreadingConsole/BenchmarkMultithreadingConsole.csproj b/BenchmarkMultithreading/BenchmarkMultithreadingConsole/BenchmarkMultithreadingConsole.csproj index d08bb06..e803b3c 100644 --- a/BenchmarkMultithreading/BenchmarkMultithreadingConsole/BenchmarkMultithreadingConsole.csproj +++ b/BenchmarkMultithreading/BenchmarkMultithreadingConsole/BenchmarkMultithreadingConsole.csproj @@ -33,8 +33,14 @@ 4 + + ..\packages\BenchmarkDotNet.0.9.7\lib\net40\BenchmarkDotNet.dll + True + False + + @@ -48,6 +54,13 @@ + + + + + {9f098a77-241d-4e01-9b3d-29fe857e1511} + BenchmarkMultithreading +