-
-
Notifications
You must be signed in to change notification settings - Fork 986
/
Copy pathMockFactory.cs
116 lines (104 loc) · 5.98 KB
/
MockFactory.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
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Engines;
using BenchmarkDotNet.Reports;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Tests.Builders;
using BenchmarkDotNet.Toolchains;
using BenchmarkDotNet.Toolchains.Results;
using BenchmarkDotNet.Validators;
namespace BenchmarkDotNet.Tests.Mocks
{
public static class MockFactory
{
public static Summary CreateSummary(Type benchmarkType) => CreateSummary(new Type[] { benchmarkType });
public static Summary CreateSummary(Type[] benchmarkTypes)
{
var runInfos = benchmarkTypes.Select(benchmarkType => BenchmarkConverter.TypeToBenchmarks(benchmarkType));
return new Summary(
"MockSummary",
runInfos.SelectMany(runInfo => runInfo.BenchmarksCases).Select((benchmark, index) => CreateReport(benchmark, 5, (index + 1) * 100)).ToImmutableArray(),
new HostEnvironmentInfoBuilder().WithoutDotNetSdkVersion().Build(),
string.Empty,
string.Empty,
TimeSpan.FromMinutes(1),
TestCultureInfo.Instance,
ImmutableArray<ValidationError>.Empty,
ImmutableArray<IColumnHidingRule>.Empty);
}
public static Summary CreateSummary(IConfig config) => new Summary(
"MockSummary",
CreateReports(config),
new HostEnvironmentInfoBuilder().WithoutDotNetSdkVersion().Build(),
string.Empty,
string.Empty,
TimeSpan.FromMinutes(1),
config.CultureInfo,
ImmutableArray<ValidationError>.Empty,
ImmutableArray<IColumnHidingRule>.Empty);
public static Summary CreateSummary(IConfig config, bool hugeSd, Metric[] metrics)
=> CreateSummary<MockBenchmarkClass>(config, hugeSd, metrics);
public static Summary CreateSummary<TBenchmark>(IConfig config, bool hugeSd, Metric[] metrics) => new Summary(
"MockSummary",
CreateBenchmarks<TBenchmark>(config).Select(b => CreateReport(b, hugeSd, metrics)).ToImmutableArray(),
new HostEnvironmentInfoBuilder().Build(),
string.Empty,
string.Empty,
TimeSpan.FromMinutes(1),
TestCultureInfo.Instance,
ImmutableArray<ValidationError>.Empty,
ImmutableArray<IColumnHidingRule>.Empty);
public static Summary CreateSummary<TBenchmark>(IConfig config, bool hugeSd, Func<BenchmarkCase, Metric[]> metricsBuilder) => new Summary(
"MockSummary",
CreateBenchmarks<TBenchmark>(config).Select(b => CreateReport(b, hugeSd, metricsBuilder(b))).ToImmutableArray(),
new HostEnvironmentInfoBuilder().Build(),
string.Empty,
string.Empty,
TimeSpan.FromMinutes(1),
TestCultureInfo.Instance,
ImmutableArray<ValidationError>.Empty,
ImmutableArray<IColumnHidingRule>.Empty);
private static ImmutableArray<BenchmarkReport> CreateReports(IConfig config)
=> CreateBenchmarks<MockBenchmarkClass>(config).Select(CreateSimpleReport).ToImmutableArray();
private static BenchmarkCase[] CreateBenchmarks<TBenchmarks>(IConfig config)
=> BenchmarkConverter.TypeToBenchmarks(typeof(TBenchmarks), config).BenchmarksCases;
private static BenchmarkReport CreateSimpleReport(BenchmarkCase benchmarkCase) => CreateReport(benchmarkCase, 1, 1);
private static BenchmarkReport CreateReport(BenchmarkCase benchmarkCase, int n, double nanoseconds)
{
var buildResult = BuildResult.Success(GenerateResult.Success(ArtifactsPaths.Empty, Array.Empty<string>()));
var measurements = Enumerable.Range(0, n)
.Select(index => new Measurement(1, IterationMode.Workload, IterationStage.Result, index + 1, 1, nanoseconds + index).ToString())
.ToList();
var executeResult = new ExecuteResult(true, 0, default, measurements, new[] { $"// Runtime=extra output line" }, Array.Empty<string>(), 1);
return new BenchmarkReport(true, benchmarkCase, buildResult, buildResult, new List<ExecuteResult> { executeResult }, Array.Empty<Metric>());
}
private static BenchmarkReport CreateReport(BenchmarkCase benchmarkCase, bool hugeSd, Metric[] metrics)
{
var buildResult = BuildResult.Success(GenerateResult.Success(ArtifactsPaths.Empty, Array.Empty<string>()));
bool isFoo = benchmarkCase.Descriptor.WorkloadMethodDisplayInfo == "Foo";
bool isBar = benchmarkCase.Descriptor.WorkloadMethodDisplayInfo == "Bar";
var measurements = new List<Measurement>
{
new Measurement(1, IterationMode.Workload, IterationStage.Result, 1, 1, 1),
new Measurement(1, IterationMode.Workload, IterationStage.Result, 2, 1, hugeSd && isFoo ? 2 : 1),
new Measurement(1, IterationMode.Workload, IterationStage.Result, 3, 1, hugeSd && isBar ? 3 : 1),
new Measurement(1, IterationMode.Workload, IterationStage.Result, 4, 1, hugeSd && isFoo ? 2 : 1),
new Measurement(1, IterationMode.Workload, IterationStage.Result, 5, 1, hugeSd && isBar ? 3 : 1),
new Measurement(1, IterationMode.Workload, IterationStage.Result, 6, 1, 1)
};
var executeResult = new ExecuteResult(measurements, default, default, 0);
return new BenchmarkReport(true, benchmarkCase, buildResult, buildResult, new List<ExecuteResult> { executeResult }, metrics);
}
[LongRunJob]
public class MockBenchmarkClass
{
[Benchmark] public void Foo() { }
[Benchmark] public void Bar() { }
}
}
}