internal SummaryTable(Summary summary, ISummaryStyle style = null) { Summary = summary; if (summary.HasCriticalValidationErrors) { Columns = Array.Empty <SummaryTableColumn>(); ColumnCount = 0; FullHeader = Array.Empty <string>(); FullContent = Array.Empty <string[]>(); FullContentStartOfGroup = Array.Empty <bool>(); FullContentWithHeader = Array.Empty <string[]>(); IsDefault = Array.Empty <bool>(); return; } // Ensure we have all required data for styling style = style ?? SummaryStyle.Default; if (style.TimeUnit == null) { style = style.WithTimeUnit(TimeUnit.GetBestTimeUnit(summary.Reports.Where(r => r.ResultStatistics != null).Select(r => r.ResultStatistics.Mean).ToArray())); } if (style.SizeUnit == null) { style = style.WithSizeUnit(SizeUnit.GetBestSizeUnit(summary.Reports.Select(r => r.GcStats.BytesAllocatedPerOperation).ToArray())); } var columns = summary.GetColumns(); ColumnCount = columns.Length; FullHeader = columns.Select(c => c.GetColumnTitle(style)).ToArray(); var orderProvider = summary.Config.GetOrderProvider() ?? DefaultOrderProvider.Instance; FullContent = summary.Reports.Select(r => columns.Select(c => c.GetValue(summary, r.Benchmark, style)).ToArray()).ToArray(); IsDefault = columns.Select(c => summary.Reports.All(r => c.IsDefault(summary, r.Benchmark))).ToArray(); var groupKeys = summary.Benchmarks.Select(b => orderProvider.GetGroupKey(b, summary)).ToArray(); FullContentStartOfGroup = new bool[summary.Reports.Length]; if (groupKeys.Distinct().Count() > 1 && FullContentStartOfGroup.Length > 0) { FullContentStartOfGroup[0] = true; for (int i = 1; i < summary.Reports.Length; i++) { FullContentStartOfGroup[i] = groupKeys[i] != groupKeys[i - 1]; } } var full = new List <string[]> { FullHeader }; full.AddRange(FullContent); FullContentWithHeader = full.ToArray(); Columns = Enumerable.Range(0, columns.Length).Select(i => new SummaryTableColumn(this, i, columns[i])).ToArray(); EffectiveSummaryStyle = style; }
public static string ToSizeStr(this long value, SizeUnit unit = null, int unitNameWidth = 1, bool showUnit = true) { unit = unit ?? SizeUnit.GetBestSizeUnit(value); var unitValue = SizeUnit.Convert(value, SizeUnit.B, unit); if (showUnit) { string unitName = unit.Name.PadLeft(unitNameWidth); return(string.Format(HostEnvironmentInfo.MainCultureInfo, "{0:0.##} {1}", unitValue, unitName)); } return(string.Format(HostEnvironmentInfo.MainCultureInfo, "{0:0.##}", unitValue)); }
private static void DisplayDiff(IEnumerable <Benchmark[]> allBenchmarks, IEnumerable <string> allNames) { // Use the first job's benchmarks as the reference for the rows: var firstBenchmarks = allBenchmarks.FirstOrDefault(); if (firstBenchmarks == null || firstBenchmarks.Length < 1) { return; } var summaries = new Dictionary <string, List <BenchmarkSummary> >(); foreach (var benchmark in firstBenchmarks) { summaries[benchmark.FullName] = new List <BenchmarkSummary>(); } foreach (var benchmarks in allBenchmarks) { foreach (var benchmark in benchmarks) { summaries[benchmark.FullName].Add(new BenchmarkSummary() { Name = benchmark.FullName, MeanNanoseconds = benchmark.Statistics.Mean, StandardErrorNanoseconds = benchmark.Statistics.StandardError, StandardDeviationNanoseconds = benchmark.Statistics.StandardDeviation, MedianNanoseconds = benchmark.Statistics.Median, Gen0 = benchmark.Memory?.Gen0Collections ?? 0, Gen1 = benchmark.Memory?.Gen1Collections ?? 0, Gen2 = benchmark.Memory?.Gen2Collections ?? 0, AllocatedBytes = benchmark.Memory?.BytesAllocatedPerOperation ?? 0 }); } } // Simplfy the benchmarks' names where possible to remove prefixes that // are all the same to reduce the width of the first column of the table // to the shortest unique string required across all benchmarks. var nameSegments = summaries.Keys.ToDictionary(key => key, value => value.Split('.')); while (true) { var areAllFirstSegmentsTheSame = nameSegments.Values .Select(segments => segments[0]) .Distinct() .Count() == 1; if (!areAllFirstSegmentsTheSame) { // The names cannot be simplified further break; } foreach (var pair in nameSegments) { nameSegments[pair.Key] = pair.Value.Skip(1).ToArray(); } } // Map the full names to their simplified name var simplifiedNames = nameSegments.ToDictionary(key => key.Key, value => string.Join(".", value.Value)); var anyAllocations = summaries.Values .SelectMany(list => list) .Select(summary => summary.AllocatedBytes) .Any(allocatedBytes => allocatedBytes > 0); // Name + baseline mean (firstBenchmarks) + (other benchmarks' mean * 2 (value + ratio)) + // baseline allocations + (other benchmarks' mean * 2 (value + ratio)) var otherCount = allNames.Count() - 1; var table = new ResultTable(1 + 1 + (anyAllocations ? 1 : 0) + (otherCount * (anyAllocations ? 4 : 2))); var firstName = allNames.First(); table.Headers.Add("benchmark"); table.Headers.Add($"mean ({firstName})"); foreach (var name in allNames.Skip(1)) { table.Headers.Add($"mean ({name})"); table.Headers.Add("ratio"); } if (anyAllocations) { table.Headers.Add($"allocated ({firstName})"); foreach (var name in allNames.Skip(1)) { table.Headers.Add($"allocated ({name})"); table.Headers.Add("ratio"); } } foreach (var benchmark in summaries.OrderBy(kvp => kvp.Key).Select(kvp => kvp.Value)) { var firstBenchmark = benchmark.First(); var simplifiedName = simplifiedNames[firstBenchmark.Name]; var benchmarks = summaries[firstBenchmark.Name]; var row = table.AddRow(); var cell = new Cell(); cell.Elements.Add(new CellElement() { Text = simplifiedName, Alignment = CellTextAlignment.Left }); row.Add(cell); AddCells(row, summary => summary.MeanNanoseconds, UnitType.Time, benchmarks); if (anyAllocations) { AddCells(row, summary => summary.AllocatedBytes, UnitType.Size, benchmarks); } } Console.WriteLine("```md"); // Format as a GitHub-flavored Markdown table table.Render(Console.Out); Console.WriteLine("```"); void AddCells( List <Cell> row, Func <BenchmarkSummary, object> valueFactory, UnitType unitType, List <BenchmarkSummary> summaries) { var rawValues = summaries .Select(summary => valueFactory(summary)) .Select(value => Convert.ToDouble(value)) .ToArray(); var precision = PrecisionHelper.GetPrecision(rawValues); var sizeUnit = unitType == UnitType.Size ? SizeUnit.GetBestSizeUnit(rawValues) : null; var timeUnit = unitType == UnitType.Time ? TimeUnit.GetBestTimeUnit(rawValues) : null; var units = unitType switch { UnitType.Size => sizeUnit.Name, UnitType.Time => timeUnit.Name, _ => string.Empty }; var baseline = summaries[0]; for (var i = 0; i < summaries.Count; i++) { var measure = rawValues[i]; var previous = rawValues[0]; var ratio = measure == 0 ? 0 : measure / previous; var formattedValue = unitType switch { UnitType.Size => new SizeValue((long)measure).ToString(sizeUnit), UnitType.Time => new TimeInterval(measure).ToString(timeUnit, precision), _ => measure.ToString("N" + precision, CultureInfo.InvariantCulture) }; var cell = new Cell(); cell.Elements.Add(new CellElement { Text = $"{formattedValue} {units}", Alignment = CellTextAlignment.Right }); row.Add(cell); // Don't render the ratio on baseline benchmark if (summaries[i] != baseline) { row.Add(cell = new Cell()); if (measure != 0) { cell.Elements.Add(new CellElement { Text = ratio.ToString("N2", CultureInfo.InvariantCulture), Alignment = CellTextAlignment.Right }); } } } } }
internal SummaryTable(Summary summary, SummaryStyle style = null) { Summary = summary; if (summary.HasCriticalValidationErrors) { Columns = Array.Empty <SummaryTableColumn>(); ColumnCount = 0; FullHeader = Array.Empty <string>(); FullContent = Array.Empty <string[]>(); FullContentStartOfHighlightGroup = Array.Empty <bool>(); FullContentWithHeader = Array.Empty <string[]>(); IsDefault = Array.Empty <bool>(); return; } // Ensure we have all required data for styling style = style ?? summary.Style ?? SummaryStyle.Default; if (style.TimeUnit == null) { style = style.WithTimeUnit(TimeUnit.GetBestTimeUnit(summary.Reports.Where(r => r.ResultStatistics != null).Select(r => r.ResultStatistics.Mean) .ToArray())); } if (style.SizeUnit == null) { style = style.WithSizeUnit(SizeUnit.GetBestSizeUnit(summary.Reports.Select(r => r.GcStats.GetBytesAllocatedPerOperation(r.BenchmarkCase)).ToArray())); } var columns = summary.GetColumns(); ColumnCount = columns.Length; FullHeader = columns.Select(c => c.GetColumnTitle(style)).ToArray(); FullContent = summary.Reports.Select(r => columns.Select(c => c.GetValue(summary, r.BenchmarkCase, style)).ToArray()).ToArray(); IsDefault = columns.Select(c => summary.Reports.All(r => c.IsDefault(summary, r.BenchmarkCase))).ToArray(); var highlightGroupKeys = summary.BenchmarksCases.Select(b => b.Config.Orderer.GetHighlightGroupKey(b)).ToArray(); FullContentStartOfHighlightGroup = new bool[summary.Reports.Length]; if (highlightGroupKeys.Distinct().Count() > 1 && FullContentStartOfHighlightGroup.Length > 0) { FullContentStartOfHighlightGroup[0] = true; for (int i = 1; i < summary.Reports.Length; i++) { FullContentStartOfHighlightGroup[i] = highlightGroupKeys[i] != highlightGroupKeys[i - 1]; } } var logicalGroupKeys = summary.BenchmarksCases .Select(b => b.Config.Orderer.GetLogicalGroupKey(summary.BenchmarksCases, b)) .ToArray(); FullContentStartOfLogicalGroup = new bool[summary.Reports.Length]; if (logicalGroupKeys.Distinct().Count() > 1 && FullContentStartOfLogicalGroup.Length > 0) { FullContentStartOfLogicalGroup[0] = true; for (int i = 1; i < summary.Reports.Length; i++) { FullContentStartOfLogicalGroup[i] = logicalGroupKeys[i] != logicalGroupKeys[i - 1]; } } SeparateLogicalGroups = summary.Orderer.SeparateLogicalGroups; var full = new List <string[]> { FullHeader }; full.AddRange(FullContent); FullContentWithHeader = full.ToArray(); Columns = new SummaryTableColumn[columns.Length]; for (int i = 0; i < columns.Length; i++) { var column = columns[i]; bool hide = summary.ColumnHidingRules.Any(rule => rule.NeedToHide(column)); Columns[i] = new SummaryTableColumn(this, i, column, hide); } EffectiveSummaryStyle = style; }