-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathCustomJsonExporter.cs
More file actions
167 lines (143 loc) · 7.99 KB
/
CustomJsonExporter.cs
File metadata and controls
167 lines (143 loc) · 7.99 KB
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
namespace Rules.Framework.BenchmarkTests.Exporters.Json
{
using System.Collections.Generic;
using BenchmarkDotNet.Exporters;
using BenchmarkDotNet.Loggers;
using BenchmarkDotNet.Reports;
using Newtonsoft.Json;
internal class CustomJsonExporter : ExporterBase
{
public CustomJsonExporter(bool indentJson)
{
this.IndentJson = indentJson;
}
public static IExporter Compressed => new CustomJsonExporter(indentJson: false);
public static IExporter Default => Indented;
public static IExporter Indented => new CustomJsonExporter(indentJson: true);
protected override string FileExtension => "json";
private bool IndentJson { get; }
public override void ExportToLog(Summary summary, ILogger logger)
{
var report = CustomJsonExporter.CreateReport(summary);
var settings = new JsonSerializerSettings
{
Formatting = Formatting.None
};
if (this.IndentJson)
{
settings.Formatting = Formatting.Indented;
}
string jsonText = JsonConvert.SerializeObject(report, settings);
logger.WriteLine(jsonText);
}
private static decimal CalculateRate(decimal baselineValue, decimal compareValue)
=> ((baselineValue - compareValue) / baselineValue) * 100;
private static BenchmarkStatisticsComparisonItem CreateBenchmarkStatisticsComparisonItem(BenchmarkStatisticsItem? baselineStatisticsItem, BenchmarkStatisticsItem? nonBaselineStatisticsItem) => new BenchmarkStatisticsComparisonItem
{
AllocatedMemoryRate = new BenchmarkStatisticsValue
{
Format = "0.##",
Unit = "%",
Value = CalculateRate(
baselineStatisticsItem.AllocatedMemory?.Value ?? 0,
nonBaselineStatisticsItem.AllocatedMemory?.Value ?? 0),
},
BaselineAllocatedMemory = baselineStatisticsItem.AllocatedMemory,
BaselineMeanTimeTaken = baselineStatisticsItem.MeanTimeTaken,
BaselineParameters = baselineStatisticsItem.Parameters,
CompareAllocatedMemory = nonBaselineStatisticsItem.AllocatedMemory,
CompareMeanTimeTaken = nonBaselineStatisticsItem.MeanTimeTaken,
CompareParameters = nonBaselineStatisticsItem.Parameters,
Key = baselineStatisticsItem.Key,
MeanTimeTakenCompareRate = new BenchmarkStatisticsValue
{
Format = "0.##",
Unit = "%",
Value = CalculateRate(
baselineStatisticsItem.MeanTimeTaken?.Value ?? 0,
nonBaselineStatisticsItem.MeanTimeTaken?.Value ?? 0),
}
};
private static BenchmarkStatisticsItem CreateBenchmarkStatisticsItem(SummaryTable.SummaryTableColumn baselineClassifierColumn, ref int current, BenchmarkDotNet.Reports.BenchmarkReport benchmarkReport)
{
var allocatedMemoryMetric = benchmarkReport.Metrics.FirstOrDefault(m => m.Key == "Allocated Memory");
var branchInstructionsMetric = benchmarkReport.Metrics.FirstOrDefault(m => m.Key == "BranchInstructions");
var branchMispredictionsMetric = benchmarkReport.Metrics.FirstOrDefault(m => m.Key == "BranchMispredictions");
var gen0CollectsMetric = benchmarkReport.Metrics.FirstOrDefault(m => m.Key == "Gen0Collects");
var statisticsItem = new BenchmarkStatisticsItem
{
AllocatedMemory = allocatedMemoryMetric.Key is null ? null : CreateBenchmarkStatisticsValue(allocatedMemoryMetric),
Baseline = baselineClassifierColumn.Content[current++],
BranchInstructionsPerOp =
branchInstructionsMetric.Key is null ? null : CreateBenchmarkStatisticsValue(branchInstructionsMetric),
BranchMispredictionsPerOp =
branchMispredictionsMetric.Key is null ? null : CreateBenchmarkStatisticsValue(branchMispredictionsMetric),
Gen0Collects = gen0CollectsMetric.Key is null ? null : CreateBenchmarkStatisticsValue(gen0CollectsMetric),
Key = $"{benchmarkReport.BenchmarkCase.Descriptor.Type.Name}.{benchmarkReport.BenchmarkCase.Descriptor.WorkloadMethod.Name}",
MeanTimeTaken = new BenchmarkStatisticsValue
{
Format = "N0",
Unit = "ns",
Value = Convert.ToDecimal(benchmarkReport.ResultStatistics?.Mean ?? 0),
},
Parameters = benchmarkReport.BenchmarkCase.Parameters.DisplayInfo,
StandardError = new BenchmarkStatisticsValue
{
Format = "N0",
Unit = "ns",
Value = Convert.ToDecimal(benchmarkReport.ResultStatistics?.StandardError ?? 0),
},
};
return statisticsItem;
}
private static BenchmarkStatisticsValue CreateBenchmarkStatisticsValue(KeyValuePair<string, Metric> allocatedMemoryMetric) => new BenchmarkStatisticsValue
{
Format = allocatedMemoryMetric.Value.Descriptor.NumberFormat,
Unit = allocatedMemoryMetric.Value.Descriptor.Unit,
Value = Convert.ToDecimal(allocatedMemoryMetric.Value.Value),
};
private static Environment CreateEnvironment(Summary summary) => new Environment
{
Architecture = summary.HostEnvironmentInfo.Architecture,
BenchmarkDotNetCaption = "BenchmarkDotNet",
BenchmarkDotNetVersion = summary.HostEnvironmentInfo.BenchmarkDotNetVersion,
BuildConfiguration = summary.HostEnvironmentInfo.Configuration,
DotNetCliVersion = summary.HostEnvironmentInfo.DotNetSdkVersion.Value,
DotNetRuntimeVersion = summary.HostEnvironmentInfo.RuntimeVersion,
LogicalCoreCount = summary.HostEnvironmentInfo.CpuInfo.Value.LogicalCoreCount.GetValueOrDefault(0),
PhysicalCoreCount = summary.HostEnvironmentInfo.CpuInfo.Value.PhysicalCoreCount.GetValueOrDefault(0),
ProcessorName = summary.HostEnvironmentInfo.CpuInfo.Value.ProcessorName,
};
private static BenchmarkReport CreateReport(Summary summary)
{
var report = new BenchmarkReport
{
Date = DateTime.UtcNow,
Environment = CreateEnvironment(summary),
Title = summary.Title,
};
var statistics = new List<BenchmarkStatisticsItem>(summary.Reports.Length);
var baselineClassifierColumn = summary.Table.Columns.First(c => c.OriginalColumn.ColumnName == "Baseline");
var current = 0;
foreach (var benchmarkReport in summary.Reports)
{
var statisticsItem = CreateBenchmarkStatisticsItem(baselineClassifierColumn, ref current, benchmarkReport);
statistics.Add(statisticsItem);
}
report.Statistics = statistics;
var baselineStatisticsItems = statistics.Where(i => string.Equals(i.Baseline, "Yes"));
var nonBaselineStatisticsItems = statistics.Where(i => string.Equals(i.Baseline, "No"));
var statisticsComparison = new List<BenchmarkStatisticsComparisonItem>();
foreach (var baselineStatisticsItem in baselineStatisticsItems)
{
foreach (var nonBaselineStatisticsItem in nonBaselineStatisticsItems.Where(i => string.Equals(i.Key, baselineStatisticsItem.Key)))
{
var statisticsComparisonItem = CreateBenchmarkStatisticsComparisonItem(baselineStatisticsItem, nonBaselineStatisticsItem);
statisticsComparison.Add(statisticsComparisonItem);
}
}
report.StatisticsComparison = statisticsComparison;
return report;
}
}
}