Skip to content

Average multi microbenchmarks results#5215

Draft
VincentBu wants to merge 25 commits into
dotnet:mainfrom
VincentBu:average-microbenchmarks-iterations
Draft

Average multi microbenchmarks results#5215
VincentBu wants to merge 25 commits into
dotnet:mainfrom
VincentBu:average-microbenchmarks-iterations

Conversation

@VincentBu
Copy link
Copy Markdown
Contributor

This PR aims at calculating average value of multiple microbenchmarks results. The work revolves around:

  1. Reduce memory usage.
  2. Change namespace of some classes and rename them for future work.

Copilot AI review requested due to automatic review settings May 1, 2026 07:37
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the GC microbenchmark infrastructure to support aggregating (averaging) results across multiple microbenchmark runs/iterations, while also renaming/refactoring parts of the analysis/presentation pipeline and introducing an outlier-removal helper.

Changes:

  • Add configurable microbenchmark iteration count (iterations) and wire it into suite creation and execution.
  • Replace the previous single-result comparison flow with a new per-benchmark aggregation/comparison pipeline (MicrobenchmarkResultComparison, GCTraceMetrics, GCTraceMetricComparisonResult).
  • Refactor output generation to primarily emit JSON (markdown generation currently disabled).

Reviewed changes

Copilot reviewed 21 out of 21 changed files in this pull request and generated 18 comments.

Show a summary per file
File Description
src/benchmarks/gc/GC.Infrastructure/GC.Infrastructure/Commands/RunCommand/CreateSuiteCommand.cs Reads configured iteration count and applies it to microbenchmark suite environment.
src/benchmarks/gc/GC.Infrastructure/GC.Infrastructure/Commands/RunCommand/BaseSuite/MicrobenchmarksToRun.txt Updates baseline suite benchmark list.
src/benchmarks/gc/GC.Infrastructure/GC.Infrastructure/Commands/RunCommand/BaseSuite/Microbenchmarks.yaml Renames environment iteration setting to iterations.
src/benchmarks/gc/GC.Infrastructure/GC.Infrastructure/Commands/Microbenchmark/MicrobenchmarkCommand.cs Runs microbenchmarks for iterations and switches to new aggregation/comparison logic before presenting results.
src/benchmarks/gc/GC.Infrastructure/GC.Infrastructure/Commands/Microbenchmark/MicrobenchmarkAnalyzeCommand.cs Updates analysis-only command to use the new aggregation/comparison logic.
src/benchmarks/gc/GC.Infrastructure/GC.Infrastructure.Core/Presentation/Microbenchmarks/Presentation.cs Changes presentation API to accept precomputed grouped results; markdown output path currently disabled.
src/benchmarks/gc/GC.Infrastructure/GC.Infrastructure.Core/Presentation/Microbenchmarks/Markdown.cs Markdown generation code is commented out.
src/benchmarks/gc/GC.Infrastructure/GC.Infrastructure.Core/Presentation/Microbenchmarks/Json/JsonOutput.cs Removes unused placeholder type.
src/benchmarks/gc/GC.Infrastructure/GC.Infrastructure.Core/Presentation/Microbenchmarks/Json.cs Moves JSON generator to Microbenchmarks presentation namespace and updates signature for grouped results.
src/benchmarks/gc/GC.Infrastructure/GC.Infrastructure.Core/Configurations/Microbenchmarks.Configuration.cs Renames iteration to iterations in microbenchmark environment configuration.
src/benchmarks/gc/GC.Infrastructure/GC.Infrastructure.Core/Configurations/InputConfiguration.cs Adds iterations map to input configuration.
src/benchmarks/gc/GC.Infrastructure/GC.Infrastructure.Core/Analysis/Microbenchmarks/MicrobenchmarkResultsAnalyzer.cs Removes old analyzer/comparison pipeline.
src/benchmarks/gc/GC.Infrastructure/GC.Infrastructure.Core/Analysis/Microbenchmarks/MicrobenchmarkResultComparison.cs Adds new JSON/trace mapping, per-benchmark analysis, and aggregation/grouping logic.
src/benchmarks/gc/GC.Infrastructure/GC.Infrastructure.Core/Analysis/Microbenchmarks/MicrobenchmarkResult.cs Introduces new MicrobenchmarkResult model (namespace currently mismatched vs usage).
src/benchmarks/gc/GC.Infrastructure/GC.Infrastructure.Core/Analysis/Microbenchmarks/MicrobenchmarkComparisonResult.cs Updates comparison to support averaged values/outlier removal and new trace-metric comparisons.
src/benchmarks/gc/GC.Infrastructure/GC.Infrastructure.Core/Analysis/GCTraceMetrics.cs Adds trace-derived metric extraction (includes reflection/stat bugs).
src/benchmarks/gc/GC.Infrastructure/GC.Infrastructure.Core/Analysis/GCTraceMetricComparisonResult.cs Adds averaged comparison for trace metrics (baseline vs comparand).
src/benchmarks/gc/GC.Infrastructure/GC.Infrastructure.Core/Analysis/GCTraceMetricComparison.cs Adds helper wrapper for metric comparison construction.
src/benchmarks/gc/GC.Infrastructure/GC.Infrastructure.Core/Analysis/BdnJsonResult.cs Refactors BDN JSON model types; renames top-level to BdnJsonResult.
src/benchmarks/gc/GC.Infrastructure/GC.Analysis.API/Statistics.cs Adds RemoveOutliers helper (IQR method).
src/benchmarks/gc/GC.Infrastructure/Configurations/Run.yaml Adds iteration configuration block (currently mismatched with new iterations input model).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/benchmarks/gc/GC.Infrastructure/Configurations/Run.yaml Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 6, 2026 05:25
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 21 out of 21 changed files in this pull request and generated 13 comments.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 6, 2026 06:26
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 21 out of 21 changed files in this pull request and generated 11 comments.

@VincentBu VincentBu marked this pull request as draft May 7, 2026 09:24
Copilot AI review requested due to automatic review settings May 7, 2026 09:25
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 21 out of 21 changed files in this pull request and generated 10 comments.

Comment thread src/benchmarks/gc/GC.Infrastructure/GC.Analysis.API/Statistics.cs
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 7, 2026 09:37
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 21 out of 21 changed files in this pull request and generated 11 comments.

// Set iterations if they exist.
if (inputConfiguration.iterations != null)
{
configuration.Environment.iterations = inputConfiguration.iterations.GetValueOrDefault<string, uint>("microbenchmarks", 1);
Comment on lines +64 to +83
if (property.PropertyType != typeof(double) || property.PropertyType != typeof(int))
{
continue;
}

string propertyName = property.Name;
double propertyValue = (double)(property.GetValue(processData.Stats) ?? double.NaN);
StatsData[propertyName] = propertyValue;
}

var fields = processData.Stats.GetType().GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
foreach (var field in fields)
{
if (field.FieldType != typeof(double) || field.FieldType != typeof(int))
{
continue;
}

string name = field.Name;
double value = (double)(field.GetValue(processData.Stats) ?? double.NaN);
Copilot AI review requested due to automatic review settings May 14, 2026 07:09
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 21 out of 21 changed files in this pull request and generated 11 comments.

Comments suppressed due to low confidence (1)

src/benchmarks/gc/GC.Infrastructure/GC.Infrastructure.Core/Presentation/Microbenchmarks/Markdown.cs:175

  • AveragedBaselineOtherMetrics/AveragedComparandOtherMetrics are Dictionary<string,double>, so GetValueOrDefault(column) returns 0 when the metric is missing. That makes missing metrics look like real 0 values and can produce incorrect deltas (and divide-by-zero in deltaPercent). Use TryGetValue and only compute delta/delta% when both sides have a value and the baseline is non-zero.
                        foreach (var column in configuration.Output.Columns)
                        {
                            double? baselineValue = lr.AveragedBaselineOtherMetrics.GetValueOrDefault(column);
                            double? comparandValue = lr.AveragedComparandOtherMetrics.GetValueOrDefault(column);

                            string baselineResult = baselineValue.HasValue ? Math.Round(baselineValue.Value, 4).ToString() : string.Empty;
                            string comparandResult = comparandValue.HasValue ? Math.Round(comparandValue.Value, 4).ToString() : string.Empty;
                            double? delta = baselineValue.HasValue && comparandValue.HasValue ? comparandValue.Value - baselineValue.Value : null;
                            string deltaResult = delta.HasValue ? Math.Round(delta.Value, 4).ToString() : string.Empty;

                            double? deltaPercent = delta.HasValue ? (delta / baselineValue.Value) * 100 : null;
                            string deltaPercentResult = deltaPercent.HasValue ? Math.Round(deltaPercent.Value, 4).ToString() : string.Empty;

Comment on lines +64 to +83
if (property.PropertyType != typeof(double) || property.PropertyType != typeof(int))
{
continue;
}

string propertyName = property.Name;
double propertyValue = (double)(property.GetValue(processData.Stats) ?? double.NaN);
StatsData[propertyName] = propertyValue;
}

var fields = processData.Stats.GetType().GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
foreach (var field in fields)
{
if (field.FieldType != typeof(double) || field.FieldType != typeof(int))
{
continue;
}

string name = field.Name;
double value = (double)(field.GetValue(processData.Stats) ?? double.NaN);
Comment on lines +78 to +86
var sortedJsonFiles = GoodLinq.Select(g, t => t.Item3)
.OrderBy(jsonFile => Path.GetFileName(Path.GetDirectoryName(jsonFile)))
.ToArray();

var traceFileNameTemplate = _benchmarkNameToTraceFilePatternMap[benchmarkName];
string outputPathForRun = Path.Combine(outputPath, run.Name);
var sortedTraceFiles = Directory.GetFiles(outputPathForRun, $"{traceFileNameTemplate}*.etl.zip", SearchOption.TopDirectoryOnly)
.OrderBy(traceFile => traceFile)
.ToArray();
run,
benchmark,
gcData: benchmarkGCData,
gcTraceMetrics: new GCTraceMetrics(benchmarkGCData, tracePath, benchmark.FullName),
Comment on lines +44 to +55
else
{
OriginalBaselineMetricCollection = baselines.Select(baseline => baseline.StatsData[fieldInfo.Name]);
OriginalComparandMetricCollection = comparands.Select(comparand => comparand.StatsData[fieldInfo.Name]);
}
}

else
{
OriginalBaselineMetricCollection = baselines.Select(baseline => baseline.StatsData[pInfo.Name]);
OriginalComparandMetricCollection = comparands.Select(comparand => comparand.StatsData[pInfo.Name]);
}
Comment on lines +23 to +35
if (includeTraces)
{
var baselineGCTraceMetricsCollection = baselines
.Where(baseline => baseline != null)
.Select(baseline => baseline.GCTraceMetrics)
.ToArray();

public MicrobenchmarkResult Baseline { get; set; }
public MicrobenchmarkResult Comparand { get; set; }
public List<ComparisonResult> ComparisonResults { get; set; }
var comparandGCTraceMetricsCollection = comparands
.Where(comparand => comparand != null)
.Select(comparand => comparand.GCTraceMetrics)
.ToArray();

// TODO: Nullable double check.
public string BaselineRunName => Baseline?.Parent?.Name;
public string ComparandRunName => Comparand?.Parent?.Name;
public string MicrobenchmarkName => Baseline.MicrobenchmarkName;
if (baselineGCTraceMetricsCollection.Length > 0 && comparandGCTraceMetricsCollection.Length > 0)
var improvements = GoodLinq.Where(ordered, o => o.GetDiffPercentFromOtherMetrics(metric) > 0.05 && o.GetDiffPercentFromOtherMetrics(metric) < 0.2);
improvements.Reverse();
sw.AddTableForSingleCriteria(configuration, improvements);
sw.AddTableForSingleCriteria(configuration, GoodLinq.Where(ordered, o => o.OtherMetricsDiffPerc[metric] > 0.05 && o.OtherMetricsDiffPerc[metric] < 0.2));
Comment on lines +189 to +194
cpuColumns: configuration.Output.cpu_columns,
columns: configuration.Output.Columns);
}
}
System.GC.Collect(2);
}
Comment on lines +180 to +204
//if (configuration.Output.cpu_columns != null)
//{
// foreach (var column in configuration.Output.cpu_columns)
// {
// if (!lr.Baseline.OtherMetrics.TryGetValue(column, out double? baselineValue))
// {
// lr.Baseline.OtherMetrics[column] = baselineValue = lr.Baseline.CPUData?.GetIncCountForGCMethod(column) ?? null;
// }
// string baselineResult = baselineValue.HasValue ? Math.Round(baselineValue.Value, 2).ToString() : string.Empty;

double? delta = baselineValue.HasValue && comparandValue.HasValue ? comparandValue.Value - baselineValue.Value : null;
string deltaResult = delta.HasValue ? Math.Round(delta.Value, 2).ToString() : string.Empty;
// if (!lr.Comparand.OtherMetrics.TryGetValue(column, out double? comparandValue))
// {
// lr.Comparand.OtherMetrics[column] = comparandValue = lr.Comparand.CPUData?.GetIncCountForGCMethod(column) ?? null;
// }
// string comparandResult = comparandValue.HasValue ? Math.Round(comparandValue.Value, 2).ToString() : string.Empty;

double? deltaPercent = delta.HasValue ? (delta / baselineValue.Value) * 100 : null;
string deltaPercentResult = deltaPercent.HasValue ? Math.Round(deltaPercent.Value, 2).ToString() : string.Empty;
// double? delta = baselineValue.HasValue && comparandValue.HasValue ? comparandValue.Value - baselineValue.Value : null;
// string deltaResult = delta.HasValue ? Math.Round(delta.Value, 2).ToString() : string.Empty;

baseRow += $"{baselineResult} | {comparandResult} | {deltaResult} | {deltaPercentResult} |";
}
}
// double? deltaPercent = delta.HasValue ? (delta / baselineValue.Value) * 100 : null;
// string deltaPercentResult = deltaPercent.HasValue ? Math.Round(deltaPercent.Value, 2).ToString() : string.Empty;

// baseRow += $"{baselineResult} | {comparandResult} | {deltaResult} | {deltaPercentResult} |";
// }
//}
Comment on lines +252 to +256
var comparandMicrobenchmarkResults = GoodLinq.Where(microbenchmarkResultsGroup, r => !r.Parent.is_baseline);

lock (_lock)
{
comparisonResults.Add(new(baselineMicrobenchmarkResults, comparandMicrobenchmarkResults, includeTraces));
Comment on lines +139 to +194
foreach (var benchmark in benchmarks)
{
Statistics statistics = benchmark.Statistics;
var benchmarkFullName = benchmark.FullName;
MicrobenchmarkResult? microbenchmarkResult = null;
if ((!excludeTraces) && configuration.TraceConfigurations.Type != "none")
{
string outputPathForRun = Path.Combine(configuration.Output.Path, run.Name!);
string tracePath = jsonToTraceMap.GetValueOrDefault(jsonPath, "");

using (var analyzer = AnalyzerManager.GetAnalyzer(tracePath))
{
List<GCProcessData> allPertinentProcesses = analyzer.GetProcessGCData("dotnet");
List<GCProcessData> corerunProcesses = analyzer.GetProcessGCData("corerun");
allPertinentProcesses.AddRange(corerunProcesses);

GCProcessData? benchmarkGCData = null;
foreach (var process in allPertinentProcesses)
{
string commandLine = process.CommandLine.Replace("\"", "").Replace("\\", "");
string runCleaned = benchmark.FullName.Replace("\"", "").Replace("\\", "");
if (commandLine.Contains(runCleaned) && commandLine.Contains("--benchmarkName"))
{
benchmarkGCData = process;
break;
}
}
if (benchmarkGCData != null)
{
int processID = benchmarkGCData.ProcessID;

/*
TODO: THIS NEEDS TO BE ADDED BACK.
if (configuration.Output.cpu_columns != null && configuration.Output.cpu_columns.Count > 0)
{
// TODO: Add parameterize.
benchmark.Value.GCData.Parent.AddCPUAnalysis(yamlPath: @"C:\Users\musharm\source\repos\GC.Analysis.API\GC.Analysis.API\CPUAnalysis\DefaultMethods.yaml",
symbolLogFile: Path.Combine(configuration.Output.Path, run.Key, Guid.NewGuid() + ".txt"),
symbolPath: Path.Combine(configuration.Output.Path, run.Key));
var d1 = benchmark.Value.GCData.Parent.CPUAnalyzer.GetCPUDataForProcessName("dotnet");
d1.AddRange(benchmark.Value.GCData.Parent.CPUAnalyzer.GetCPUDataForProcessName("corerun"));
benchmark.Value.CPUData = d1.FirstOrDefault(p => p.ProcessID == processID);
}
*/
microbenchmarkResult = new(benchmarkFullName,
run,
benchmark,
gcData: benchmarkGCData,
gcTraceMetrics: new GCTraceMetrics(benchmarkGCData, tracePath, benchmark.FullName),
additionalReportMetrics: configuration.Output.additional_report_metrics,
cpuColumns: configuration.Output.cpu_columns,
columns: configuration.Output.Columns);
}
}
System.GC.Collect(2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants