Backtestshugorun-20261024-162305#15027
metrics: --templateMetrics duration columns have inconsistent formatting
Our patch was judged better than the merged fix.
In this judgment, Patch B = our agent, Patch A = the merged fix.
Both patches replace the three Duration.String() calls with a formatDuration helper that prints two decimals and pads the unit to width 2, so both fix the reported misalignment; the independent tests pass identically. Patch A pads by inserting a space before the unit and a hard-coded double space for seconds ("4.34 s"), which also forces a change to the partials integration test regex because the old pattern no longer matched a space between number and unit. Patch B pads with %-2s ("1.00s ", "12.35ms"), which keeps the existing duration-stripping regex working unchanged and is documented with a clear comment; the abs handling for negative durations is harmless extra. On tests, A adds only a four-case table check of the helper. B adds a helper test covering zero, the unit boundaries, and minute-scale values, plus a WriteMetrics-level test that builds a mixed-magnitude store and verifies the decimal points of all three columns line up across rows in both hints modes, which directly exercises the issue's actual complaint. B's test relies on Go 1.22 range-over-int and the builtin max, and on a header line count I cannot verify, which slightly tempers confidence, but overall B is the cleaner fix with materially stronger tests.
Four judges ruled on this ticket in parallel, and the verdict comes from counting their votes. Each saw the issue and the two patches, labelled A and B in random order (two of them saw ours as A), with issue links and version numbers removed. Where tests could check the fix, they also saw the results of tests a separate agent wrote for the issue, run on both patches. None was told which patch was ours. The reasoning above is one judge's, shown verbatim.
Solve
18m 28s
Wall-clock time in the container
Full test suite
No new failures
176 passed · 0 skipped · 178 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
98vs 41 merged
Changed lines, ours vs merged
The issue
View on GitHubThe --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
metrics/metrics.goindex 08d9322ab..08039b634 100644@@ -180,13 +180,34 @@ 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 two decimal places and a unit suffix+// padded to a fixed width, so that decimal points line up when the+// values are right-aligned in a column.+func formatDuration(d time.Duration) string {+ var (+ value float64+ unit string+ )+ switch abs := max(d, -d); {+ case abs >= time.Second:+ value, unit = d.Seconds(), "s"+ case abs >= time.Millisecond:+ value, unit = float64(d)/float64(time.Millisecond), "ms"+ case abs >= time.Microsecond:+ value, unit = float64(d)/float64(time.Microsecond), "µs"+ default:+ value, unit = float64(d), "ns"+ }+ 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..955abb5bd 100644@@ -14,9 +14,11 @@ package metrics import (+ "fmt" "html/template" "strings" "testing"+ "time" "github.com/gohugoio/hugo/resources/page" @@ -58,6 +60,77 @@ func TestSimilarPercentageNonString(t *testing.T) { c.Assert(howSimilar(testStruct{Name: "A"}, testStruct{Name: "A"}), qt.Equals, 100) } +func TestFormatDuration(t *testing.T) {+ c := qt.New(t)++ c.Assert(formatDuration(0), qt.Equals, "0.00ns")+ c.Assert(formatDuration(500*time.Nanosecond), qt.Equals, "500.00ns")+ c.Assert(formatDuration(1500*time.Nanosecond), qt.Equals, "1.50µs")+ c.Assert(formatDuration(12345*time.Microsecond), qt.Equals, "12.35ms")+ c.Assert(formatDuration(time.Second), qt.Equals, "1.00s ")+ c.Assert(formatDuration(2*time.Minute+3*time.Second), qt.Equals, "123.00s ")+}++func TestWriteMetricsAlignedDurations(t *testing.T) {+ durations := map[string][]time.Duration{+ "a.html": {1234567890 * time.Nanosecond, 2 * time.Second},+ "b.html": {12345 * time.Microsecond},+ "c.html": {1500 * time.Nanosecond, 2500 * time.Nanosecond},+ "d.html": {700 * time.Nanosecond},+ }++ for _, hints := range []bool{false, true} {+ t.Run(fmt.Sprintf("hints=%t", hints), func(t *testing.T) {+ c := qt.New(t)+ s := NewProvider(hints).(*Store)+ for k, v := range durations {+ s.metrics[k] = v+ }++ var b strings.Builder+ s.WriteMetrics(&b)++ lines := strings.Split(strings.TrimRight(b.String(), "\n"), "\n")+ c.Assert(lines, qt.HasLen, 3+len(durations))+ rows := lines[3:]++ // Rows are sorted by cumulative duration, descending.+ for i, name := range []string{"a.html", "b.html", "c.html", "d.html"} {+ c.Assert(strings.HasSuffix(rows[i], " "+name), qt.IsTrue, qt.Commentf("row %d: %q", i, rows[i]))+ }++ fields := strings.Fields(rows[0])+ c.Assert(fields[:3], qt.DeepEquals, []string{"3.23s", "1.62s", "2.00s"})+ fields = strings.Fields(rows[2])+ c.Assert(fields[:3], qt.DeepEquals, []string{"4.00µs", "2.00µs", "2.50µs"})++ // The decimal points of each duration column must line up across all rows.+ for col := range 3 {+ want := -1+ for _, row := range rows {+ runes := []rune(row)+ // Locate the col'th decimal point in the row.+ pos, seen := -1, 0+ for j, r := range runes {+ if r == '.' {+ if seen == col {+ pos = j+ break+ }+ seen+++ }+ }+ c.Assert(pos, qt.Not(qt.Equals), -1, qt.Commentf("row %q", row))+ if want == -1 {+ want = pos+ }+ c.Assert(pos, qt.Equals, want, qt.Commentf("column %d misaligned in row %q", col, row))+ }+ }+ })+ }+}+ func BenchmarkHowSimilar(b *testing.B) { s1 := "Hugo is cool and " + strings.Repeat("fun ", 10) + "!" s2 := "Hugo is cool and " + strings.Repeat("cool ", 10) + "!"
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, "")