-
Notifications
You must be signed in to change notification settings - Fork 275
/
Copy pathPerf.SemaphoreSlim.cs
55 lines (48 loc) · 1.48 KB
/
Perf.SemaphoreSlim.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using BenchmarkDotNet.Attributes;
using MicroBenchmarks;
using System.Threading.Tasks;
namespace System.Threading.Tests
{
[BenchmarkCategory(Categories.Libraries, Categories.NoWASM)]
public class Perf_SemaphoreSlim
{
private readonly SemaphoreSlim _sem = new SemaphoreSlim(0, 1);
private readonly CancellationTokenSource _cts = new CancellationTokenSource();
[Benchmark]
public void ReleaseWait()
{
_sem.Release();
_sem.Wait();
}
[Benchmark]
public Task ReleaseWaitAsync()
{
_sem.Release();
return _sem.WaitAsync();
}
[Benchmark]
public Task ReleaseWaitAsync_WithCancellationToken()
{
Task t = _sem.WaitAsync(_cts.Token);
_sem.Release();
return t;
}
[Benchmark]
public Task ReleaseWaitAsync_WithTimeout()
{
Task t = _sem.WaitAsync(TimeSpan.FromDays(1));
_sem.Release();
return t;
}
[Benchmark]
public Task ReleaseWaitAsync_WithCancellationTokenAndTimeout()
{
Task t = _sem.WaitAsync(TimeSpan.FromDays(1), _cts.Token);
_sem.Release();
return t;
}
}
}