Skip to content

Commit

Permalink
Add histogram support (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
bitfaster authored Nov 27, 2023
1 parent b2e2302 commit a254d73
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions Benchly.Benchmarks/Md5VsSha256.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Benchly.Benchmarks
{
[BoxPlot(Title = "Box Plot", Colors = "skyblue,slateblue")]
[BarPlot(Title = "Bar Plot", Colors = "skyblue,slateblue")]
[Histogram]
[MemoryDiagnoser, SimpleJob(RuntimeMoniker.Net60), SimpleJob(RuntimeMoniker.Net48)]
public class Md5VsSha256
{
Expand Down
2 changes: 2 additions & 0 deletions Benchly.UnitTests/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ public void Test1()
boxPlotExporter.Info.Title = "Box Plot";
var barPlotExporter = new BarPlotExporter();
barPlotExporter.Info.Title = "Bar Plot";
var histExporter = new HistogramExporter();
var files1 = boxPlotExporter.ExportToFiles(summary, NullLogger.Instance);
var files2 = barPlotExporter.ExportToFiles(summary, NullLogger.Instance);
var files3 = histExporter.ExportToFiles(summary, NullLogger.Instance);
}
}
}
14 changes: 14 additions & 0 deletions Benchly/BoxPlotAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,18 @@ public BarPlotAttribute()
exp.Info = plotInfo;
}
}

/// <summary>
/// Export a histogram plot.
/// </summary>
public sealed class HistogramAttribute : ExporterConfigBaseAttribute
{
/// <summary>
/// Initializes a new instance of the <see cref="BarPlotAttribute"/> class.
/// </summary>
public HistogramAttribute()
: base(new HistogramExporter())
{
}
}
}
47 changes: 47 additions & 0 deletions Benchly/HistogramExporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using BenchmarkDotNet.Exporters;
using BenchmarkDotNet.Loggers;
using BenchmarkDotNet.Reports;
using Plotly.NET;
using Plotly.NET.ImageExport;

namespace Benchly
{
internal class HistogramExporter : IExporter
{
public PlotInfo Info { get; set; } = new PlotInfo();

public string Name => nameof(HistogramExporter);

public IEnumerable<string> ExportToFiles(Summary summary, ILogger consoleLogger)
{
string baseName = ExporterBase.GetFileName(summary);
List<string> files = new List<string>();
foreach (var r in summary.Reports)
{
if (!r.Success)
{
continue;
}

var title = $"{r.BenchmarkCase.Descriptor.WorkloadMethodDisplayInfo} ({r.BenchmarkCase.Job.ResolvedId})";
var file = Path.Combine(summary.ResultsDirectoryPath, baseName + "-hist-" + title);

var data = r.AllMeasurements.Where(m => m.IterationMode == BenchmarkDotNet.Engines.IterationMode.Workload && m.IterationStage == BenchmarkDotNet.Engines.IterationStage.Actual).Select(m => m.GetAverageTime().Nanoseconds).ToArray();

Chart2D.Chart.Histogram<double, double, string>(X: data)
.WithoutVerticalGridlines()
.WithAxisTitles("Latency (ns)", "Frequency")
.WithLayout(title)
.SaveSVG(file, Width: 1000, Height: 600);

files.Add(file + ".svg");
}

return files;
}

public void ExportToLog(Summary summary, ILogger logger)
{
}
}
}
8 changes: 8 additions & 0 deletions Benchly/PlotExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ public static GenericChart.GenericChart WithAxisTitles(this GenericChart.Generic
return chart.WithXAxisStyle(Title.init(Font: font)).WithYAxisStyle(Title.init(yt, Font: font));
}

public static GenericChart.GenericChart WithAxisTitles(this GenericChart.GenericChart chart, string xTitle, string yTitle)
{
var font = new FSharpOption<Font>(Font.init(Size: new FSharpOption<double>(16)));
FSharpOption<string> xt = new FSharpOption<string>(xTitle);
FSharpOption<string> yt = new FSharpOption<string>(yTitle);
return chart.WithXAxisStyle(Title.init(xt, Font: font)).WithYAxisStyle(Title.init(yt, Font: font));
}

public static GenericChart.GenericChart WithoutVerticalGridlines(this GenericChart.GenericChart chart)
{
var gridColor = new FSharpOption<Color>(Color.fromKeyword(ColorKeyword.Gainsboro));
Expand Down

0 comments on commit a254d73

Please sign in to comment.