Skip to content

Commit 464caba

Browse files
committed
Refactor to make StreamOrLoggerWriter internal.
1 parent 8bd2a1a commit 464caba

38 files changed

+154
-65
lines changed

src/BenchmarkDotNet/Disassemblers/Exporters/CombinedDisassemblyExporter.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using BenchmarkDotNet.Diagnosers;
77
using BenchmarkDotNet.Exporters;
88
using BenchmarkDotNet.Extensions;
9+
using BenchmarkDotNet.Helpers;
910
using BenchmarkDotNet.Loggers;
1011
using BenchmarkDotNet.Reports;
1112
using BenchmarkDotNet.Running;
@@ -35,7 +36,7 @@ internal CombinedDisassemblyExporter(IReadOnlyDictionary<BenchmarkCase, Disassem
3536
protected override string FileExtension => "html";
3637
protected override string FileCaption => "disassembly-report";
3738

38-
protected override async ValueTask ExportAsync(Summary summary, StreamOrLoggerWriter writer, CancellationToken cancellationToken)
39+
public override async ValueTask ExportAsync(Summary summary, CancelableStreamWriter writer, CancellationToken cancellationToken)
3940
{
4041
var benchmarksByTarget = summary.BenchmarksCases
4142
.Where(benchmark => results.ContainsKey(benchmark))
@@ -78,7 +79,7 @@ await PrintTableAsync(
7879
await writer.WriteLineAsync("</html>", cancellationToken).ConfigureAwait(false);
7980
}
8081

81-
private async ValueTask PrintTableAsync(BenchmarkCase[] benchmarksCase, StreamOrLoggerWriter writer, string title, Func<BenchmarkCase, string> headerTitleProvider, CancellationToken cancellationToken)
82+
private async ValueTask PrintTableAsync(BenchmarkCase[] benchmarksCase, CancelableStreamWriter writer, string title, Func<BenchmarkCase, string> headerTitleProvider, CancellationToken cancellationToken)
8283
{
8384
await writer.WriteLineAsync("<table>", cancellationToken).ConfigureAwait(false);
8485
await writer.WriteLineAsync("<thead>", cancellationToken).ConfigureAwait(false);

src/BenchmarkDotNet/Disassemblers/Exporters/GithubMarkdownDiffDisassemblyExporter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ internal GithubMarkdownDiffDisassemblyExporter(IReadOnlyDictionary<BenchmarkCase
2828
this.config = config;
2929
}
3030

31-
protected override async ValueTask ExportAsync(Summary summary, StreamOrLoggerWriter writer, CancellationToken cancellationToken)
31+
public override async ValueTask ExportAsync(Summary summary, CancelableStreamWriter writer, CancellationToken cancellationToken)
3232
{
3333
var benchmarksCases = summary.BenchmarksCases.Where(results.ContainsKey).ToArray();
3434

@@ -45,7 +45,7 @@ protected override async ValueTask ExportAsync(Summary summary, StreamOrLoggerWr
4545
}
4646
}
4747

48-
private async ValueTask ExportDiff(Summary summary, StreamOrLoggerWriter writer, BenchmarkCase firstBenchmarkCase, BenchmarkCase secondBenchmarkCase, CancellationToken cancellationToken)
48+
private async ValueTask ExportDiff(Summary summary, CancelableStreamWriter writer, BenchmarkCase firstBenchmarkCase, BenchmarkCase secondBenchmarkCase, CancellationToken cancellationToken)
4949
{
5050
// We want to get diff for the same method and different JITs
5151
if (firstBenchmarkCase.Descriptor.WorkloadMethod == secondBenchmarkCase.Descriptor.WorkloadMethod)

src/BenchmarkDotNet/Disassemblers/Exporters/GithubMarkdownDisassemblyExporter.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using BenchmarkDotNet.Diagnosers;
66
using BenchmarkDotNet.Exporters;
77
using BenchmarkDotNet.Extensions;
8+
using BenchmarkDotNet.Helpers;
89
using BenchmarkDotNet.Loggers;
910
using BenchmarkDotNet.Reports;
1011
using BenchmarkDotNet.Running;
@@ -25,7 +26,7 @@ internal GithubMarkdownDisassemblyExporter(IReadOnlyDictionary<BenchmarkCase, Di
2526
protected override string FileExtension => "md";
2627
protected override string FileCaption => "asm";
2728

28-
protected override async ValueTask ExportAsync(Summary summary, StreamOrLoggerWriter writer, CancellationToken cancellationToken)
29+
public override async ValueTask ExportAsync(Summary summary, CancelableStreamWriter writer, CancellationToken cancellationToken)
2930
{
3031
foreach (var benchmarkCase in summary.BenchmarksCases.Where(results.ContainsKey))
3132
{
@@ -36,7 +37,7 @@ protected override async ValueTask ExportAsync(Summary summary, StreamOrLoggerWr
3637
}
3738
}
3839

39-
internal static async ValueTask ExportAsync(StreamOrLoggerWriter writer, DisassemblyResult disassemblyResult, DisassemblyDiagnoserConfig config, bool quotingCode = true, CancellationToken cancellationToken = default)
40+
internal static async ValueTask ExportAsync(CancelableStreamWriter writer, DisassemblyResult disassemblyResult, DisassemblyDiagnoserConfig config, bool quotingCode = true, CancellationToken cancellationToken = default)
4041
{
4142
int methodIndex = 0;
4243
foreach (var method in disassemblyResult.Methods.Where(method => method.Problem.IsBlank()))

src/BenchmarkDotNet/Disassemblers/Exporters/HtmlDisassemblyExporter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ internal HtmlDisassemblyExporter(IReadOnlyDictionary<BenchmarkCase, DisassemblyR
2929
protected override string FileExtension => "html";
3030
protected override string FileCaption => "asm";
3131

32-
protected override async ValueTask ExportAsync(Summary summary, StreamOrLoggerWriter writer, CancellationToken cancellationToken)
32+
public override async ValueTask ExportAsync(Summary summary, CancelableStreamWriter writer, CancellationToken cancellationToken)
3333
{
3434
await writer.WriteLineAsync("<!DOCTYPE html><html lang='en'><head><meta charset='utf-8' /><head>", cancellationToken).ConfigureAwait(false);
3535
await writer.WriteLineAsync($"<title>Pretty Output of DisassemblyDiagnoser for {summary.Title}</title>", cancellationToken).ConfigureAwait(false);
@@ -52,7 +52,7 @@ await writer.WriteLineAsync(@"
5252
await writer.WriteLineAsync("</body></html>", cancellationToken).ConfigureAwait(false);
5353
}
5454

55-
private async ValueTask<int> Export(StreamOrLoggerWriter writer, Summary summary, DisassemblyResult disassemblyResult, BenchmarkCase benchmarkCase, int referenceIndex, CancellationToken cancellationToken)
55+
private async ValueTask<int> Export(CancelableStreamWriter writer, Summary summary, DisassemblyResult disassemblyResult, BenchmarkCase benchmarkCase, int referenceIndex, CancellationToken cancellationToken)
5656
{
5757
await writer.WriteLineAsync($"<h2>{summary[benchmarkCase]!.GetRuntimeInfo()}</h2>", cancellationToken).ConfigureAwait(false);
5858
await writer.WriteLineAsync($"<h3>Job: {benchmarkCase.Job.DisplayInfo}</h3>", cancellationToken).ConfigureAwait(false);

src/BenchmarkDotNet/Exporters/AsciiDocExporter.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Linq;
22
using System.Threading;
33
using System.Threading.Tasks;
4+
using BenchmarkDotNet.Helpers;
45
using BenchmarkDotNet.Loggers;
56
using BenchmarkDotNet.Reports;
67

@@ -16,35 +17,36 @@ private AsciiDocExporter()
1617
{
1718
}
1819

19-
protected override async ValueTask ExportAsync(Summary summary, StreamOrLoggerWriter writer, CancellationToken cancellationToken)
20+
public override async ValueTask ExportAsync(Summary summary, CancelableStreamWriter writer, CancellationToken cancellationToken)
2021
{
2122
await writer.WriteLineAsync("....", cancellationToken).ConfigureAwait(false);
2223
foreach (string infoLine in summary.HostEnvironmentInfo.ToFormattedString())
2324
{
24-
await writer.WriteLineAsync(infoLine, LogKind.Info, cancellationToken).ConfigureAwait(false);
25+
await writer.WriteLineAsync(infoLine, cancellationToken).ConfigureAwait(false);
2526
}
26-
await writer.WriteLineAsync(summary.AllRuntimes, LogKind.Info, cancellationToken).ConfigureAwait(false);
27+
await writer.WriteLineAsync(summary.AllRuntimes, cancellationToken).ConfigureAwait(false);
2728
await writer.WriteLineAsync(cancellationToken).ConfigureAwait(false);
2829

2930
var table = summary.Table;
3031
if (table.FullContent.Length == 0)
3132
{
3233
await writer.WriteLineAsync("[WARNING]", cancellationToken).ConfigureAwait(false);
3334
await writer.WriteLineAsync("====", cancellationToken).ConfigureAwait(false);
34-
await writer.WriteLineAsync("There are no benchmarks found ", LogKind.Error, cancellationToken).ConfigureAwait(false);
35+
await writer.WriteLineAsync("There are no benchmarks found ", cancellationToken).ConfigureAwait(false);
3536
await writer.WriteLineAsync("====", cancellationToken).ConfigureAwait(false);
3637
await writer.WriteLineAsync(cancellationToken).ConfigureAwait(false);
3738
return;
3839
}
3940

40-
await table.PrintCommonColumnsAsync(writer, cancellationToken).ConfigureAwait(false);
41+
var wrappedWriter = new StreamWriterWrapper(writer);
42+
await table.PrintCommonColumnsAsync(wrappedWriter, cancellationToken).ConfigureAwait(false);
4143
await writer.WriteLineAsync("....", cancellationToken).ConfigureAwait(false);
4244

4345
await writer.WriteLineAsync("[options=\"header\"]", cancellationToken).ConfigureAwait(false);
4446
await writer.WriteLineAsync("|===", cancellationToken).ConfigureAwait(false);
45-
await table.PrintLineAsync(table.FullHeader, writer, "|", string.Empty, cancellationToken).ConfigureAwait(false);
47+
await table.PrintLineAsync(table.FullHeader, wrappedWriter, "|", string.Empty, cancellationToken).ConfigureAwait(false);
4648
foreach (var line in table.FullContent)
47-
await table.PrintLineAsync(line, writer, "|", string.Empty, cancellationToken).ConfigureAwait(false);
49+
await table.PrintLineAsync(line, wrappedWriter, "|", string.Empty, cancellationToken).ConfigureAwait(false);
4850
await writer.WriteLineAsync("|===", cancellationToken).ConfigureAwait(false);
4951

5052
var benchmarksWithTroubles = summary.Reports
@@ -56,10 +58,10 @@ protected override async ValueTask ExportAsync(Summary summary, StreamOrLoggerWr
5658
{
5759
await writer.WriteLineAsync(cancellationToken).ConfigureAwait(false);
5860
await writer.WriteLineAsync("[WARNING]", cancellationToken).ConfigureAwait(false);
59-
await writer.WriteLineAsync(".Benchmarks with issues", LogKind.Error, cancellationToken).ConfigureAwait(false);
61+
await writer.WriteLineAsync(".Benchmarks with issues", cancellationToken).ConfigureAwait(false);
6062
await writer.WriteLineAsync("====", cancellationToken).ConfigureAwait(false);
6163
foreach (var benchmarkWithTroubles in benchmarksWithTroubles)
62-
await writer.WriteLineAsync($"* {benchmarkWithTroubles.DisplayInfo}", LogKind.Error, cancellationToken).ConfigureAwait(false);
64+
await writer.WriteLineAsync($"* {benchmarkWithTroubles.DisplayInfo}", cancellationToken).ConfigureAwait(false);
6365
await writer.WriteLineAsync("====", cancellationToken).ConfigureAwait(false);
6466
}
6567
}

src/BenchmarkDotNet/Exporters/BenchmarkReportExporter.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Threading;
22
using System.Threading.Tasks;
33
using BenchmarkDotNet.Extensions;
4+
using BenchmarkDotNet.Helpers;
45
using BenchmarkDotNet.Loggers;
56
using BenchmarkDotNet.Reports;
67

@@ -14,7 +15,13 @@ private BenchmarkReportExporter()
1415
{
1516
}
1617

17-
protected override async ValueTask ExportAsync(Summary summary, StreamOrLoggerWriter writer, CancellationToken cancellationToken)
18+
public override ValueTask ExportAsync(Summary summary, CancelableStreamWriter writer, CancellationToken cancellationToken)
19+
=> ExportCore(summary, new StreamWriterWrapper(writer), cancellationToken);
20+
21+
internal static ValueTask ExportToLogAsync(Summary summary, ILogger logger, CancellationToken cancellationToken)
22+
=> ExportCore(summary, new LoggerWriter(logger), cancellationToken);
23+
24+
private static async ValueTask ExportCore(Summary summary, StreamOrLoggerWriter writer, CancellationToken cancellationToken)
1825
{
1926
foreach (var report in summary.Reports)
2027
{

src/BenchmarkDotNet/Exporters/Csv/CsvExporter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Threading;
22
using System.Threading.Tasks;
3+
using BenchmarkDotNet.Helpers;
34
using BenchmarkDotNet.Loggers;
45
using BenchmarkDotNet.Reports;
56
using JetBrains.Annotations;
@@ -21,7 +22,7 @@ public CsvExporter(CsvSeparator separator, SummaryStyle? style = null)
2122
this.style = style;
2223
}
2324

24-
protected override async ValueTask ExportAsync(Summary summary, StreamOrLoggerWriter writer, CancellationToken cancellationToken)
25+
public override async ValueTask ExportAsync(Summary summary, CancelableStreamWriter writer, CancellationToken cancellationToken)
2526
{
2627
string realSeparator = separator.ToRealSeparator();
2728
var exportStyle = (style ?? summary.Style).WithZeroMetricValuesInContent();

src/BenchmarkDotNet/Exporters/Csv/CsvMeasurementsExporter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using BenchmarkDotNet.Characteristics;
77
using BenchmarkDotNet.Columns;
88
using BenchmarkDotNet.Jobs;
9+
using BenchmarkDotNet.Helpers;
910
using BenchmarkDotNet.Loggers;
1011
using BenchmarkDotNet.Reports;
1112
using JetBrains.Annotations;
@@ -38,7 +39,7 @@ public CsvMeasurementsExporter(CsvSeparator separator, SummaryStyle? style = nul
3839

3940
[PublicAPI] public static Job[] GetJobs(Summary summary) => summary.BenchmarksCases.Select(b => b.Job).ToArray();
4041

41-
protected override async ValueTask ExportAsync(Summary summary, StreamOrLoggerWriter writer, CancellationToken cancellationToken)
42+
public override async ValueTask ExportAsync(Summary summary, CancelableStreamWriter writer, CancellationToken cancellationToken)
4243
{
4344
string realSeparator = Separator;
4445
var columns = GetColumns(summary);

src/BenchmarkDotNet/Exporters/ExporterBase.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,7 @@ public abstract class ExporterBase : IExporter
1818
protected virtual string FileNameSuffix => string.Empty;
1919
protected virtual string FileCaption => "report";
2020

21-
// This will execute synchronously (because ILogger doesn't support async),
22-
// but it's needed to re-use the logic for both file and logger export to avoid code duplication on every implementation.
23-
internal async ValueTask ExportToLogAsync(Summary summary, ILogger logger, CancellationToken cancellationToken)
24-
{
25-
var writer = new LoggerWriter(logger);
26-
await ExportAsync(summary, writer, cancellationToken).ConfigureAwait(false);
27-
}
28-
29-
protected abstract ValueTask ExportAsync(Summary summary, StreamOrLoggerWriter writer, CancellationToken cancellationToken);
21+
public abstract ValueTask ExportAsync(Summary summary, CancelableStreamWriter writer, CancellationToken cancellationToken);
3022

3123
public async ValueTask ExportAsync(Summary summary, ILogger logger, CancellationToken cancellationToken)
3224
{

src/BenchmarkDotNet/Exporters/HtmlExporter.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Threading;
22
using System.Threading.Tasks;
33
using BenchmarkDotNet.Extensions;
4+
using BenchmarkDotNet.Helpers;
45
using BenchmarkDotNet.Loggers;
56
using BenchmarkDotNet.Reports;
67

@@ -20,7 +21,7 @@ public class HtmlExporter : ExporterBase
2021

2122
public static readonly IExporter Default = new HtmlExporter();
2223

23-
protected override async ValueTask ExportAsync(Summary summary, StreamOrLoggerWriter writer, CancellationToken cancellationToken)
24+
public override async ValueTask ExportAsync(Summary summary, CancelableStreamWriter writer, CancellationToken cancellationToken)
2425
{
2526
await writer.WriteLineAsync("<!DOCTYPE html>", cancellationToken).ConfigureAwait(false);
2627
await writer.WriteLineAsync("<html lang='en'>", cancellationToken).ConfigureAwait(false);
@@ -47,24 +48,25 @@ protected override async ValueTask ExportAsync(Summary summary, StreamOrLoggerWr
4748
await writer.WriteLineAsync("</html>", cancellationToken).ConfigureAwait(false);
4849
}
4950

50-
private static async ValueTask PrintTableAsync(SummaryTable table, StreamOrLoggerWriter writer, CancellationToken cancellationToken)
51+
private static async ValueTask PrintTableAsync(SummaryTable table, CancelableStreamWriter writer, CancellationToken cancellationToken)
5152
{
5253
if (table.FullContent.Length == 0)
5354
{
54-
await writer.WriteLineAsync("<pre>There are no benchmarks found</pre>", LogKind.Error, cancellationToken).ConfigureAwait(false);
55+
await writer.WriteLineAsync("<pre>There are no benchmarks found</pre>", cancellationToken).ConfigureAwait(false);
5556
return;
5657
}
5758

59+
var wrappedWriter = new StreamWriterWrapper(writer);
5860
await writer.WriteAsync("<pre><code>", cancellationToken).ConfigureAwait(false);
59-
await table.PrintCommonColumnsAsync(writer, cancellationToken).ConfigureAwait(false);
61+
await table.PrintCommonColumnsAsync(wrappedWriter, cancellationToken).ConfigureAwait(false);
6062
await writer.WriteLineAsync("</code></pre>", cancellationToken).ConfigureAwait(false);
6163
await writer.WriteLineAsync(cancellationToken).ConfigureAwait(false);
6264

6365
await writer.WriteLineAsync("<table>", cancellationToken).ConfigureAwait(false);
6466

6567
await writer.WriteAsync("<thead>", cancellationToken).ConfigureAwait(false);
6668
await writer.WriteAsync("<tr>", cancellationToken).ConfigureAwait(false);
67-
await table.PrintLineAsync(table.FullHeader, writer, "<th>", "</th>", cancellationToken).ConfigureAwait(false);
69+
await table.PrintLineAsync(table.FullHeader, wrappedWriter, "<th>", "</th>", cancellationToken).ConfigureAwait(false);
6870
await writer.WriteLineAsync("</tr>", cancellationToken).ConfigureAwait(false);
6971
await writer.WriteAsync("</thead>", cancellationToken).ConfigureAwait(false);
7072

@@ -80,13 +82,13 @@ private static async ValueTask PrintTableAsync(SummaryTable table, StreamOrLogge
8082
await writer.WriteLineAsync("</table>", cancellationToken).ConfigureAwait(false);
8183
}
8284

83-
private static async ValueTask PrintLineAsync(SummaryTable table, string[] line, StreamOrLoggerWriter writer, string leftDel, string rightDel, CancellationToken cancellationToken)
85+
private static async ValueTask PrintLineAsync(SummaryTable table, string[] line, CancelableStreamWriter writer, string leftDel, string rightDel, CancellationToken cancellationToken)
8486
{
8587
for (int columnIndex = 0; columnIndex < table.ColumnCount; columnIndex++)
8688
{
8789
if (table.Columns[columnIndex].NeedToShow)
8890
{
89-
await writer.WriteAsync(leftDel + line[columnIndex].HtmlEncode() + rightDel, LogKind.Statistic, cancellationToken).ConfigureAwait(false);
91+
await writer.WriteAsync(leftDel + line[columnIndex].HtmlEncode() + rightDel, cancellationToken).ConfigureAwait(false);
9092
}
9193
}
9294

0 commit comments

Comments
 (0)