-
-
Notifications
You must be signed in to change notification settings - Fork 986
/
Copy pathExceptionDiagnoserTests.cs
84 lines (75 loc) · 2.84 KB
/
ExceptionDiagnoserTests.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
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Reports;
using BenchmarkDotNet.Running;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
namespace BenchmarkDotNet.IntegrationTests
{
public class ExceptionDiagnoserTests
{
[Fact]
public void ExceptionCountIsAccurate()
{
var config = CreateConfig();
var summary = BenchmarkRunner.Run<ExceptionCount>(config);
AssertStats(summary, new Dictionary<string, (string metricName, double expectedValue)>
{
{ nameof(ExceptionCount.DoNothing), ("ExceptionFrequency", 0.0) },
{ nameof(ExceptionCount.ThrowOneException), ("ExceptionFrequency", 1.0) },
{ nameof(ExceptionCount.ThrowFromMultipleThreads), ("ExceptionFrequency", 100.0) }
});
}
public class ExceptionCount
{
[Benchmark]
public void ThrowOneException()
{
try
{
throw new Exception();
}
catch { }
}
[Benchmark]
public void DoNothing() { }
[Benchmark]
public async Task ThrowFromMultipleThreads()
{
void ThrowMultipleExceptions()
{
for (int i = 0; i < 10; i++)
{
ThrowOneException();
}
}
var tasks = Enumerable.Range(1, 10).Select(
i => Task.Run(ThrowMultipleExceptions));
await Task.WhenAll(tasks);
}
}
private IConfig CreateConfig()
=> 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
.AddColumnProvider(DefaultColumnProviders.Instance)
.AddDiagnoser(ExceptionDiagnoser.Default);
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);
Assert.Equal(assertion.Value.expectedValue, metric.Value.Value);
}
}
}
}