Backtestshugorun-20260917-015623#15027
metrics: --templateMetrics duration columns have inconsistent formatting
The harness couldn't finish this ticket.
The judge's reply could not be parsed.. No judgment was produced for this ticket. It counts as an error in the run total.
Solve
17m 23s1 turn
Wall-clock time in the container
Full test suite
No new failures
3,723 passed · 99 skipped · 3,828 total
Contamination audit
Unknown
See the audit notes for this run
Patch size
54vs 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..788597440 100644@@ -180,13 +180,32 @@ 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 a consistent width so that the decimal points align+// across rows in the metrics table.+func formatDuration(d time.Duration) string {+ var value float64+ var unit string+ switch {+ case d < time.Microsecond:+ value, unit = float64(d.Nanoseconds()), "ns"+ case d < time.Millisecond:+ value, unit = float64(d.Nanoseconds())/1e3, "µs"+ case d < time.Second:+ value, unit = float64(d.Nanoseconds())/1e6, "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..52041c91c 100644@@ -14,15 +14,46 @@ package metrics import (+ "bytes" "html/template" "strings" "testing"+ "time" "github.com/gohugoio/hugo/resources/page" qt "github.com/frankban/quicktest" ) +func TestWriteMetricsDurationFormatting(t *testing.T) {+ c := qt.New(t)++ s := &Store{+ metrics: map[string][]time.Duration{+ "a.html": {500 * time.Nanosecond},+ "b.html": {2 * time.Millisecond},+ "c.html": {1500 * time.Millisecond},+ },+ diffs: make(map[string]*diff),+ cached: make(map[string]int),+ }++ var buf bytes.Buffer+ s.WriteMetrics(&buf)+ out := buf.String()++ // Durations should be printed with a fixed two decimal places and a+ // unit suffix so the decimal points align across rows.+ c.Assert(out, qt.Contains, "500.00ns")+ c.Assert(out, qt.Contains, "2.00ms")+ c.Assert(out, qt.Contains, "1.50s")++ // The old time.Duration.String() forms must no longer appear.+ c.Assert(strings.Contains(out, "500ns"), qt.IsFalse)+ c.Assert(strings.Contains(out, "2ms"), qt.IsFalse)+ c.Assert(strings.Contains(out, "1.5s"), qt.IsFalse)+}+ func TestSimilarPercentage(t *testing.T) { c := qt.New(t)
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, "")
What the agent reported
Implementer done. Waiting for the test-writer to finish before running the gate.