Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions BenchmarkMultithreading/BenchmarkMultithreading.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{9F098A77-241D-4E01-9B3D-29FE857E1511}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>BenchmarkMultithreading</RootNamespace>
<AssemblyName>BenchmarkMultithreading</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="BenchmarkDotNet, Version=0.9.7.0, Culture=neutral, PublicKeyToken=aa0ca2f9092cefc4, processorArchitecture=MSIL">
<HintPath>..\packages\BenchmarkDotNet.0.9.7\lib\net40\BenchmarkDotNet.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Management" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="BenchmarkContainer.cs" />
<Compile Include="Benchmarks\ComparsionCollectionList.cs" />
<Compile Include="Benchmarks\ComparsionCollectionConcurrent.cs" />
<Compile Include="IBenchmarkable.cs" />
<Compile Include="Benchmarks\Lock.cs" />
<Compile Include="Benchmarks\MonitorEnter.cs" />
<Compile Include="IComparsion.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TestDomainObjects\Client.cs" />
<Compile Include="TestDomainObjects\Credit.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
Original file line number Diff line number Diff line change
@@ -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<string> safeSource = new ConcurrentBag<string>();
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()
{
}
}
}
Original file line number Diff line number Diff line change
@@ -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<string> unsafeSource = new List<string>();
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()
{
}
}
}
23 changes: 23 additions & 0 deletions BenchmarkMultithreading/BenchmarkMultithreading/Benchmarks/Lock.cs
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace BenchmarkMultithreading
{
internal interface IBenchmarkable
{
void Run();
void Setup();
}
}
11 changes: 11 additions & 0 deletions BenchmarkMultithreading/BenchmarkMultithreading/IComparsion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace BenchmarkMultithreading
{
internal interface IComparsion
{
void fillSource();
void changeSource();
void threadsInit();
}
}
Loading