Backtestshugorun-20261009-153657#15027

Issue #15027 Merged fix PR # Run 9 Oct 2026 · run-20261009-153657 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 fix the root cause the same way. They replace `time.Duration.String()` with a fixed two-decimal formatter whose unit suffix has a constant width, so decimal points line up in the right-aligned columns. Patch A's suffix is 3 runes (`"  s"`, `" ms"`, `" µs"`); Patch B's is 2 (`"s "`, `"ms"`, etc. via `%-2s`). Go's fmt pads by rune, so `µs` is handled correctly in both.

Correctness: both resolve the issue. Patch B also handles rounding carry-over, so 999.996ms prints as `1.00s ` instead of `1000.00 ms`. Patch A's output still aligns in that case, but the value reads oddly, so B is a little more polished.

Tests: this is where B clearly wins. Patch A only unit-tests `formatDuration` on four values. It also had to loosen the partials integration test regex (`\s*` between number and unit) because its new space-separated format would otherwise break that test. Patch B unit-tests the formatter, including zero and the rounding-carry boundaries. It also adds an end-to-end `WriteMetrics` test, run with and without hints, that checks decimal-point positions across rows of different magnitudes. That test directly checks the issue's stated goal (aligned decimal points) and would fail on the old code. B's compact format (`1.23ms`) matches the existing integration-test regex, so no test had to be weakened.

Scope and craft: both changes are focused. B's formatter is cleaner, with a doc comment and no duplicated `Sprintf` per branch. A's double-space `"%.2f  s"` with an inline comment is slightly awkward.

Overall, B has stronger, issue-targeted tests and handles the rounding edge case.

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

49m 32s

Wall-clock time in the container

Full test suite

No new failures

174 passed · 4 skipped · 178 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

113vs 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+111 −2
metrics/metrics.goindex 08d9322ab..4074ef7db 100644@@ -180,13 +180,46 @@ 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 decimal points line up when printed right-aligned+// in a table column.+func formatDuration(d time.Duration) string {+	var (+		unit string+		div  time.Duration+	)+	switch {+	case d >= time.Second:+		unit, div = "s", time.Second+	case d >= time.Millisecond:+		unit, div = "ms", time.Millisecond+	case d >= time.Microsecond:+		unit, div = "µs", time.Microsecond+	default:+		unit, div = "ns", time.Nanosecond+	}+	s := fmt.Sprintf("%.2f", float64(d)/float64(div))+	if s == "1000.00" {+		// Rounding carried over into the next unit, e.g. 999.996ms.+		switch unit {+		case "ms":+			s, unit = "1.00", "s"+		case "µs":+			s, unit = "1.00", "ms"+		case "ns":+			s, unit = "1.00", "µs"+		}+	}+	return fmt.Sprintf("%s%-2s", s, unit)+}+ // A result represents the calculated results for a given metric. type result struct { 	key         stringmetrics/metrics_test.goindex 83dc1433f..6ec94e75f 100644@@ -14,9 +14,12 @@ package metrics  import (+	"bytes" 	"html/template" 	"strings" 	"testing"+	"time"+	"unicode/utf8"  	"github.com/gohugoio/hugo/resources/page" @@ -58,6 +61,79 @@ 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(5*time.Nanosecond), qt.Equals, "5.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.Millisecond), qt.Equals, "1.00ms")+	c.Assert(formatDuration(2*time.Second+345*time.Millisecond), qt.Equals, "2.35s ")+	c.Assert(formatDuration(90*time.Second), qt.Equals, "90.00s ")++	// Rounding up to 1000 carries over into the next unit.+	c.Assert(formatDuration(999999*time.Nanosecond), qt.Equals, "1.00ms")+	c.Assert(formatDuration(999999*time.Microsecond), qt.Equals, "1.00s ")+	c.Assert(formatDuration(999994*time.Microsecond), qt.Equals, "999.99ms")+}++func TestWriteMetricsDurationsAligned(t *testing.T) {+	durations := map[string][]time.Duration{+		"seconds.html": {2*time.Second + 123456789*time.Nanosecond, 1 * time.Second},+		"millis.html":  {12345678 * time.Nanosecond, 3 * time.Millisecond},+		"micros.html":  {123456 * time.Nanosecond, 5 * time.Microsecond},+		"nanos.html":   {500 * time.Nanosecond, 7 * time.Nanosecond},+		"round.html":   {time.Millisecond},+	}++	for _, calculateHints := range []bool{false, true} {+		t.Run("calculateHints="+map[bool]string{false: "false", true: "true"}[calculateHints], func(t *testing.T) {+			c := qt.New(t)+			s := NewProvider(calculateHints).(*Store)+			for k, v := range durations {+				s.metrics[k] = v+			}++			var b bytes.Buffer+			s.WriteMetrics(&b)++			lines := strings.Split(strings.TrimRight(b.String(), "\n"), "\n")+			rows := lines[3:]+			c.Assert(rows, qt.HasLen, len(durations))++			// The decimal points of the sum, avg and max columns must line up across all rows.+			var want []int+			for _, row := range rows {+				var dots []int+				pos := 0+				for _, r := range row {+					if r == '.' {+						dots = append(dots, pos)+					}+					pos+++				}+				c.Assert(len(dots) >= 3, qt.IsTrue, qt.Commentf("row %q", row))+				dots = dots[:3]+				if want == nil {+					want = dots+				}+				c.Assert(dots, qt.DeepEquals, want, qt.Commentf("row %q", row))+				c.Assert(utf8.ValidString(row), qt.IsTrue)+			}++			out := b.String()+			c.Assert(out, qt.Contains, "3.12s ")+			c.Assert(out, qt.Contains, "2.12s ")+			c.Assert(out, qt.Contains, "15.35ms")+			c.Assert(out, qt.Contains, "128.46µs")+			c.Assert(out, qt.Contains, "507.00ns")+			c.Assert(out, qt.Contains, "1.00ms")+			c.Assert(out, qt.Not(qt.Contains), "1ms ")+		})+	}+}+ func BenchmarkHowSimilar(b *testing.B) { 	s1 := "Hugo is cool and " + strings.Repeat("fun ", 10) + "!" 	s2 := "Hugo is cool and " + strings.Repeat("cool ", 10) + "!" 
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, "")