Skip to content

Average multi microbenchmarks results#5215

Draft
VincentBu wants to merge 16 commits intodotnet:mainfrom
VincentBu:average-microbenchmarks-iterations
Draft

Average multi microbenchmarks results#5215
VincentBu wants to merge 16 commits intodotnet: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 on lines +24 to +55
// If property isn't found on the GCTraceMetrics, look in GCStats.
// TODO: Add the case where we look into the map.
else
{
pInfo = typeof(GCStats).GetProperty(metricName, BindingFlags.Instance | BindingFlags.Public);
if (pInfo == null)
{
FieldInfo fieldInfo = typeof(GCStats).GetField(metricName, BindingFlags.Instance | BindingFlags.Public);
if (fieldInfo == null)
{
// Out of luck!
OriginalBaselineMetricCollection = Array.Empty<double>();
OriginalComparandMetricCollection = Array.Empty<double>();
OutliersFreeBaselineMetricCollection = Array.Empty<double>();
OutliersFreeComparandMetricCollection = Array.Empty<double>();
AveragedBaselineMetric = double.NaN;
AveragedComparandMetric = double.NaN;
return;
}

else
{
OriginalBaselineMetricCollection = GoodLinq.Select(baselines, baseline => (double)fieldInfo.GetValue(baseline));
OriginalComparandMetricCollection = GoodLinq.Select(comparands, comparand => (double)fieldInfo.GetValue(comparand));
}
}

else
{
OriginalBaselineMetricCollection = GoodLinq.Select(baselines, baseline => (double)pInfo.GetValue(baseline));
OriginalComparandMetricCollection = GoodLinq.Select(comparands, comparand => (double)pInfo.GetValue(comparand));
}
Comment on lines +15 to +30
var baselineGCTraceMetricsCollection = GoodLinq.Select(baselines, baseline => baseline.GCTraceMetrics);
var comparandGCTraceMetricsCollection = GoodLinq.Select(comparands, comparand => comparand.GCTraceMetrics);

string[] metricNames = new string[]
{
"PctTimePausedInGC",
"ExecutionTimeMSec",
"PauseDurationMSec_MeanWhereIsEphemeral",
"PauseDurationMSec_MeanWhereIsBackground",
"PauseDurationMSec_MeanWhereIsBlockingGen2"
};

foreach (var metricName in metricNames)
{
ComparisonResults.Add(
GCTraceMetricComparison.CompareGCTraceMetric(baselineGCTraceMetricsCollection, comparandGCTraceMetricsCollection, metricName));
Comment on lines +13 to +14
{ "System.Collections.CtorGivenSize<String>.Array(Size: 512)", "System.Collections.CtorGivenSize_String_.Array_size_512_"},
{ "System.Collections.Tests.Perf_BitArray.BitArrayByteArrayCtor(Size: 512)", "System.Collections.Tests.Perf_BitArray.BitArrayByteArrayCtor_size_512_"},
Comment on lines +69 to +70
benchmarkFullNameJsonMap[fullName] = benchmarkFullNameJsonMap.GetValueOrDefault(fullName, new());
benchmarkFullNameJsonMap[fullName].Add(jsonFile);
Comment on lines +35 to +54
public static IEnumerable<double> RemoveOutliers(IEnumerable<double> collection)
{
if (!collection.Any())
{
return Array.Empty<double>();
}
// Calculate Q1 (25th percentile) and Q3 (75th percentile)
double q1 = GC.Analysis.API.Statistics.Percentile(collection, 0.25);
double q3 = GC.Analysis.API.Statistics.Percentile(collection, 0.75);

// Calculate IQR (Interquartile Range)
double iqr = q3 - q1;

// Calculate bounds: [Q1 - 1.5*IQR, Q3 + 1.5*IQR]
double lowerBound = q1 - 1.5 * iqr;
double upperBound = q3 + 1.5 * iqr;

// Filter out outliers
return GoodLinq.Where(collection, x => x >= lowerBound && x <= upperBound);
}
Comment on lines +36 to +45
Run? run = configuration.Runs.Values.FirstOrDefault();
if (run == null)
{
throw new InvalidOperationException("No runs found in the configuration.");
}
string outputPathForRun = Path.Combine(configuration.Output.Path, run.Name);
var benchmarkFullNameJsonMap = MicrobenchmarkResultComparison.MapBenchmarkFullNameToJsonForRun(outputPathForRun);
List<MicrobenchmarkComparisonResult> comparisonResultForAllBenchmarks = new();

foreach (var benchmarkFullName in benchmarkFullNameJsonMap.Keys)
if (format == "markdown")
{
Markdown.GenerateTable(configuration, comparisonResults, executionDetails, Path.Combine(configuration.Output.Path, "Results.md"));
//Markdown.GenerateTable(configuration, comparisonResultsGroupedByName, executionDetails, Path.Combine(configuration.Output.Path, "Results.md"));

Presentation.Present(configuration, comparisonResultsGroupedName, executionDetails); // Execution details aren't available for the analysis-only mode.
Directory.SetCurrentDirectory(currentDirectory);
AnsiConsole.Markup($"[bold green] ({DateTime.Now}) Wrote Microbechmark Results to: {Markup.Escape(Path.Combine(configuration.Output.Path, "Results.md"))} [/]");
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 +15 to 16
//Markdown.GenerateTable(configuration, comparisonResultsGroupedByName, executionDetails, Path.Combine(configuration.Output.Path, "Results.md"));
continue;

Presentation.Present(configuration, comparisonResultsGroupedName, executionDetails); // Execution details aren't available for the analysis-only mode.
Directory.SetCurrentDirectory(currentDirectory);
AnsiConsole.Markup($"[bold green] ({DateTime.Now}) Wrote Microbechmark Results to: {Markup.Escape(Path.Combine(configuration.Output.Path, "Results.md"))} [/]");
Comment on lines +36 to +41
Run? run = configuration.Runs.Values.FirstOrDefault();
if (run == null)
{
throw new InvalidOperationException("No runs found in the configuration.");
}
string outputPathForRun = Path.Combine(configuration.Output.Path, run.Name);
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 +69 to +70
benchmarkFullNameJsonMap[fullName] = benchmarkFullNameJsonMap.GetValueOrDefault(fullName, new());
benchmarkFullNameJsonMap[fullName].Add(jsonFile);
Comment on lines +96 to +110
if (!_benchmarkNameToTraceFilePatternMap.Keys.Contains(benchmarkFullName))
{
throw new KeyNotFoundException("No trace file pattern found for benchmark: " + benchmarkFullName);
}
string traceFileNameTemplate = _benchmarkNameToTraceFilePatternMap[benchmarkFullName];

string[] sortedTraceFiles = Enumerable.Where(Directory.GetFiles(outputPathForRun, "*.etl.zip", SearchOption.TopDirectoryOnly), traceFile =>
Path.GetFileName(traceFile).ToLower().Contains(traceFileNameTemplate.ToLower()))
.OrderBy(traceFile => traceFile)
.ToArray();

if (sortedJsonFiles.Length != sortedTraceFiles.Length)
{
throw new InvalidOperationException(
$"The number of JSON files ({sortedJsonFiles.Length}) does not match the number of trace files ({sortedTraceFiles.Length}) for benchmark: {benchmarkFullName}");
Comment on lines +127 to +134
string outputPathForRun = Path.Combine(configuration.Output.Path, run.Key);
run.Value.Name ??= run.Key;

var benchmarkToJsonMapForRun = MapBenchmarkFullNameToJsonForRun(outputPathForRun);
var jsonFiles = benchmarkToJsonMapForRun.GetValueOrDefault(benchmarkFullName, new());

runsToResults[run.Value] = runsToResults.GetValueOrDefault(run.Value, new());

Comment on lines +13 to +31
if (includeTraces)
{
var baselineGCTraceMetricsCollection = GoodLinq.Select(baselines, baseline => baseline.GCTraceMetrics);
var comparandGCTraceMetricsCollection = GoodLinq.Select(comparands, comparand => comparand.GCTraceMetrics);

string[] metricNames = new string[]
{
"PctTimePausedInGC",
"ExecutionTimeMSec",
"PauseDurationMSec_MeanWhereIsEphemeral",
"PauseDurationMSec_MeanWhereIsBackground",
"PauseDurationMSec_MeanWhereIsBlockingGen2"
};

foreach (var metricName in metricNames)
{
ComparisonResults.Add(
GCTraceMetricComparison.CompareGCTraceMetric(baselineGCTraceMetricsCollection, comparandGCTraceMetricsCollection, metricName));
}
Comment on lines +24 to +55
// If property isn't found on the GCTraceMetrics, look in GCStats.
// TODO: Add the case where we look into the map.
else
{
pInfo = typeof(GCStats).GetProperty(metricName, BindingFlags.Instance | BindingFlags.Public);
if (pInfo == null)
{
FieldInfo fieldInfo = typeof(GCStats).GetField(metricName, BindingFlags.Instance | BindingFlags.Public);
if (fieldInfo == null)
{
// Out of luck!
OriginalBaselineMetricCollection = Array.Empty<double>();
OriginalComparandMetricCollection = Array.Empty<double>();
OutliersFreeBaselineMetricCollection = Array.Empty<double>();
OutliersFreeComparandMetricCollection = Array.Empty<double>();
AveragedBaselineMetric = double.NaN;
AveragedComparandMetric = double.NaN;
return;
}

else
{
OriginalBaselineMetricCollection = GoodLinq.Select(baselines, baseline => (double)fieldInfo.GetValue(baseline));
OriginalComparandMetricCollection = GoodLinq.Select(comparands, comparand => (double)fieldInfo.GetValue(comparand));
}
}

else
{
OriginalBaselineMetricCollection = GoodLinq.Select(baselines, baseline => (double)pInfo.GetValue(baseline));
OriginalComparandMetricCollection = GoodLinq.Select(comparands, comparand => (double)pInfo.GetValue(comparand));
}
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