Skip to content

Commit

Permalink
Support custom size (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
bitfaster authored Nov 28, 2023
1 parent 587c247 commit a9ca875
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 18 deletions.
8 changes: 4 additions & 4 deletions Benchly.Benchmarks/Md5VsSha256.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Benchly.Benchmarks
{
[BoxPlot(Title = "Box Plot", Colors = "skyblue,slateblue")]
[ColumnChart(Title = "Column Chart", Colors = "skyblue,slateblue")]
[Histogram]
[Timeline]
[BoxPlot(Title = "Box Plot", Colors = "skyblue,slateblue", Height = 800)]
[ColumnChart(Title = "Column Chart", Colors = "skyblue,slateblue", Height =800)]
[Histogram(Width=500)]
[Timeline(Width = 500)]
[MemoryDiagnoser, SimpleJob(RuntimeMoniker.Net60), SimpleJob(RuntimeMoniker.Net48)]
public class Md5VsSha256
{
Expand Down
50 changes: 41 additions & 9 deletions Benchly/BoxPlotAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Exporters;

namespace Benchly
{
/// <summary>
/// Export a box plot.
/// </summary>
public sealed class BoxPlotAttribute : ExporterConfigBaseAttribute
public sealed class BoxPlotAttribute : PlotBaseAttribute
{
private readonly PlotInfo plotInfo = new PlotInfo();

/// <summary>
/// Gets or sets the title of the plot.
/// </summary>
Expand Down Expand Up @@ -39,12 +38,10 @@ public BoxPlotAttribute()
}

/// <summary>
/// Export a bar plot.
/// Export a column chart.
/// </summary>
public sealed class ColumnChartAttribute : ExporterConfigBaseAttribute
public sealed class ColumnChartAttribute : PlotBaseAttribute
{
private readonly PlotInfo plotInfo = new PlotInfo();

/// <summary>
/// Gets or sets the title of the plot.
/// </summary>
Expand Down Expand Up @@ -77,27 +74,62 @@ public ColumnChartAttribute()
/// <summary>
/// Export a histogram plot.
/// </summary>
public sealed class HistogramAttribute : ExporterConfigBaseAttribute
public sealed class HistogramAttribute : PlotBaseAttribute
{
/// <summary>
/// Initializes a new instance of the <see cref="HistogramAttribute"/> class.
/// </summary>
public HistogramAttribute()
: base(new HistogramExporter())
{
var exp = Config.GetExporters().OfType<HistogramExporter>().Single();
exp.Info = plotInfo;
}
}

/// <summary>
/// Export a histogram plot.
/// </summary>
public sealed class TimelineAttribute : ExporterConfigBaseAttribute
public sealed class TimelineAttribute : PlotBaseAttribute
{
/// <summary>
/// Initializes a new instance of the <see cref="TimelineAttribute"/> class.
/// </summary>
public TimelineAttribute()
: base(new TimelineExporter())
{
var exp = Config.GetExporters().OfType<TimelineExporter>().Single();
exp.Info = plotInfo;
}
}

/// <summary>
/// Base class for plot attributes.
/// </summary>
public abstract class PlotBaseAttribute : ExporterConfigBaseAttribute
{
internal readonly PlotInfo plotInfo = new PlotInfo();

/// <summary>
/// Gets or sets the width of the plot.
/// </summary>
public int Width
{
get => plotInfo.Width;
set => plotInfo.Width = value;
}

/// <summary>
/// Gets or sets the height of the plot.
/// </summary>
public int Height
{
get => plotInfo.Height;
set => plotInfo.Height = value;
}

internal PlotBaseAttribute(IExporter exporter)
: base(exporter)
{
}
}
Expand Down
2 changes: 1 addition & 1 deletion Benchly/BoxPlotExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public IEnumerable<string> ExportToFiles(Summary summary, ILogger consoleLogger)
.WithAxisTitles($"Time ({timeUnit})")
.WithLayout(title)
.WithGroupBox()
.SaveSVG(file, Width: 1000, Height: 600);
.SaveSVG(file, Width: Info.Width, Height: Info.Height);

return new[] { file + ".svg" };
}
Expand Down
4 changes: 2 additions & 2 deletions Benchly/ColumnChartExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private IEnumerable<string> NoParameter(Summary summary)
.WithoutVerticalGridlines()
.WithLayout(title);

chart.SaveSVG(file, Width: 1000, Height: 600);
chart.SaveSVG(file, Width: Info.Width, Height: Info.Height);

return new[] { file + ".svg" };
}
Expand Down Expand Up @@ -137,7 +137,7 @@ private IEnumerable<string> OneParameter(Summary summary)
.WithoutVerticalGridlines()
.WithAxisTitles("Time (ms)")
.WithLayout(title)
.SaveSVG(file, Width: 1000, Height: 600);
.SaveSVG(file, Width: Info.Width, Height: Info.Height);

return new[] { file + ".svg" };
}
Expand Down
4 changes: 3 additions & 1 deletion Benchly/HistogramExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ 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)
Expand All @@ -30,7 +32,7 @@ public IEnumerable<string> ExportToFiles(Summary summary, ILogger consoleLogger)
.WithoutVerticalGridlines()
.WithAxisTitles("Latency (ns)", "Frequency")
.WithLayout(title)
.SaveSVG(file, Width: 1000, Height: 600);
.SaveSVG(file, Width: Info.Width, Height: Info.Height);

files.Add(file + ".svg");
}
Expand Down
4 changes: 4 additions & 0 deletions Benchly/PlotInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ internal class PlotInfo

public string Colors { get; set; }

public int Width { get; set; } = 1000;

public int Height { get; set; } = 600;

internal Color[] GetColors()
{
if (string.IsNullOrEmpty(Colors))
Expand Down
2 changes: 1 addition & 1 deletion Benchly/TimelineExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public IEnumerable<string> ExportToFiles(Summary summary, ILogger consoleLogger)
.WithoutVerticalGridlines()
.WithAxisTitles("Iteration", "Latency (ns)")
.WithLayout(title)
.SaveSVG(file, Width: 1000, Height: 600);
.SaveSVG(file, Width: Info.Width, Height: Info.Height);

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

0 comments on commit a9ca875

Please sign in to comment.