Skip to content

Commit d93772a

Browse files
authored
Enable ColumnChart PerMethod with params (#29)
1 parent b2cd0ad commit d93772a

File tree

4 files changed

+82
-6
lines changed

4 files changed

+82
-6
lines changed

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 ({JOB})", Output=OutputMode.PerJob)]
8+
[ColumnChart(Title = "Hashing ({METHOD})", Output=OutputMode.PerMethod)]
99
[Histogram]
1010
[Timeline]
1111
[MemoryDiagnoser, SimpleJob(RuntimeMoniker.Net60), SimpleJob(RuntimeMoniker.Net48)]

Benchly/ColumnChartExporter.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,29 @@ private IEnumerable<string> PerMethod(Summary summary)
3838
{
3939
var files = new List<string>();
4040

41+
if (summary.Reports[0].BenchmarkCase.HasParameters)
42+
{
43+
int paramCount = summary.Reports[0].BenchmarkCase.Parameters.Count;
44+
45+
if (paramCount == 1)
46+
{
47+
var subPlots = GetSubPlots(summary);
48+
49+
foreach (var method in summary.Reports.Select(r => r.BenchmarkCase.Descriptor.WorkloadMethodDisplayInfo).Distinct())
50+
{
51+
var title = TitleFormatter.Format(this.Info, summary, string.Join(",", summary.Reports.Select(r => r.BenchmarkCase.Job.ResolvedId).Distinct()), method);
52+
var file = Path.Combine(summary.ResultsDirectoryPath, ExporterBase.GetFileName(summary) + "-" + method + "-columnchart");
53+
var methodSubPlots = subPlots.ToPerMethod(method);
54+
ColumnChartRenderer.Render(methodSubPlots, title, file, Info.Width, Info.Height, ColorMap.GetColorList(Info));
55+
files.Add(file + ".svg");
56+
}
57+
58+
return files;
59+
}
60+
61+
return Array.Empty<string>();
62+
}
63+
4164
foreach (var report in summary.Reports)
4265
{
4366
if (!report.Success)
@@ -47,7 +70,7 @@ private IEnumerable<string> PerMethod(Summary summary)
4770

4871
int paramCount = report.BenchmarkCase.Parameters.Count;
4972

50-
var title = TitleFormatter.Format(this.Info, summary, report.BenchmarkCase.Job.ResolvedId);
73+
var title = TitleFormatter.Format(this.Info, summary, report.BenchmarkCase.Job.ResolvedId, report.BenchmarkCase.Descriptor.WorkloadMethodDisplayInfo);
5174
var fileSlug = paramCount == 0
5275
? report.BenchmarkCase.Job.ResolvedId + "-" + report.BenchmarkCase.Descriptor.WorkloadMethodDisplayInfo
5376
: report.BenchmarkCase.Job.ResolvedId + "-" + report.BenchmarkCase.Descriptor.WorkloadMethodDisplayInfo + "-" + report.BenchmarkCase.Parameters.PrintInfo;
@@ -83,7 +106,7 @@ private IEnumerable<string> PerJob(Summary summary)
83106
{
84107
var title = TitleFormatter.Format(this.Info, summary, job);
85108
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() });
109+
var jobSubPlots = subPlots.ToPerJob(job);
87110
ColumnChartRenderer.Render(jobSubPlots, title, file, Info.Width, Info.Height, ColorMap.GetColorList(Info));
88111
files.Add(file + ".svg");
89112
}

Benchly/ColumnChartRenderer.cs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
using Microsoft.FSharp.Core;
1+
using BenchmarkDotNet.Jobs;
2+
using Microsoft.FSharp.Core;
23
using Plotly.NET;
34
using Plotly.NET.ImageExport;
45
using Plotly.NET.LayoutObjects;
6+
using System.Collections.Generic;
57
using static Plotly.NET.StyleParam;
68

79
namespace Benchly
@@ -27,6 +29,57 @@ internal class SubPlot
2729
public List<TraceInfo> Traces { get; set; }
2830
}
2931

32+
internal static class SubPlotExt
33+
{
34+
internal static List<SubPlot> ToPerJob(this List<SubPlot> subPlots, string job)
35+
{
36+
return subPlots.Select(s => new SubPlot() { Title = s.Title, Traces = s.Traces.Where(t => t.TraceName == job).ToList() }).ToList();
37+
}
38+
39+
internal static List<SubPlot> ToPerMethod(this List<SubPlot> subPlots, string method)
40+
{
41+
List<SubPlot> perMethod = new List<SubPlot>();
42+
43+
foreach (var subPlot in subPlots)
44+
{
45+
var methodSubPlot = new SubPlot();
46+
47+
// suppress the per plot label
48+
methodSubPlot.Title = string.Empty;
49+
methodSubPlot.Traces = new List<TraceInfo>();
50+
51+
foreach (var t in subPlot.Traces)
52+
{
53+
54+
var trace = new TraceInfo() { TraceName = t.TraceName };
55+
56+
var values = new List<double>();
57+
58+
for (int i = 0; i < t.Values.Length; i++)
59+
{
60+
if (t.Keys[i] == method)
61+
{
62+
values.Add(t.Values[i]);
63+
}
64+
}
65+
66+
// label the x axis using the paremeter name+value
67+
trace.Values = values.ToArray();
68+
trace.Keys = Enumerable.Range(0, trace.Values.Length).Select(_ => subPlot.Title).ToArray();
69+
70+
if (trace.Values.Length > 0)
71+
{
72+
methodSubPlot.Traces.Add(trace);
73+
}
74+
}
75+
76+
perMethod.Add(methodSubPlot);
77+
}
78+
79+
return perMethod;
80+
}
81+
}
82+
3083
internal class ColumnChartRenderer
3184
{
3285
public static void Render(IEnumerable<TraceInfo> traces, string title, string file, int width, int height, bool showLegend)

Benchly/TitleFormatter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ namespace Benchly
44
{
55
internal class TitleFormatter
66
{
7-
public static string Format(PlotInfo Info, Summary summary, string currentJob)
7+
public static string Format(PlotInfo Info, Summary summary, string currentJob, string currentMethod = null)
88
{
99
if (!string.IsNullOrEmpty(Info.Title))
1010
{
1111
var title = Info.Title;
1212

13-
return title.Replace("{JOB}", currentJob);
13+
return title.Replace("{JOB}", currentJob).Replace("{METHOD}", currentMethod);
1414
}
1515

1616
return summary.Title;

0 commit comments

Comments
 (0)