Skip to content

Commit 6d90d8f

Browse files
committed
Added benchmark
1 parent 1d5ab2a commit 6d90d8f

File tree

6 files changed

+149
-1
lines changed

6 files changed

+149
-1
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<OutputType>Exe</OutputType>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="AsyncKeyedLock" Version="6.1.1" />
12+
<PackageReference Include="BenchmarkDotNet" Version="0.13.4" />
13+
<PackageReference Include="NeoSmart.AsyncLock" Version="3.2.1" />
14+
<PackageReference Include="Nito.AsyncEx" Version="5.1.2" />
15+
<PackageReference Include="SixLabors.ImageSharp.Web" Version="2.0.2" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<ProjectReference Include="..\AsyncKeyLock\AsyncKeyLock.csproj" />
20+
</ItemGroup>
21+
22+
</Project>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using BenchmarkDotNet.Attributes;
2+
using BenchmarkDotNet.Jobs;
3+
4+
namespace AsyncKeyLock.Benchmarks;
5+
6+
[MemoryDiagnoser]
7+
public class BenchmarkSimpleKeyLock
8+
{
9+
[GlobalSetup]
10+
public void GlobalSetup()
11+
{
12+
_AsyncKeyLock = new AsyncLock<string>();
13+
_AsyncKeyedLock = new AsyncKeyedLock.AsyncKeyedLocker<string>();
14+
_ImageSharpWebLock = new SixLabors.ImageSharp.Web.Synchronization.AsyncKeyLock<string>();
15+
}
16+
17+
private AsyncLock<string> _AsyncKeyLock;
18+
private AsyncKeyedLock.AsyncKeyedLocker<string> _AsyncKeyedLock;
19+
private SixLabors.ImageSharp.Web.Synchronization.AsyncKeyLock<string> _ImageSharpWebLock;
20+
21+
[Params(1_00, 1_000, 10_000)]
22+
public int NumberOfLocks;
23+
24+
[Benchmark(Baseline = true)]
25+
public async Task AsyncKeyLock()
26+
{
27+
for (int i = 0; i < NumberOfLocks; i++)
28+
{
29+
using (await _AsyncKeyLock.WriterLockAsync(string.Empty))
30+
{
31+
32+
}
33+
}
34+
}
35+
36+
[Benchmark]
37+
public async Task AsyncKeyedLock()
38+
{
39+
for (int i = 0; i < NumberOfLocks; i++)
40+
{
41+
using (await _AsyncKeyedLock.LockAsync(string.Empty))
42+
{
43+
44+
}
45+
}
46+
}
47+
48+
[Benchmark]
49+
public async Task ImageSharpWeb()
50+
{
51+
for (int i = 0; i < NumberOfLocks; i++)
52+
{
53+
using (await _ImageSharpWebLock.LockAsync(string.Empty))
54+
{
55+
56+
}
57+
}
58+
}
59+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using BenchmarkDotNet.Attributes;
2+
3+
namespace AsyncKeyLock.Benchmarks;
4+
5+
[MemoryDiagnoser]
6+
public class BenchmarkSimpleWriterLock
7+
{
8+
[GlobalSetup]
9+
public void GlobalSetup()
10+
{
11+
_AsyncKeyLock = new AsyncLock();
12+
_NeoSmartLock = new NeoSmart.AsyncLock.AsyncLock();
13+
_NitoLock = new Nito.AsyncEx.AsyncLock();
14+
}
15+
16+
private AsyncLock _AsyncKeyLock;
17+
private NeoSmart.AsyncLock.AsyncLock _NeoSmartLock;
18+
private Nito.AsyncEx.AsyncLock _NitoLock;
19+
20+
[Params(1_00, 1_000, 10_000)]
21+
public int NumberOfLocks;
22+
23+
[Benchmark(Baseline = true)]
24+
public async Task AsyncKeyLock()
25+
{
26+
for (int i = 0; i < NumberOfLocks; i++)
27+
{
28+
using (await _AsyncKeyLock.WriterLockAsync())
29+
{
30+
31+
}
32+
}
33+
}
34+
35+
[Benchmark]
36+
public async Task NeoSmart()
37+
{
38+
for (int i = 0; i < NumberOfLocks; i++)
39+
{
40+
using (await _NeoSmartLock.LockAsync())
41+
{
42+
43+
}
44+
}
45+
}
46+
47+
[Benchmark]
48+
public async Task Nito()
49+
{
50+
for (int i = 0; i < NumberOfLocks; i++)
51+
{
52+
using (await _NitoLock.LockAsync())
53+
{
54+
55+
}
56+
}
57+
}
58+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
using AsyncKeyLock.Benchmarks;
2+
using BenchmarkDotNet.Running;
3+
4+
BenchmarkRunner.Run<BenchmarkSimpleKeyLock>();

src/AsyncKeyLock.Tests/AsyncLockTest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ public async Task CancelWriterLock()
148148
await Assert.ThrowsAsync<TaskCanceledException>(async () =>
149149
{
150150
var w2 = await lockEntity.WriterLockAsync(source.Token);
151-
152151
});
153152
}
154153

src/AsyncKeyLock.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AsyncKeyLock", "AsyncKeyLoc
77
EndProject
88
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AsyncKeyLock.Tests", "AsyncKeyLock.Tests\AsyncKeyLock.Tests.csproj", "{0F412253-BF96-430F-9825-9884F4D8C008}"
99
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AsyncKeyLock.Benchmarks", "AsyncKeyLock.Benchmarks\AsyncKeyLock.Benchmarks.csproj", "{977B3660-8811-4646-BB7B-B3083E39ACC2}"
11+
EndProject
1012
Global
1113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1214
Debug|Any CPU = Debug|Any CPU
@@ -21,6 +23,10 @@ Global
2123
{0F412253-BF96-430F-9825-9884F4D8C008}.Debug|Any CPU.Build.0 = Debug|Any CPU
2224
{0F412253-BF96-430F-9825-9884F4D8C008}.Release|Any CPU.ActiveCfg = Release|Any CPU
2325
{0F412253-BF96-430F-9825-9884F4D8C008}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{977B3660-8811-4646-BB7B-B3083E39ACC2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{977B3660-8811-4646-BB7B-B3083E39ACC2}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{977B3660-8811-4646-BB7B-B3083E39ACC2}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{977B3660-8811-4646-BB7B-B3083E39ACC2}.Release|Any CPU.Build.0 = Release|Any CPU
2430
EndGlobalSection
2531
GlobalSection(SolutionProperties) = preSolution
2632
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)