Skip to content

Commit b2cd0ad

Browse files
authored
Enable ColumnChart per job with params (#28)
1 parent 495b5d4 commit b2cd0ad

File tree

5 files changed

+66
-23
lines changed

5 files changed

+66
-23
lines changed

Benchly.Benchmarks/Md5VsSha256.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace Benchly.Benchmarks
66
{
77
[BoxPlot(Title = "Box Plot", Colors = "skyblue,slateblue", Height = 800)]
8-
[ColumnChart(Title = "Column Chart", Colors = "skyblue,slateblue", Height =800, Output = OutputMode.PerJob)]
8+
[ColumnChart(Title = "Column Chart ({JOB})", Colors = "skyblue,slateblue", Height =800, Output = OutputMode.PerJob)]
99
[Histogram(Width=500)]
1010
[Timeline(Width = 500)]
1111
[MemoryDiagnoser, SimpleJob(RuntimeMoniker.Net60), SimpleJob(RuntimeMoniker.Net48)]

Benchly.Benchmarks/Md5VsSha256Params.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace Benchly.Benchmarks
66
{
77
[BoxPlot(Title = "Box Plot")]
8-
[ColumnChart(Title = "Column Chart", Output=OutputMode.Combined)]
8+
[ColumnChart(Title = "Column Chart ({JOB})", Output=OutputMode.PerJob)]
99
[Histogram]
1010
[Timeline]
1111
[MemoryDiagnoser, SimpleJob(RuntimeMoniker.Net60), SimpleJob(RuntimeMoniker.Net48)]

Benchly/ColumnChartExporter.cs

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using BenchmarkDotNet.Exporters;
22
using BenchmarkDotNet.Loggers;
33
using BenchmarkDotNet.Reports;
4+
using Plotly.NET;
45

56
namespace Benchly
67
{
@@ -46,7 +47,7 @@ private IEnumerable<string> PerMethod(Summary summary)
4647

4748
int paramCount = report.BenchmarkCase.Parameters.Count;
4849

49-
var title = this.Info.Title ?? summary.Title;
50+
var title = TitleFormatter.Format(this.Info, summary, report.BenchmarkCase.Job.ResolvedId);
5051
var fileSlug = paramCount == 0
5152
? report.BenchmarkCase.Job.ResolvedId + "-" + report.BenchmarkCase.Descriptor.WorkloadMethodDisplayInfo
5253
: report.BenchmarkCase.Job.ResolvedId + "-" + report.BenchmarkCase.Descriptor.WorkloadMethodDisplayInfo + "-" + report.BenchmarkCase.Parameters.PrintInfo;
@@ -68,14 +69,31 @@ private IEnumerable<string> PerMethod(Summary summary)
6869
}
6970
private IEnumerable<string> PerJob(Summary summary)
7071
{
71-
// don't support params for now
72+
var files = new List<string>();
73+
7274
if (summary.Reports[0].BenchmarkCase.HasParameters)
7375
{
76+
int paramCount = summary.Reports[0].BenchmarkCase.Parameters.Count;
77+
78+
if (paramCount == 1)
79+
{
80+
var subPlots = GetSubPlots(summary);
81+
82+
foreach (var job in summary.Reports.Select(r => r.BenchmarkCase.Job.ResolvedId).Distinct())
83+
{
84+
var title = TitleFormatter.Format(this.Info, summary, job);
85+
var file = Path.Combine(summary.ResultsDirectoryPath, ExporterBase.GetFileName(summary) + "-" + job + "-columnchart");
86+
var jobSubPlots = subPlots.Select(s => new SubPlot() { Title = s.Title, Traces = s.Traces.Where(t => t.TraceName == job).ToList() });
87+
ColumnChartRenderer.Render(jobSubPlots, title, file, Info.Width, Info.Height, ColorMap.GetColorList(Info));
88+
files.Add(file + ".svg");
89+
}
90+
91+
return files;
92+
}
93+
7494
return Array.Empty<string>();
7595
}
7696

77-
var files = new List<string>();
78-
7997
var charts = summary.Reports.Select(r => new TraceInfo()
8098
{
8199
TraceName = r.BenchmarkCase.Job.ResolvedId,
@@ -86,13 +104,13 @@ private IEnumerable<string> PerJob(Summary summary)
86104
var colors = ColorMap.GetColorList(Info);
87105

88106
// make 1 chart per column so that we can color by bar index. Legend is disabled since it is not needed.
89-
foreach (var chartData in charts)
107+
foreach (var chart in charts)
90108
{
91-
var title = this.Info.Title ?? summary.Title;
92-
var file = Path.Combine(summary.ResultsDirectoryPath, ExporterBase.GetFileName(summary) + "-" + chartData.Key + "-columnchart");
109+
var title = TitleFormatter.Format(this.Info, summary, chart.Key);
110+
var file = Path.Combine(summary.ResultsDirectoryPath, ExporterBase.GetFileName(summary) + "-" + chart.Key + "-columnchart");
93111

94-
ColorMap.Fill(chartData, colors);
95-
ColumnChartRenderer.Render(chartData, title, file, Info.Width, Info.Height, false);
112+
ColorMap.Fill(chart, colors);
113+
ColumnChartRenderer.Render(chart, title, file, Info.Width, Info.Height, false);
96114

97115
files.Add(file + ".svg");
98116
}
@@ -120,7 +138,7 @@ private IEnumerable<string> Combined(Summary summary)
120138

121139
private IEnumerable<string> NoParameterCombined(Summary summary)
122140
{
123-
var title = this.Info.Title ?? summary.Title;
141+
var title = TitleFormatter.Format(this.Info, summary, string.Join(",", summary.Reports.Select(r => r.BenchmarkCase.Job.ResolvedId).Distinct()));
124142
var file = Path.Combine(summary.ResultsDirectoryPath, ExporterBase.GetFileName(summary) + "-columnchart");
125143

126144
var charts = summary.Reports.Select(r => new
@@ -141,10 +159,19 @@ private IEnumerable<string> NoParameterCombined(Summary summary)
141159

142160
private IEnumerable<string> OneParameterCombined(Summary summary)
143161
{
144-
var title = this.Info.Title ?? summary.Title;
162+
var title = TitleFormatter.Format(this.Info, summary, string.Join(",", summary.Reports.Select(r => r.BenchmarkCase.Job.ResolvedId).Distinct()));
145163
var file = Path.Combine(summary.ResultsDirectoryPath, ExporterBase.GetFileName(summary) + "-columnchart");
146164

147-
var subPlots = summary.Reports
165+
var subPlots = GetSubPlots(summary);
166+
var colors = ColorMap.GetColorList(Info);
167+
ColumnChartRenderer.Render(subPlots, title, file, Info.Width, Info.Height, colors);
168+
169+
return new[] { file + ".svg" };
170+
}
171+
172+
private List<SubPlot> GetSubPlots(Summary summary)
173+
{
174+
return summary.Reports
148175
.Select(r => new
149176
{
150177
param = r.BenchmarkCase.Parameters.PrintInfo,
@@ -158,18 +185,13 @@ private IEnumerable<string> OneParameterCombined(Summary summary)
158185
Title = bp.Key,
159186
Traces = bp
160187
.GroupBy(p => p.job)
161-
.Select(j => new TraceInfo()
162-
{
188+
.Select(j => new TraceInfo()
189+
{
163190
TraceName = j.Key,
164-
Values = j.Select(j => j.mean).ToArray(),
191+
Values = j.Select(j => j.mean).ToArray(),
165192
Keys = j.Select(j => j.name).ToArray(),
166193
}).ToList()
167194
}).ToList();
168-
169-
var colors = ColorMap.GetColorList(Info);
170-
ColumnChartRenderer.Render(subPlots, title, file, Info.Width, Info.Height, colors);
171-
172-
return new[] { file + ".svg" };
173195
}
174196
}
175197
}

Benchly/ColumnChartRenderer.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public static void Render(IEnumerable<SubPlot> subPlot, string title, string fil
5252
{
5353
var timeUnit = TimeNormalization.Normalize(subPlot.SelectMany(sp => sp.Traces));
5454

55+
bool singleJob = subPlot.SelectMany(sp => sp.Traces).Select(t => t.TraceName).Distinct().Count() == 1;
56+
5557
// make a grid with 1 row, n columns, where n is number of params
5658
// y axis only on first chart
5759
var gridCharts = new List<GenericChart.GenericChart>();
@@ -68,7 +70,7 @@ public static void Render(IEnumerable<SubPlot> subPlot, string title, string fil
6870
{
6971
var chart2 = Chart2D.Chart
7072
.Column<double, string, string, double, double>(trace.Values, trace.Keys, Name: trace.TraceName, MarkerColor: trace.MarkerColor)
71-
.WithLegendGroup(trace.TraceName, paramCount == 0);
73+
.WithLegendGroup(trace.TraceName, paramCount == 0 && !singleJob);
7274
charts.Add(chart2);
7375
}
7476

Benchly/TitleFormatter.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using BenchmarkDotNet.Reports;
2+
3+
namespace Benchly
4+
{
5+
internal class TitleFormatter
6+
{
7+
public static string Format(PlotInfo Info, Summary summary, string currentJob)
8+
{
9+
if (!string.IsNullOrEmpty(Info.Title))
10+
{
11+
var title = Info.Title;
12+
13+
return title.Replace("{JOB}", currentJob);
14+
}
15+
16+
return summary.Title;
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)