From bdb8d438db5a2b6ad32923d2433acbcf2362ba97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Fri, 1 May 2026 15:47:27 +0200 Subject: [PATCH] refactor: enhance benchmark table display names --- Pipeline/Build.Benchmarks.cs | 147 +++++++++++++++++++++++++++++------ 1 file changed, 123 insertions(+), 24 deletions(-) diff --git a/Pipeline/Build.Benchmarks.cs b/Pipeline/Build.Benchmarks.cs index 6445c054..82b88078 100644 --- a/Pipeline/Build.Benchmarks.cs +++ b/Pipeline/Build.Benchmarks.cs @@ -185,10 +185,16 @@ string CreateBenchmarkCommentBody(string[] files, string baselineResultsDirector sb.AppendLine("
"); sb.AppendLine("Details"); BenchmarkTableParser parser = new(columnsToRemove); + List tableBuffer = new(); + int tableMeanIndex = -1; foreach (string line in lines) { if (line.StartsWith("```")) { + FlushTableBuffer(sb, tableBuffer, tableMeanIndex, baselineRows, injectBaseline, + ref anyBaselineInjected); + tableMeanIndex = -1; + count++; if (count == 1) { @@ -205,39 +211,24 @@ string CreateBenchmarkCommentBody(string[] files, string baselineResultsDirector continue; } - if (!parser.TryConsume(line, out _, out string filteredLine, out string[] filteredTokens)) + if (!parser.TryConsume(line, out int rowIndex, out _, out string[] filteredTokens)) { + FlushTableBuffer(sb, tableBuffer, tableMeanIndex, baselineRows, injectBaseline, + ref anyBaselineInjected); + tableMeanIndex = -1; sb.AppendLine(line); continue; } - bool isMockolateRow = filteredLine.Contains("_Mockolate", StringComparison.OrdinalIgnoreCase); - - if (isMockolateRow && injectBaseline && parser.MeanIndex > 0) + if (rowIndex == 0) { - string key = string.Join("|", filteredTokens.Take(parser.MeanIndex)); - if (baselineRows.TryGetValue(key, out string[] baselineTokens) && baselineTokens.Length > 0) - { - string[] modifiedBaseline = new string[baselineTokens.Length]; - for (int i = 0; i < baselineTokens.Length; i++) - { - string content = i == 0 ? "baseline*" : baselineTokens[i]; - modifiedBaseline[i] = string.IsNullOrWhiteSpace(content) ? content : $"_{content}_"; - } - - sb.AppendLine(JoinTokens(modifiedBaseline)); - anyBaselineInjected = true; - } + tableMeanIndex = parser.MeanIndex; } - if (isMockolateRow) - { - MakeLineBold(sb, filteredLine); - continue; - } - - sb.AppendLine(filteredLine); + tableBuffer.Add(new TableRow(rowIndex, filteredTokens)); } + + FlushTableBuffer(sb, tableBuffer, tableMeanIndex, baselineRows, injectBaseline, ref anyBaselineInjected); } if (anyBaselineInjected) @@ -251,6 +242,112 @@ string CreateBenchmarkCommentBody(string[] files, string baselineResultsDirector return body; } + static void FlushTableBuffer(StringBuilder sb, List tableBuffer, int meanIndex, + Dictionary baselineRows, bool injectBaseline, ref bool anyBaselineInjected) + { + if (tableBuffer.Count == 0) + { + return; + } + + string commonPrefix = FindCommonRowPrefix(tableBuffer); + + foreach (TableRow row in tableBuffer) + { + string[] displayTokens = ApplyHeaderAndPrefixStripping(row, commonPrefix); + string displayLine = JoinTokens(displayTokens); + + bool isMockolateRow = row.RowIndex >= BenchmarkTableParser.DataRowStartIndex + && row.Tokens.Length > 0 + && row.Tokens[0].Contains("_Mockolate", StringComparison.OrdinalIgnoreCase); + + if (isMockolateRow && injectBaseline && meanIndex > 0) + { + string key = string.Join("|", row.Tokens.Take(meanIndex)); + if (baselineRows.TryGetValue(key, out string[] baselineTokens) && baselineTokens.Length > 0) + { + string[] modifiedBaseline = new string[baselineTokens.Length]; + for (int i = 0; i < baselineTokens.Length; i++) + { + string content = i == 0 ? "baseline*" : baselineTokens[i]; + modifiedBaseline[i] = string.IsNullOrWhiteSpace(content) ? content : $"_{content}_"; + } + + sb.AppendLine(JoinTokens(modifiedBaseline)); + anyBaselineInjected = true; + } + } + + if (isMockolateRow) + { + MakeLineBold(sb, displayLine); + } + else + { + sb.AppendLine(displayLine); + } + } + + tableBuffer.Clear(); + } + + static string[] ApplyHeaderAndPrefixStripping(TableRow row, string commonPrefix) + { + if (commonPrefix == null || row.Tokens.Length == 0) + { + return row.Tokens; + } + + string[] result = (string[])row.Tokens.Clone(); + if (row.RowIndex == 0) + { + result[0] = commonPrefix.TrimEnd('_'); + } + else if (row.RowIndex >= BenchmarkTableParser.DataRowStartIndex + && result[0].StartsWith(commonPrefix, StringComparison.Ordinal)) + { + result[0] = result[0].Substring(commonPrefix.Length); + } + + return result; + } + + static string FindCommonRowPrefix(List rows) + { + string prefix = null; + foreach (TableRow row in rows) + { + if (row.RowIndex < BenchmarkTableParser.DataRowStartIndex) + { + continue; + } + + if (row.Tokens.Length == 0) + { + return null; + } + + string name = row.Tokens[0]; + int underscoreIdx = name.IndexOf('_'); + if (underscoreIdx <= 0) + { + return null; + } + + string thisPrefix = name.Substring(0, underscoreIdx + 1); + if (prefix == null) + { + prefix = thisPrefix; + } + else if (!string.Equals(prefix, thisPrefix, StringComparison.Ordinal)) + { + return null; + } + } + + return prefix; + } + static Dictionary LoadBaselineRows(string baselineFile, string[] columnsToRemove) { Dictionary result = new(); @@ -364,6 +461,8 @@ static void MakeLineBold(StringBuilder sb, string line) sb.AppendLine(); } + sealed record TableRow(int RowIndex, string[] Tokens); + /// /// Parses the markdown tables produced by BenchmarkDotNet's GitHub exporter. /// Tables are framed by a header row (index 0), a separator row like |---|---| (index 1), and one or more