Backtestshugorun-20260922-141813#15027

Issue #15027 Merged fix PR # Run 22 Sep 2026 · run-20260922-141813 gohugoio/hugo

metrics: --templateMetrics duration columns have inconsistent formatting

Better

Our patch was judged better than the merged fix.

In this judgment, Patch B = our agent, Patch A = the merged fix.

Both patches correctly resolve the root cause — they replace time.Duration.String() with a fixed-decimal formatter (2 decimals + fixed-width unit) that, under the table's right-justified %12s/%15s columns, aligns decimal points across rows. Correctness is effectively a tie: A pads the unit to a consistent 3-char suffix (using a deliberate double-space '  s' for the 1-char second unit), B uses '%-2s' to pad the unit to a consistent 2-rune width; both achieve aligned decimals. The differences favor B on the remaining criteria. Tests: B's TestWriteMetricsDurationFormatting exercises the public WriteMetrics path and directly asserts the property the issue is about — it computes the byte index of the decimal point on each layout row and asserts they are equal, i.e. it verifies alignment, not merely the format literals. A's TestFormatDuration only asserts the exact output strings of formatDuration (e.g. '4.34  s'); it pins the format but is essentially a change-detector that never checks that columns line up, which is the actual reported symptom. Scope: A must also edit tpl/partials/partials_integration_test.go, widening the duration-stripping regex to '\s*' because A's space-separated format ('170.29 ms') is no longer matched by the existing '\b[\.\d]*(ms|ns|µs|s)\b' regex; that extra file change is forced by A's format choice. B deliberately emits no space between value and unit ('1.50ms', '500.00µs'), so the existing integration-test regex still strips it and no third file needs touching — a genuinely smaller blast radius (2 files vs 3), and the test evidence indicates B's tests still pass. Craft: B adds a doc comment explaining *why* the padding exists (alignment), and uses the clearer d.Seconds()/d.Nanoseconds() accessors and idiomatic '%-2s' padding, whereas A relies on a slightly awkward hard-coded double-space with an inline note. A's space-before-unit is marginally nicer to read, but that is a minor, subjective cosmetic point against B's stronger test, smaller scope, and cleaner rationale. Net: B is the better fix.

The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.

Solve

15m 48s21 turns

Wall-clock time in the container

Full test suite

No new failures

3,723 passed · 99 skipped · 3,828 total

Contamination audit

Unknown

See the audit notes for this run

Patch size

58vs 41 merged

Changed lines, ours vs merged

The --templateMetrics output table prints duration values (sum/avg/max) using time.Duration.String(), which varies both units and decimal places depending on the magnitude. This makes the table hard to scan when rows have durations of different magnitudes.

Proposed fix: Use a fixed-decimal formatter (two decimal places, unit suffix padded to consistent width) so decimal points align across all rows in the table.

The two patches

Our patch+56 −2
metrics/metrics.goindex 08d9322ab..8f412b87f 100644@@ -180,13 +180,32 @@ func (s *Store) WriteMetrics(w io.Writer) { 	sort.Sort(bySum(results)) 	for _, v := range results { 		if s.calculateHints {-			fmt.Fprintf(w, "  %15s  %12s  %12s  %9d  %7.f  %6d  %5d  %s\n", v.sum, v.avg, v.max, v.cacheFactor, float64(v.cacheCount)/float64(v.count)*100, v.cacheCount, v.count, v.key)+			fmt.Fprintf(w, "  %15s  %12s  %12s  %9d  %7.f  %6d  %5d  %s\n", formatDuration(v.sum), formatDuration(v.avg), formatDuration(v.max), v.cacheFactor, float64(v.cacheCount)/float64(v.count)*100, v.cacheCount, v.count, v.key) 		} else {-			fmt.Fprintf(w, "  %15s  %12s  %12s  %5d  %s\n", v.sum, v.avg, v.max, v.count, v.key)+			fmt.Fprintf(w, "  %15s  %12s  %12s  %5d  %s\n", formatDuration(v.sum), formatDuration(v.avg), formatDuration(v.max), v.count, v.key) 		} 	} } +// formatDuration formats d with a fixed number of decimals and a unit+// suffix padded to a consistent width so decimal points align in the+// metrics table columns.+func formatDuration(d time.Duration) string {+	var value float64+	var unit string+	switch {+	case d < time.Microsecond:+		value, unit = float64(d.Nanoseconds()), "ns"+	case d < time.Millisecond:+		value, unit = float64(d.Nanoseconds())/1e3, "µs"+	case d < time.Second:+		value, unit = float64(d.Nanoseconds())/1e6, "ms"+	default:+		value, unit = d.Seconds(), "s"+	}+	return fmt.Sprintf("%.2f%-2s", value, unit)+}+ // A result represents the calculated results for a given metric. type result struct { 	key         stringmetrics/metrics_test.goindex 83dc1433f..c71db98e0 100644@@ -14,9 +14,11 @@ package metrics  import (+	"bytes" 	"html/template" 	"strings" 	"testing"+	"time"  	"github.com/gohugoio/hugo/resources/page" @@ -46,6 +48,39 @@ func TestSimilarPercentage(t *testing.T) { 	c.Assert(howSimilar("\n", ""), qt.Equals, 100) } +func TestWriteMetricsDurationFormatting(t *testing.T) {+	c := qt.New(t)++	s := &Store{+		metrics: map[string][]time.Duration{+			"layout/a.html": {2 * time.Second},+			"layout/b.html": {1500 * time.Microsecond},+			"layout/c.html": {500 * time.Microsecond},+		},+	}++	var buf bytes.Buffer+	s.WriteMetrics(&buf)++	out := buf.String()++	c.Assert(out, qt.Contains, "2.00s")+	c.Assert(out, qt.Contains, "1.50ms")+	c.Assert(out, qt.Contains, "500.00µs")++	var dotIndexes []int+	for line := range strings.Lines(out) {+		if !strings.Contains(line, "layout/") {+			continue+		}+		dotIndexes = append(dotIndexes, strings.IndexByte(line, '.'))+	}++	c.Assert(dotIndexes, qt.HasLen, 3)+	c.Assert(dotIndexes[1], qt.Equals, dotIndexes[0])+	c.Assert(dotIndexes[2], qt.Equals, dotIndexes[0])+}+ type testStruct struct { 	Name string } 
Merged fix · PR #+37 −4
metrics/metrics.goindex 08d9322ab..aca7c4c9b 100644@@ -178,15 +178,29 @@ func (s *Store) WriteMetrics(w io.Writer) { 	}  	sort.Sort(bySum(results))+ 	for _, v := range results { 		if s.calculateHints {-			fmt.Fprintf(w, "  %15s  %12s  %12s  %9d  %7.f  %6d  %5d  %s\n", v.sum, v.avg, v.max, v.cacheFactor, float64(v.cacheCount)/float64(v.count)*100, v.cacheCount, v.count, v.key)+			fmt.Fprintf(w, "  %15s  %12s  %12s  %9d  %7.f  %6d  %5d  %s\n", formatDuration(v.sum), formatDuration(v.avg), formatDuration(v.max), v.cacheFactor, float64(v.cacheCount)/float64(v.count)*100, v.cacheCount, v.count, v.key) 		} else {-			fmt.Fprintf(w, "  %15s  %12s  %12s  %5d  %s\n", v.sum, v.avg, v.max, v.count, v.key)+			fmt.Fprintf(w, "  %15s  %12s  %12s  %5d  %s\n", formatDuration(v.sum), formatDuration(v.avg), formatDuration(v.max), v.count, v.key) 		} 	} } +func formatDuration(d time.Duration) string {+	switch {+	case d >= time.Second:+		return fmt.Sprintf("%.2f  s", float64(d)/float64(time.Second)) // additional spacing between value and unit+	case d >= time.Millisecond:+		return fmt.Sprintf("%.2f ms", float64(d)/float64(time.Millisecond))+	case d >= time.Microsecond:+		return fmt.Sprintf("%.2f µs", float64(d)/float64(time.Microsecond))+	default:+		return fmt.Sprintf("%.2f ns", float64(d)/float64(time.Nanosecond))+	}+}+ // A result represents the calculated results for a given metric. type result struct { 	key         stringmetrics/metrics_test.goindex 83dc1433f..1e5221ec1 100644@@ -17,6 +17,7 @@ import ( 	"html/template" 	"strings" 	"testing"+	"time"  	"github.com/gohugoio/hugo/resources/page" @@ -66,3 +67,21 @@ func BenchmarkHowSimilar(b *testing.B) { 		howSimilar(s1, s2) 	} }++func TestFormatDuration(t *testing.T) {+	c := qt.New(t)+	tests := []struct {+		duration time.Duration+		want     string+	}{+		{4*time.Second + 342*time.Millisecond, "4.34  s"}, // additional spacing between value and unit+		{170*time.Millisecond + 289*time.Microsecond, "170.29 ms"},+		{16*time.Microsecond + 90*time.Nanosecond, "16.09 µs"},+		{147 * time.Nanosecond, "147.00 ns"},+	}++	for _, tt := range tests {+		got := formatDuration(tt.duration)+		c.Assert(got, qt.Equals, tt.want)+	}+}tpl/partials/partials_integration_test.goindex dc49cbdc5..58b4248c0 100644@@ -170,8 +170,8 @@ D1  	got := buf.String() -	// Get rid of all the durations, they are never the same.-	durationRe := regexp.MustCompile(`\b[\.\d]*(ms|ns|µs|s)\b`)+	// Get rid of all the durations, including the space and unit, they are never the same.+	durationRe := regexp.MustCompile(`\b[\.\d]*\s*(ms|ns|µs|s)\b`)  	normalize := func(s string) string { 		s = durationRe.ReplaceAllString(s, "") 

What the agent reported

The fix is complete and verified. Here is my final report.

What was wrong

metrics/metrics.go's (*Store).WriteMetrics printed the cumulative/average/maximum duration columns by passing time.Duration values directly to fmt.Fprintf with %s, which invokes time.Duration.String(). That method varies both unit and decimal-place count by magnitude ("2s", "1.5ms", "500µs"), so decimal points never lined up down a column and the --templateMetrics table was hard to scan.

What I changed

  • metrics/metrics.go — Added an unexported formatDuration(d time.Duration) string helper that formats each duration with exactly two decimal places (%.2f) and a unit suffix (ns/µs/ms/s) right-padded to a consistent width of 2 runes (%-2s). This makes the text after the decimal point a constant 4 runes for every magnitude, so within each right-justified %15s/%12s column the decimal points align. Both branches of the row-printing loop now wrap v.sum, v.avg, v.max in formatDuration(...). Headers, sorting, and all other columns are untouched.
  • metrics/metrics_test.go — Added TestWriteMetricsDurationFormatting (regression test).

Test evidence

  • Red before fix: the new test failed on qt.Contains "2.00s" — old output was 2s / 1.5ms / 500µs.
  • Green after fix: ./bt-test ./metrics/ → TestWriteMetricsDurationFormatting pass, plus existing TestSimilarPercentage and TestSimilarPercentageNonString still pass; package pass.
  • Blast radius: two integration tests inspect WriteMetrics output. hugolib/pagesfromdata only asserts on the template key (unaffected). tpl/partials's TestIncludeCacheHints normalizes durations away with a regex and trims the leading columns — I ran it: pass. The reviewer confirmed the fix is correct/complete and nothing unrelated changed.

Requirements checklist

  1. Duration columns (sum/avg/max) use a fixed-decimal formatter, two decimal places — Implemented (%.2f), covered by the regression test asserting 2.00s / 1.50ms / 500.00µs.
  2. Unit suffix padded to consistent width — Implemented (%-2s, so s → "s "), covered implicitly by the alignment assertion.
  3. Decimal points align across all rows — Implemented; the test asserts the first-. index is equal across all three mixed-magnitude rows.

Note: the pre-existing staged deletions of AGENTS.md / CLAUDE.md were present in the initial git status, are unrelated to this problem, and were not touched. All changes are left uncommitted in the working tree.