Skip to content

Commit e01f08b

Browse files
author
Alex Peck
committed
normalize time units
1 parent 1923b97 commit e01f08b

File tree

3 files changed

+55
-13
lines changed

3 files changed

+55
-13
lines changed

Benchly/ColumnChartExporter.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private IEnumerable<string> PerMethod(Summary summary)
5757
var columnChartData = new TraceInfo()
5858
{
5959
Keys = new[] { report.BenchmarkCase.Descriptor.WorkloadMethodDisplayInfo },
60-
Values = new[] { report.Success ? ConvertNanosToMs(report.ResultStatistics.Mean) : 0 }
60+
Values = new[] { report.Success ? report.ResultStatistics.Mean : 0 }
6161
};
6262

6363
ColumnChartRenderer.Render(new[] { columnChartData }, title, file, Info.Width, Info.Height, false);
@@ -80,7 +80,7 @@ private IEnumerable<string> PerJob(Summary summary)
8080
{
8181
TraceName = r.BenchmarkCase.Job.ResolvedId,
8282
Keys = new[] { r.BenchmarkCase.Descriptor.WorkloadMethodDisplayInfo },
83-
Values = new[] { r.Success ? ConvertNanosToMs(r.ResultStatistics.Mean) : 0 }
83+
Values = new[] { r.Success ? r.ResultStatistics.Mean : 0 }
8484
}).GroupBy(r => r.TraceName);
8585

8686
var colors = ColorMap.GetColorList(Info);
@@ -127,7 +127,7 @@ private IEnumerable<string> NoParameterCombined(Summary summary)
127127
{
128128
job = r.BenchmarkCase.Job.ResolvedId,
129129
name = r.BenchmarkCase.Descriptor.WorkloadMethodDisplayInfo,
130-
mean = r.Success ? ConvertNanosToMs(r.ResultStatistics.Mean) : 0
130+
mean = r.Success ? r.ResultStatistics.Mean : 0
131131
})
132132
.GroupBy(r => r.job)
133133
.Select(job => new TraceInfo() { Values = job.Select(j => j.mean).ToArray(), Keys = job.Select(j => j.name).ToArray(), TraceName = job.Key });
@@ -150,7 +150,7 @@ private IEnumerable<string> OneParameterCombined(Summary summary)
150150
param = r.BenchmarkCase.Parameters.PrintInfo,
151151
job = r.BenchmarkCase.Job.ResolvedId,
152152
name = r.BenchmarkCase.Descriptor.WorkloadMethodDisplayInfo,
153-
mean = r.Success ? ConvertNanosToMs(r.ResultStatistics.Mean) : 0
153+
mean = r.Success ? r.ResultStatistics.Mean : 0
154154
})
155155
.GroupBy(r => r.param)
156156
.Select(bp => new SubPlot()
@@ -164,7 +164,7 @@ private IEnumerable<string> OneParameterCombined(Summary summary)
164164
Values = j.Select(j => j.mean).ToArray(),
165165
Keys = j.Select(j => j.name).ToArray(),
166166
}).ToList()
167-
});
167+
}).ToList();
168168

169169
var colors2 = ColorMap.GetColorList(Info);
170170
ColumnChartRenderer.Render(subPlots, title, file, Info.Width, Info.Height, colors2);
@@ -174,9 +174,9 @@ private IEnumerable<string> OneParameterCombined(Summary summary)
174174

175175
// internal measurements are in nanos
176176
// https://github.com/dotnet/BenchmarkDotNet/blob/e4d37d03c0b1ef14e7bde224970bd0fc547fd95a/src/BenchmarkDotNet/Templates/BuildPlots.R#L63-L75
177-
private static double ConvertNanosToMs(double nanos)
178-
{
179-
return nanos * 0.000001;
180-
}
177+
//private static double ConvertNanosToMs(double nanos)
178+
//{
179+
// return nanos * 0.000001;
180+
//}
181181
}
182182
}

Benchly/ColumnChartRenderer.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ internal class ColumnChartRenderer
3131
{
3232
public static void Render(IEnumerable<TraceInfo> traces, string title, string file, int width, int height, bool showLegend)
3333
{
34-
// TODO: pre-process to determine best units
34+
var timeUnit = TimeNormalization.Normalize(traces);
3535

3636
var charts = traces
3737
.Select((cd, i) => Chart2D.Chart.Column<double, string, string, double, double>(
@@ -42,15 +42,15 @@ public static void Render(IEnumerable<TraceInfo> traces, string title, string fi
4242
.WithLegendGroup(cd.TraceName, showLegend));
4343

4444
Chart.Combine(charts)
45-
.WithAxisTitles($"Latency (ms)")
45+
.WithAxisTitles($"Latency ({timeUnit})")
4646
.WithoutVerticalGridlines()
4747
.WithLayout(title)
4848
.SaveSVG(file, Width: width, Height: height);
4949
}
5050

5151
public static void Render(IEnumerable<SubPlot> subPlot, string title, string file, int width, int height, Color[] colors)
5252
{
53-
// TODO: pre-process to determine best units
53+
var timeUnit = TimeNormalization.Normalize(subPlot.SelectMany(sp => sp.Traces));
5454

5555
// make a grid with 1 row, n columns, where n is number of params
5656
// y axis only on first chart
@@ -99,7 +99,7 @@ public static void Render(IEnumerable<SubPlot> subPlot, string title, string fil
9999
.Grid<IEnumerable<GenericChart.GenericChart>>(1, subPlot.Count(), Pattern: pattern).Invoke(gridCharts)
100100
.WithAnnotations(annotations)
101101
.WithoutVerticalGridlines()
102-
.WithAxisTitles("Time (ms)")
102+
.WithAxisTitles($"Time ({timeUnit})")
103103
.WithLayout(title)
104104
.SaveSVG(file, Width: width, Height: height);
105105
}

Benchly/TimeNormalization.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+

2+
namespace Benchly
3+
{
4+
internal class TimeNormalization
5+
{
6+
public static string Normalize(IEnumerable<TraceInfo> traces)
7+
{
8+
var max = traces.SelectMany(t => t.Values).Max();
9+
10+
string timeUnit = "ns";
11+
12+
if (max > 1_000_000_000)
13+
{
14+
Reduce(traces, 0.000000001);
15+
timeUnit = "s";
16+
}
17+
else if (max > 1_000_000)
18+
{
19+
Reduce(traces, 0.000001);
20+
timeUnit = "ms";
21+
}
22+
else if (max > 1_000)
23+
{
24+
Reduce(traces, 0.001);
25+
timeUnit = "μs";
26+
}
27+
28+
return timeUnit;
29+
}
30+
31+
private static void Reduce(IEnumerable<TraceInfo> traces, double factor)
32+
{
33+
foreach (var plot in traces)
34+
{
35+
for (int i = 0; i < plot.Values.Length; i++)
36+
{
37+
plot.Values[i] *= factor;
38+
}
39+
}
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)