Skip to content

Commit

Permalink
Added benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
usercode committed Feb 4, 2023
1 parent 1d5ab2a commit 6d90d8f
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/AsyncKeyLock.Benchmarks/AsyncKeyLock.Benchmarks.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<OutputType>Exe</OutputType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AsyncKeyedLock" Version="6.1.1" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.4" />
<PackageReference Include="NeoSmart.AsyncLock" Version="3.2.1" />
<PackageReference Include="Nito.AsyncEx" Version="5.1.2" />
<PackageReference Include="SixLabors.ImageSharp.Web" Version="2.0.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\AsyncKeyLock\AsyncKeyLock.csproj" />
</ItemGroup>

</Project>
59 changes: 59 additions & 0 deletions src/AsyncKeyLock.Benchmarks/BenchmarkSimpleKeyLock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;

namespace AsyncKeyLock.Benchmarks;

[MemoryDiagnoser]
public class BenchmarkSimpleKeyLock
{
[GlobalSetup]
public void GlobalSetup()
{
_AsyncKeyLock = new AsyncLock<string>();
_AsyncKeyedLock = new AsyncKeyedLock.AsyncKeyedLocker<string>();
_ImageSharpWebLock = new SixLabors.ImageSharp.Web.Synchronization.AsyncKeyLock<string>();
}

private AsyncLock<string> _AsyncKeyLock;
private AsyncKeyedLock.AsyncKeyedLocker<string> _AsyncKeyedLock;
private SixLabors.ImageSharp.Web.Synchronization.AsyncKeyLock<string> _ImageSharpWebLock;

[Params(1_00, 1_000, 10_000)]
public int NumberOfLocks;

[Benchmark(Baseline = true)]
public async Task AsyncKeyLock()
{
for (int i = 0; i < NumberOfLocks; i++)
{
using (await _AsyncKeyLock.WriterLockAsync(string.Empty))
{

}
}
}

[Benchmark]
public async Task AsyncKeyedLock()
{
for (int i = 0; i < NumberOfLocks; i++)
{
using (await _AsyncKeyedLock.LockAsync(string.Empty))
{

}
}
}

[Benchmark]
public async Task ImageSharpWeb()
{
for (int i = 0; i < NumberOfLocks; i++)
{
using (await _ImageSharpWebLock.LockAsync(string.Empty))
{

}
}
}
}
58 changes: 58 additions & 0 deletions src/AsyncKeyLock.Benchmarks/BenchmarkSimpleWriterLock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using BenchmarkDotNet.Attributes;

namespace AsyncKeyLock.Benchmarks;

[MemoryDiagnoser]
public class BenchmarkSimpleWriterLock
{
[GlobalSetup]
public void GlobalSetup()
{
_AsyncKeyLock = new AsyncLock();
_NeoSmartLock = new NeoSmart.AsyncLock.AsyncLock();
_NitoLock = new Nito.AsyncEx.AsyncLock();
}

private AsyncLock _AsyncKeyLock;
private NeoSmart.AsyncLock.AsyncLock _NeoSmartLock;
private Nito.AsyncEx.AsyncLock _NitoLock;

[Params(1_00, 1_000, 10_000)]
public int NumberOfLocks;

[Benchmark(Baseline = true)]
public async Task AsyncKeyLock()
{
for (int i = 0; i < NumberOfLocks; i++)
{
using (await _AsyncKeyLock.WriterLockAsync())
{

}
}
}

[Benchmark]
public async Task NeoSmart()
{
for (int i = 0; i < NumberOfLocks; i++)
{
using (await _NeoSmartLock.LockAsync())
{

}
}
}

[Benchmark]
public async Task Nito()
{
for (int i = 0; i < NumberOfLocks; i++)
{
using (await _NitoLock.LockAsync())
{

}
}
}
}
4 changes: 4 additions & 0 deletions src/AsyncKeyLock.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
using AsyncKeyLock.Benchmarks;
using BenchmarkDotNet.Running;

BenchmarkRunner.Run<BenchmarkSimpleKeyLock>();
1 change: 0 additions & 1 deletion src/AsyncKeyLock.Tests/AsyncLockTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ public async Task CancelWriterLock()
await Assert.ThrowsAsync<TaskCanceledException>(async () =>
{
var w2 = await lockEntity.WriterLockAsync(source.Token);

});
}

Expand Down
6 changes: 6 additions & 0 deletions src/AsyncKeyLock.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AsyncKeyLock", "AsyncKeyLoc
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AsyncKeyLock.Tests", "AsyncKeyLock.Tests\AsyncKeyLock.Tests.csproj", "{0F412253-BF96-430F-9825-9884F4D8C008}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AsyncKeyLock.Benchmarks", "AsyncKeyLock.Benchmarks\AsyncKeyLock.Benchmarks.csproj", "{977B3660-8811-4646-BB7B-B3083E39ACC2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +23,10 @@ Global
{0F412253-BF96-430F-9825-9884F4D8C008}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0F412253-BF96-430F-9825-9884F4D8C008}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0F412253-BF96-430F-9825-9884F4D8C008}.Release|Any CPU.Build.0 = Release|Any CPU
{977B3660-8811-4646-BB7B-B3083E39ACC2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{977B3660-8811-4646-BB7B-B3083E39ACC2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{977B3660-8811-4646-BB7B-B3083E39ACC2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{977B3660-8811-4646-BB7B-B3083E39ACC2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit 6d90d8f

Please sign in to comment.