forked from dotnet/BenchmarkDotNet
/
ThreadingDiagnoserTests.cs
165 lines (135 loc) · 6.13 KB
/
ThreadingDiagnoserTests.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#if !NETFRAMEWORK
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Loggers;
using BenchmarkDotNet.Reports;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Tests.Loggers;
using BenchmarkDotNet.Toolchains;
using BenchmarkDotNet.Toolchains.CoreRt;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using BenchmarkDotNet.Portability;
using Xunit;
using Xunit.Abstractions;
namespace BenchmarkDotNet.IntegrationTests
{
public class ThreadingDiagnoserTests
{
private readonly ITestOutputHelper output;
public ThreadingDiagnoserTests(ITestOutputHelper outputHelper) => output = outputHelper;
public static IEnumerable<object[]> GetToolchains()
{
yield return new object[] { Job.Default.GetToolchain() };
// Currently, CoreRtToolchain.Core50 produces the following error on Unix without installed clang:
// Standard error:
// ~/.nuget/packages/microsoft.dotnet.ilcompiler/6.0.0-preview.5.21269.1/build/Microsoft.NETCore.Native.Unix.props(99,5):
// error : Platform linker ('clang') not found. Try installing clang or the appropriate package for your platform to resolve the problem.
if (RuntimeInformation.IsWindows())
yield return new object[] { CoreRtToolchain.Core50 };
// TODO: Support InProcessEmitToolchain.Instance
// yield return new object[] { InProcessEmitToolchain.Instance };
}
[Theory, MemberData(nameof(GetToolchains))]
public void CompletedWorkItemCountIsAccurate(IToolchain toolchain)
{
var config = CreateConfig(toolchain);
var summary = BenchmarkRunner.Run<CompletedWorkItemCount>(config);
AssertStats(summary, new Dictionary<string, (string metricName, double expectedValue)>
{
{ nameof(CompletedWorkItemCount.DoNothing), ("CompletedWorkItemCount", 0.0) },
{ nameof(CompletedWorkItemCount.CompleteOneWorkItem), ("CompletedWorkItemCount", 1.0) }
});
}
public class CompletedWorkItemCount
{
[Benchmark]
public void CompleteOneWorkItem()
{
ManualResetEvent done = new ManualResetEvent(false);
ThreadPool.QueueUserWorkItem(m => (m as ManualResetEvent).Set(), done);
done.WaitOne();
}
[Benchmark]
public void DoNothing() { }
}
[Theory, MemberData(nameof(GetToolchains))]
public void LockContentionCountIsAccurate(IToolchain toolchain)
{
var config = CreateConfig(toolchain);
var summary = BenchmarkRunner.Run<LockContentionCount>(config);
AssertStats(summary, new Dictionary<string, (string metricName, double expectedValue)>
{
{ nameof(LockContentionCount.DoNothing), ("LockContentionCount", 0.0) },
{ nameof(LockContentionCount.RunIntoLockContention), ("LockContentionCount", 1.0) }
});
}
public class LockContentionCount
{
private readonly object guard = new object();
private ManualResetEvent lockTaken;
private ManualResetEvent failedToAcquire;
[Benchmark]
public void DoNothing() { }
[Benchmark]
public void RunIntoLockContention()
{
lockTaken = new ManualResetEvent(false);
failedToAcquire = new ManualResetEvent(false);
Thread first = new Thread(FirstThread);
Thread second = new Thread(SecondThread);
first.Start();
second.Start();
second.Join();
first.Join();
}
private void FirstThread()
{
Monitor.Enter(guard);
lockTaken.Set();
failedToAcquire.WaitOne();
Monitor.Exit(guard);
}
private void SecondThread()
{
lockTaken.WaitOne();
bool taken = Monitor.TryEnter(guard, TimeSpan.FromMilliseconds(10));
if (taken)
{
throw new InvalidOperationException("Impossible!");
}
failedToAcquire.Set();
}
}
private IConfig CreateConfig(IToolchain toolchain)
=> ManualConfig.CreateEmpty()
.AddJob(Job.ShortRun
.WithEvaluateOverhead(false) // no need to run idle for this test
.WithWarmupCount(0) // don't run warmup to save some time for our CI runs
.WithIterationCount(1) // single iteration is enough for us
.WithGcForce(false)
.WithToolchain(toolchain))
.AddColumnProvider(DefaultColumnProviders.Instance)
.AddDiagnoser(ThreadingDiagnoser.Default)
.AddLogger(toolchain.IsInProcess
? ConsoleLogger.Default
: new OutputLogger(output)); // we can't use OutputLogger for the InProcess toolchains because it allocates memory on the same thread
private void AssertStats(Summary summary, Dictionary<string, (string metricName, double expectedValue)> assertions)
{
foreach (var assertion in assertions)
{
var selectedReport = summary.Reports.Single(report => report.BenchmarkCase.DisplayInfo.Contains(assertion.Key));
var metric = selectedReport.Metrics.Single(m => m.Key == assertion.Value.metricName);
// precision is set to 2 because CoreCLR might schedule some work item on it's own and hence affect the results..
// precision = 3 is not enough (e.g., sometimes the actual value may be equal 1.0009765625 while the expected value is 1.0)
Assert.Equal(assertion.Value.expectedValue, metric.Value.Value, precision: 2);
}
}
}
}
#endif