Backtestshugorun-20261007-020926#15027

Issue #15027 Merged fix PR # Run 7 Oct 2026 · run-20261007-020926 gohugoio/hugo

metrics: --templateMetrics duration columns have inconsistent formatting

Better

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 prints two fixed decimals with a fixed-width unit suffix and uses it for the sum/avg/max columns in both `WriteMetrics` branches. Each pads the unit so the decimal points align: Patch A uses "ns"/"µs"/"ms"/"s ", and Patch B uses " ns"/" µs"/" ms"/"  s". Go's fmt width counts runes, so the multi-byte µ does not break alignment in either one, and both are correct. Patch A also handles negative durations via `Abs`, which is harmless hardening.

The difference is in the tests and the side effects. Patch A keeps the value and unit adjacent, as before, so the existing duration-stripping regex in `partials_integration_test.go` still matches and no unrelated test needs changing. Patch B inserts a space between value and unit, which changes the output format and forces it to edit that integration test's regex. That is extra churn and a small risk for anything else that parses this output.

On tests, Patch A is stronger. It unit-tests `formatDuration`, including boundaries (0, exactly 1s, rounding). It also adds a `WriteMetrics` test, in both `calculateHints` modes, that checks the decimal point sits in the same rune column on every row for durations of mixed magnitude. That checks the issue's actual requirement and would fail against the old `Duration.String()` output. Patch B only unit-tests the helper with four values and never checks the table alignment.

One minor uncertainty: Patch A uses `strconv` without a visible import change, so it presumably relies on an existing import. Overall, Patch A is the more targeted and better-verified 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

46m 33s

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

88vs 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+86 −2
metrics/metrics.goindex 08d9322ab..3ad2abe2f 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 number of decimals and a unit suffix+// padded to a fixed width, so that decimal points line up when the values+// are right aligned in a table column.+func formatDuration(d time.Duration) string {+	var (+		div  time.Duration+		unit string+	)+	switch abs := d.Abs(); {+	case abs < time.Microsecond:+		div, unit = time.Nanosecond, "ns"+	case abs < time.Millisecond:+		div, unit = time.Microsecond, "µs"+	case abs < time.Second:+		div, unit = time.Millisecond, "ms"+	default:+		div, unit = time.Second, "s "+	}+	return strconv.FormatFloat(float64(d)/float64(div), 'f', 2, 64) + unit+}+ // A result represents the calculated results for a given metric. type result struct { 	key         stringmetrics/metrics_test.goindex 83dc1433f..b9e1a2275 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,66 @@ 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(90*time.Second+123*time.Millisecond), qt.Equals, "90.12s ")+}++func TestWriteMetricsDurationsAligned(t *testing.T) {+	for _, calculateHints := range []bool{false, true} {+		t.Run("", func(t *testing.T) {+			c := qt.New(t)++			s := NewProvider(calculateHints).(*Store)+			s.metrics["a.html"] = []time.Duration{2*time.Second + 345*time.Millisecond, 7 * time.Millisecond}+			s.metrics["b.html"] = []time.Duration{3456 * time.Microsecond}+			s.metrics["c.html"] = []time.Duration{12345 * time.Nanosecond, 3 * time.Nanosecond}+			s.metrics["d.html"] = []time.Duration{42 * time.Nanosecond}++			var buf bytes.Buffer+			s.WriteMetrics(&buf)++			lines := strings.Split(strings.TrimRight(buf.String(), "\n"), "\n")[3:]+			c.Assert(lines, qt.HasLen, 4)++			// Each of the sum, avg and max columns must have its decimal point+			// in the same (rune) column on every row.+			for col := range 3 {+				pos := -1+				for _, l := range lines {+					fields := strings.Fields(l)+					idx := 0+					for i := 0; i <= col; i++ {+						idx += strings.Index(l[idx:], fields[i])+						if i < col {+							idx += len(fields[i])+						}+					}+					c.Assert(strings.Count(fields[col], "."), qt.Equals, 1, qt.Commentf("line %q", l))+					p := utf8.RuneCountInString(l[:idx+strings.Index(fields[col], ".")])+					if pos == -1 {+						pos = p+					}+					c.Assert(p, qt.Equals, pos, qt.Commentf("column %d not aligned:\n%s", col, buf.String()))+				}+			}++			c.Assert(lines[0], qt.Contains, "2.35s ")+			c.Assert(lines[0], qt.Contains, "1.18s ")+			c.Assert(lines[1], qt.Contains, "3.46ms")+			c.Assert(lines[2], qt.Contains, "12.35µs")+			c.Assert(lines[2], qt.Contains, "6.17µs")+			c.Assert(lines[3], qt.Contains, "42.00ns")+		})+	}+}+ 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, "")