Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ All notable changes to BenchmarkGate are documented here.
`compare` pipeline. Deterministic output ordering via a new
`Core.Identity.BenchmarkIdentityComparer`. Policy-free — no threshold or
pass/fail vocabulary.
- `Reporting.ConsoleComparisonReporter`, `Reporting.JsonComparisonReporter`,
`Reporting.MarkdownComparisonReporter` — console, JSON (`schemaVersion=1`,
independently versioned, full precision preserved), and Markdown output
for `ComparisonResult`. No JUnit — no test verdict exists for a
policy-free comparison. No pass/fail vocabulary in any of the three.
- `Reporting.MarkdownBuilder.CodeSpan` — CommonMark-safe backtick-delimited
code spans.

### Changed
- `Core.Comparison.MetricValue.Unit` is now `string?` rather than `string`.
Expand Down
74 changes: 74 additions & 0 deletions src/BenchmarkGate.Reporting/ComparisonValueFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using Bijecta.BenchmarkGate.Core.Comparison;
using Bijecta.BenchmarkGate.Core.Evaluation;
using System.Globalization;

namespace Bijecta.BenchmarkGate.Reporting;

/// <summary>
/// Shared formatting for human-readable comparison reporters (Console,
/// Markdown). Policy-free — this is reporting-layer sharing, not a Core
/// dependency.
/// </summary>
internal static class ComparisonValueFormatter
{
/// <summary>
/// Formats a raw metric value. Uses <see cref="MetricValue.Unit"/> as
/// the source of truth for whether a known-unit formatter applies —
/// when unit is null (unknown metric), falls back to a raw
/// round-trippable numeric representation rather than reconstructing
/// unit semantics from the metric name alone.
/// </summary>
public static string FormatMetricValue(string metricName, MetricValue? value)
{
if (value is not { } metricValue)
{
return "-";
}

return metricValue.Unit is null
? metricValue.Value.ToString("G17", CultureInfo.InvariantCulture)
: MetricFormatters.For(metricName).Format(metricValue.Value);
}

/// <summary>
/// Formats an absolute delta with an explicit "+" for positive values
/// (formatter output already carries "-" naturally for negative ones).
/// Meaningful even when <see cref="MetricComparison.PercentDelta"/> is
/// null (a zero-reference change) — callers must not skip this column
/// just because the percent column shows "n/a".
/// </summary>
/// <param name="metricName">
/// The metric's name, used to select a display formatter when
/// <paramref name="unit"/> is known.
/// </param>
/// <param name="unit">
/// The comparison's known unit for this metric (from either side's
/// <see cref="MetricValue.Unit"/> — safe to use either today, since
/// genuine unit-mismatch detection isn't producible yet and a
/// Comparable metric receives the same catalog-derived unit on both
/// sides). Null means unknown, same as <see cref="FormatMetricValue"/>'s
/// contract — the delta then gets a raw round-trippable representation
/// rather than assuming a formatter derived from the metric name alone
/// knows the right unit.
/// </param>
/// <param name="absoluteDelta">
/// The raw delta from <see cref="MetricComparison.AbsoluteDelta"/>, or
/// null when no delta was computed (any non-Comparable metric status).
/// </param>
public static string FormatAbsoluteDelta(string metricName, string? unit, double? absoluteDelta)
{
if (absoluteDelta is not { } delta)
{
return "-";
}

var formatted = unit is null
? delta.ToString("G17", CultureInfo.InvariantCulture)
: MetricFormatters.For(metricName).Format(delta);

return delta > 0d ? $"+{formatted}" : formatted;
}

public static string FormatPercentDelta(double? percentDelta) =>
percentDelta is { } percent ? MarkdownBuilder.FormatDeltaPercent(percent) : "n/a";
}
129 changes: 129 additions & 0 deletions src/BenchmarkGate.Reporting/ConsoleComparisonReporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
using System.Globalization;
using Bijecta.BenchmarkGate.Core.Comparison;

namespace Bijecta.BenchmarkGate.Reporting;

/// <summary>
/// Renders a <see cref="ComparisonResult"/> as a compact console table. One
/// printed row per (benchmark, metric) pair for Comparable benchmarks;
/// Added/Removed benchmarks are listed separately by name, not folded into
/// the metric table, since they have no meaningful reference-vs-candidate
/// row. Reporting only consumes the comparison result — it never
/// re-compares anything, and never prints SuiteDecision's pass/fail
/// vocabulary (Passed/Warning/Regressed/Unstable).
/// </summary>
/// <remarks>
/// Preserves <see cref="ComparisonResult.Benchmarks"/>' given order rather
/// than re-sorting — per ADR-0004, canonical ordering is
/// <c>BenchmarkComparisonEngine</c>'s responsibility alone; a reporter
/// re-sorting by <c>CanonicalString</c> would duplicate that responsibility
/// with a different (string, not structured) comparator.
/// </remarks>
public static class ConsoleComparisonReporter
{
public static void Write(TextWriter output, ComparisonResult comparison)
{
ArgumentNullException.ThrowIfNull(output);
ArgumentNullException.ThrowIfNull(comparison);

output.WriteLine(string.Create(CultureInfo.InvariantCulture,
$"Suite: {comparison.Suite} " +
$"Comparable: {comparison.ComparableCount} " +
$"Added: {comparison.AddedCount} " +
$"Removed: {comparison.RemovedCount}"));
output.WriteLine();

var comparableBenchmarks = comparison.Benchmarks
.Where(b => b.Status == BenchmarkComparisonStatus.Comparable)
.ToList();

if (comparableBenchmarks.Count == 0 && comparison.AddedCount == 0 && comparison.RemovedCount == 0)
{
output.WriteLine("No benchmarks compared.");
return;
}

if (comparableBenchmarks.Count > 0)
{
WriteMetricTable(output, comparableBenchmarks);
}

WriteNameList(output, "Added benchmarks:", comparison.Benchmarks, BenchmarkComparisonStatus.Added);
WriteNameList(output, "Removed benchmarks:", comparison.Benchmarks, BenchmarkComparisonStatus.Removed);
}

private static void WriteMetricTable(TextWriter output, IReadOnlyList<BenchmarkComparison> benchmarks)
{
const int nameWidth = 40;
const int metricWidth = 20;
const int valueWidth = 12;
const int absoluteWidth = 14;
const int percentWidth = 10;
const int directionWidth = 14;

output.WriteLine(
$"{"Benchmark".PadRight(nameWidth)} {"Metric".PadRight(metricWidth)} {"Reference".PadRight(valueWidth)} " +
$"{"Candidate".PadRight(valueWidth)} {"Abs Delta".PadRight(absoluteWidth)} {"% Delta".PadRight(percentWidth)} " +
$"{"Direction".PadRight(directionWidth)} Status");

foreach (var benchmark in benchmarks)
{
var name = Truncate(benchmark.Identity.CanonicalString, nameWidth);

if (benchmark.Metrics.Count == 0)
{
// The benchmark itself IS Comparable — "No metrics" says
// why the row has no values, rather than "-" which could
// read as an unknown/missing status.
output.WriteLine(
$"{name.PadRight(nameWidth)} {"-".PadRight(metricWidth)} {"-".PadRight(valueWidth)} " +
$"{"-".PadRight(valueWidth)} {"-".PadRight(absoluteWidth)} {"-".PadRight(percentWidth)} " +
$"{"-".PadRight(directionWidth)} No metrics");
continue;
}

foreach (var metric in benchmark.Metrics)
{
var metricName = Truncate(metric.MetricName, metricWidth);
var unit = metric.Reference?.Unit ?? metric.Candidate?.Unit;
var referenceText = Truncate(ComparisonValueFormatter.FormatMetricValue(metric.MetricName, metric.Reference), valueWidth);
var candidateText = Truncate(ComparisonValueFormatter.FormatMetricValue(metric.MetricName, metric.Candidate), valueWidth);
var absoluteText = Truncate(ComparisonValueFormatter.FormatAbsoluteDelta(metric.MetricName, unit, metric.AbsoluteDelta), absoluteWidth);
var percentText = Truncate(ComparisonValueFormatter.FormatPercentDelta(metric.PercentDelta), percentWidth);
var directionText = Truncate(metric.Direction?.ToString() ?? "-", directionWidth);

output.WriteLine(
$"{name.PadRight(nameWidth)} {metricName.PadRight(metricWidth)} {referenceText.PadRight(valueWidth)} " +
$"{candidateText.PadRight(valueWidth)} {absoluteText.PadRight(absoluteWidth)} {percentText.PadRight(percentWidth)} " +
$"{directionText.PadRight(directionWidth)} {metric.Status}");

// Only print the benchmark name on the first metric row to
// avoid visual repetition on multi-metric benchmarks.
name = "";
}
}

output.WriteLine();
}

private static void WriteNameList(
TextWriter output, string heading, IReadOnlyList<BenchmarkComparison> benchmarks, BenchmarkComparisonStatus status)
{
var matching = benchmarks.Where(b => b.Status == status).ToList();
if (matching.Count == 0)
{
return;
}

output.WriteLine(heading);
foreach (var benchmark in matching)
{
output.WriteLine($" {benchmark.Identity.CanonicalString}");
}

output.WriteLine();
}

private static string Truncate(string value, int maxLength) =>
value.Length <= maxLength ? value : value[..(maxLength - 1)] + "\u2026";
}
Loading
Loading