-
-
Notifications
You must be signed in to change notification settings - Fork 987
/
Copy pathExporterIOTests.cs
171 lines (151 loc) · 6.61 KB
/
ExporterIOTests.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
166
167
168
169
170
171
using System;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Exporters;
using BenchmarkDotNet.Loggers;
using BenchmarkDotNet.Reports;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Tests.XUnit;
using BenchmarkDotNet.Validators;
using Xunit;
using Xunit.Abstractions;
namespace BenchmarkDotNet.IntegrationTests
{
public class ExporterIOTests : BenchmarkTestExecutor
{
public ExporterIOTests(ITestOutputHelper output) : base(output) { }
[Fact]
public void ExporterWritesToFile()
{
string resultsDirectoryPath = Path.GetTempPath();
var exporter = new MockExporter();
var mockSummary = GetMockSummary(resultsDirectoryPath, config: null);
var filePath = $"{Path.Combine(mockSummary.ResultsDirectoryPath, mockSummary.Title)}-report.txt"; // ExporterBase default
try
{
exporter.ExportToFiles(mockSummary, NullLogger.Instance);
Assert.Equal(1, exporter.ExportCount);
Assert.True(File.Exists(filePath));
}
finally
{
if (File.Exists(filePath))
File.Delete(filePath);
}
}
[FactEnvSpecific("On Unix, it's possible to write to an opened file", EnvRequirement.WindowsOnly)]
public void ExporterWorksWhenFileIsLocked()
{
string resultsDirectoryPath = Path.GetTempPath();
var exporter = new MockExporter();
var mockSummary = GetMockSummary(resultsDirectoryPath, config: null);
var filePath = $"{Path.Combine(mockSummary.ResultsDirectoryPath, mockSummary.Title)}-report.txt"; // ExporterBase default
try
{
exporter.ExportToFiles(mockSummary, NullLogger.Instance);
Assert.Equal(1, exporter.ExportCount);
Assert.True(File.Exists(filePath));
using (var handle = File.OpenRead(filePath)) // Gets a lock on the target file
{
exporter.ExportToFiles(mockSummary, NullLogger.Instance);
Assert.Equal(2, exporter.ExportCount);
}
var savedFiles = Directory.EnumerateFiles(Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(filePath) + "*");
Assert.Equal(2, savedFiles.Count());
}
finally
{
if (File.Exists(filePath))
File.Delete(filePath);
var otherFiles = Directory.EnumerateFiles(Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(filePath) + "*");
foreach (var file in otherFiles)
File.Delete(file);
}
}
[Fact]
public void ExporterUsesFullyQualifiedTypeNameAsFileName()
{
string resultsDirectoryPath = Path.GetTempPath();
var exporter = new MockExporter();
var mockSummary = GetMockSummary(resultsDirectoryPath, config: null, typeof(Generic<int>));
var expectedFilePath = $"{Path.Combine(mockSummary.ResultsDirectoryPath, "BenchmarkDotNet.IntegrationTests.Generic_Int32_")}-report.txt";
string? actualFilePath = null;
try
{
actualFilePath = exporter.ExportToFiles(mockSummary, NullLogger.Instance).First();
Assert.Equal(expectedFilePath, actualFilePath);
}
finally
{
if (File.Exists(actualFilePath))
File.Delete(actualFilePath);
}
}
[Fact]
public void ExporterUsesSummaryTitleAsFileNameWhenBenchmarksJoinedToSingleSummary()
{
string resultsDirectoryPath = Path.GetTempPath();
var exporter = new MockExporter();
var joinConfig = ManualConfig.CreateEmpty().WithOptions(ConfigOptions.JoinSummary);
var mockSummary = GetMockSummary(resultsDirectoryPath, joinConfig, typeof(ClassA), typeof(ClassB));
var expectedFilePath = $"{Path.Combine(mockSummary.ResultsDirectoryPath, mockSummary.Title)}-report.txt";
string? actualFilePath = null;
try
{
actualFilePath = exporter.ExportToFiles(mockSummary, NullLogger.Instance).First();
Assert.Equal(expectedFilePath, actualFilePath);
}
finally
{
if (File.Exists(actualFilePath))
File.Delete(actualFilePath);
}
}
private Summary GetMockSummary(string resultsDirectoryPath, IConfig config, params Type[] typesWithBenchmarks)
{
return new Summary(
title: "bdn-test",
reports: typesWithBenchmarks.Length > 0 ? CreateReports(typesWithBenchmarks, config) : ImmutableArray<BenchmarkReport>.Empty,
hostEnvironmentInfo: Environments.HostEnvironmentInfo.GetCurrent(),
resultsDirectoryPath: resultsDirectoryPath,
logFilePath: string.Empty,
totalTime: System.TimeSpan.Zero,
cultureInfo: TestCultureInfo.Instance,
validationErrors: ImmutableArray<ValidationError>.Empty,
columnHidingRules: ImmutableArray<IColumnHidingRule>.Empty
);
}
private class MockExporter : ExporterBase
{
public int ExportCount;
public override void ExportToLog(Summary summary, ILogger logger)
{
ExportCount++;
}
}
private ImmutableArray<BenchmarkReport> CreateReports(Type[] types, IConfig? config = null)
=> CreateBenchmarks(types, config).Select(CreateReport).ToImmutableArray();
private BenchmarkCase[] CreateBenchmarks(Type[] types, IConfig config)
{
return types.SelectMany(type => BenchmarkConverter.TypeToBenchmarks(type, config).BenchmarksCases).ToArray();
}
private BenchmarkReport CreateReport(BenchmarkCase benchmarkCase)
{
return new BenchmarkReport(success: true,
benchmarkCase: benchmarkCase,
generateResult: null,
buildResult: null,
executeResults: null,
metrics: null);
}
}
public class Generic<T>
{
[Benchmark]
public void Method() { }
}
}