Backtestshugorun-20261013-011256#15027
metrics: --templateMetrics duration columns have inconsistent formatting
Our patch was judged better than the merged fix.
In this judgment, Patch A = our agent, Patch B = the merged fix.
Both patches fix the issue the same way. Each adds a formatDuration helper that picks s/ms/µs/ns, prints two fixed decimals, and pads the unit to a fixed width, so decimal points line up in the right-aligned %15s/%12s columns. Go's fmt width counts runes, so the multibyte 'µ' does not break alignment in either patch. Correctness is therefore essentially equal.
Tests decide it.
- Patch A unit-tests formatDuration across all unit boundaries, including 0, 999ns, the µs/ms/s ranges, and the padded 's '. It also adds TestWriteMetricsAlignedDurations, which drives WriteMetrics in both hints and no-hints modes with rows of mixed magnitudes. That test checks sort order and asserts that the rune positions of the decimal points match in every row. This directly tests the issue's requirement, and it would fail against the old time.Duration.String() output.
- Patch B only unit-tests formatDuration with four values and never checks alignment in the actual table.
Scope:
- Patch A keeps the number and unit adjacent ('1.50µs', '1.00s '), which matches the issue's 'unit suffix padded' wording. The existing partials integration test regex still matches this output, so no other tests need to change.
- Patch B inserts a space between the number and the unit, which forced it to change the regex in tpl/partials/partials_integration_test.go. The change is small but adds churn.
Craft is comparable; A's single Sprintf with a computed divisor and unit is slightly cleaner. Overall, A is better on tests and slightly better on scope.
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
33m 54s
Wall-clock time in the container
Full test suite
No new failures
173 passed · 4 skipped · 178 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
84vs 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..0ae77b754 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 a fixed two decimal places and a unit+// suffix padded to two characters, so that decimal points line up when+// the values are right aligned in a column.+func formatDuration(d time.Duration) string {+ var (+ div float64+ unit string+ )+ switch {+ case d >= time.Second:+ div, unit = float64(time.Second), "s"+ case d >= time.Millisecond:+ div, unit = float64(time.Millisecond), "ms"+ case d >= time.Microsecond:+ div, unit = float64(time.Microsecond), "µs"+ default:+ div, unit = 1, "ns"+ }+ return fmt.Sprintf("%.2f%-2s", float64(d)/div, unit)+}+ // A result represents the calculated results for a given metric. type result struct { key stringmetrics/metrics_test.goindex 83dc1433f..35304cc00 100644@@ -14,9 +14,11 @@ package metrics import (+ "bytes" "html/template" "strings" "testing"+ "time" "github.com/gohugoio/hugo/resources/page" @@ -66,3 +68,60 @@ 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(999*time.Nanosecond), qt.Equals, "999.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(90*time.Second), qt.Equals, "90.00s ")+}++func TestWriteMetricsAlignedDurations(t *testing.T) {+ for _, hints := range []bool{false, true} {+ t.Run(map[bool]string{false: "nohints", true: "hints"}[hints], func(t *testing.T) {+ c := qt.New(t)++ s := NewProvider(hints).(*Store)+ s.metrics["a.html"] = []time.Duration{2 * time.Second, 1500 * time.Millisecond}+ s.metrics["b.html"] = []time.Duration{12345 * time.Microsecond}+ s.metrics["c.html"] = []time.Duration{1500 * time.Nanosecond}+ s.metrics["d.html"] = []time.Duration{250 * time.Nanosecond}++ var b bytes.Buffer+ s.WriteMetrics(&b)++ lines := strings.Split(strings.TrimRight(b.String(), "\n"), "\n")+ c.Assert(lines, qt.HasLen, 7)+ rows := lines[3:]++ // Rows are sorted by cumulative duration.+ 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, "1.50µs")+ c.Assert(rows[3], qt.Contains, "250.00ns")++ // The decimal point of each duration column must be at the same+ // (rune) position in every row. The first three dots on each row+ // belong to the sum, avg and max columns.+ decimalPositions := func(line string) []int {+ var pos []int+ for i, r := range []rune(line) {+ if r == '.' {+ pos = append(pos, i)+ }+ }+ return pos[:3]+ }+ want := decimalPositions(rows[0])+ for _, row := range rows[1:] {+ c.Assert(decimalPositions(row), qt.DeepEquals, want, 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, "")