Backtestshugorun-20261005-133023#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 `time.Duration.String()` with a fixed two-decimal formatter whose unit suffix has a constant width. Because the `%15s/%12s` verbs right-align and Go's fmt pads by rune count, the decimal points line up in either patch. So both fix the root cause of the issue.
Patch A puts a space before the unit ("4.34 s", "170.29 ms"). That changes the output shape enough that the existing partials integration test regex had to be loosened. Its rounding is also naive: 999.996µs prints as "1000.00 µs". That still aligns, but it is the wrong unit. A's test is a small table of `formatDuration` cases.
Patch B pads the unit with `%-2s` ("1.50s ", "12.35ms"), which keeps the existing integration-test regex working unchanged. It also handles values that would round up to 1000.00 by lowering the unit thresholds, and uses `d.Abs()` so negative durations are bucketed correctly. These are small edge cases, but they make the formatter more correct.
B's tests are stronger. They cover the boundary and rounding cases, zero, and multi-unit values. They also add an end-to-end `WriteMetrics` test, with and without hints, that checks the decimal-point column positions line up across rows. That is exactly what the issue asks for, and it would fail against the old `Duration.String()` output.
B's code is also clearer: it computes the formatted values once and has an explanatory doc comment. Overall B is equally correct on the core fix, handles edges better, and is better tested.
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
57m 30s
Wall-clock time in the container
Full test suite
No new failures
3,730 passed · 99 skipped · 3,831 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
85vs 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..9abd19ded 100644@@ -179,14 +179,38 @@ func (s *Store) WriteMetrics(w io.Writer) { sort.Sort(bySum(results)) for _, v := range results {+ sum, avg, max := formatDuration(v.sum), formatDuration(v.avg), formatDuration(v.max) 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", sum, avg, 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", sum, avg, max, v.count, v.key) } } } +// formatDuration formats d with two decimal places and a unit suffix+// padded to a fixed width, so the 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 := d.Abs(); {+ case abs < time.Microsecond:+ value, unit = float64(d), "ns"+ // The upper bounds are lowered by half of the last printed digit so+ // that values rounding up to 1000.00 move to the next unit.+ case abs < time.Millisecond-5*time.Nanosecond:+ value, unit = float64(d)/float64(time.Microsecond), "µs"+ case abs < time.Second-5*time.Microsecond:+ value, unit = float64(d)/float64(time.Millisecond), "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..de0086332 100644@@ -14,9 +14,12 @@ package metrics import (+ "bytes"+ "fmt" "html/template" "strings" "testing"+ "time" "github.com/gohugoio/hugo/resources/page" @@ -66,3 +69,57 @@ func BenchmarkHowSimilar(b *testing.B) { howSimilar(s1, s2) } }++func TestFormatDuration(t *testing.T) {+ c := qt.New(t)++ c.Assert(formatDuration(0), qt.Equals, "0.00ns")+ c.Assert(formatDuration(512*time.Nanosecond), qt.Equals, "512.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(1500*time.Millisecond), qt.Equals, "1.50s ")+ c.Assert(formatDuration(90*time.Second), qt.Equals, "90.00s ")++ // Values that would round up to 1000.00 move to the next unit.+ c.Assert(formatDuration(999994*time.Nanosecond), qt.Equals, "999.99µs")+ c.Assert(formatDuration(999996*time.Nanosecond), qt.Equals, "1.00ms")+ c.Assert(formatDuration(999999*time.Microsecond), qt.Equals, "1.00s ")+}++func TestWriteMetricsDurationsAligned(t *testing.T) {+ 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)+ s.metrics["a.html"] = []time.Duration{1500 * time.Millisecond, 2 * time.Second}+ s.metrics["b.html"] = []time.Duration{12345 * time.Microsecond}+ s.metrics["c.html"] = []time.Duration{1500 * time.Nanosecond, 3 * time.Microsecond}+ s.metrics["d.html"] = []time.Duration{512 * time.Nanosecond}++ var buf bytes.Buffer+ s.WriteMetrics(&buf)++ lines := strings.Split(strings.TrimRight(buf.String(), "\n"), "\n")+ c.Assert(lines, qt.HasLen, 7)+ rows := lines[3:]++ c.Assert(rows[0], qt.Contains, "3.50s ")+ c.Assert(rows[0], qt.Contains, "1.75s ")+ c.Assert(rows[0], qt.Contains, "2.00s ")+ c.Assert(rows[1], qt.Contains, "12.35ms")+ c.Assert(rows[2], qt.Contains, "4.50µs")+ c.Assert(rows[2], qt.Contains, "2.25µs")+ c.Assert(rows[3], qt.Contains, "512.00ns")++ // The decimal points of each duration column must line up across rows.+ for _, end := range []int{2 + 15, 2 + 15 + 2 + 12, 2 + 15 + 2 + 12 + 2 + 12} {+ // Decimal point is 5 runes before the column end: ".dd" + 2 unit runes.+ for _, row := range rows {+ r := []rune(row)+ c.Assert(string(r[end-5]), qt.Equals, ".", qt.Commentf("row %q", row))+ }+ }+ })+ }+}
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, "")