Write the bar chart's latency the way its table does
The ranked bars said "8825 ms" beside a table that said "8.8 s". One formatter, humanLatency, now serves both, with a test. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
c1926be869
commit
be9f45aa94
3 files changed
+24
-14
No files matched your search
@@ -145,3 +145,11 @@ func TestTheRankedWrapperIsNotGivenAFixedHeight(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLatencyReadsAsAPersonWouldSayIt(t *testing.T) {
|
||||
for ms, want := range map[float64]string{5: "5 ms", 8825: "8.8 s", 110695: "1m 50s", 225634: "3m 45s"} {
|
||||
if got := humanLatency(ms); got != want {
|
||||
t.Errorf("%v ms: got %q, want %q", ms, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -229,7 +229,7 @@ func (d *DashboardData) addPeopleAndModels(ctx context.Context, usage UsageSourc
|
||||
rows = append(rows, Row{
|
||||
Label: m.Model,
|
||||
Value: float64(m.Requests),
|
||||
Note: fmt.Sprintf("%.0f ms", m.AvgLatency),
|
||||
Note: humanLatency(m.AvgLatency),
|
||||
})
|
||||
}
|
||||
d.ModelChart = HBarChart(rows, "--viz-3", "req")
|
||||
|
||||
+15
-13
@@ -44,19 +44,7 @@ var funcs = template.FuncMap{
|
||||
}
|
||||
return strings.Join(v, ", ")
|
||||
},
|
||||
// latency writes a mean latency the way a person reads one: 5 ms, 8.8 s,
|
||||
// 1m 51s — not 110695 ms.
|
||||
"latency": func(ms float64) string {
|
||||
switch {
|
||||
case ms < 1000:
|
||||
return fmt.Sprintf("%.0f ms", ms)
|
||||
case ms < 60_000:
|
||||
return fmt.Sprintf("%.1f s", ms/1000)
|
||||
default:
|
||||
d := time.Duration(ms) * time.Millisecond
|
||||
return fmt.Sprintf("%dm %02ds", int(d.Minutes()), int(d.Seconds())%60)
|
||||
}
|
||||
},
|
||||
"latency": humanLatency,
|
||||
"duration": func(sec int64) string {
|
||||
return time.Duration(sec * int64(time.Second)).Round(time.Second).String()
|
||||
},
|
||||
@@ -208,3 +196,17 @@ func render(w http.ResponseWriter, name string, data any) {
|
||||
http.Error(w, "template error", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
// humanLatency writes a mean latency the way a person reads one: 5 ms, 8.8 s,
|
||||
// 1m 51s — not 110695 ms.
|
||||
func humanLatency(ms float64) string {
|
||||
switch {
|
||||
case ms < 1000:
|
||||
return fmt.Sprintf("%.0f ms", ms)
|
||||
case ms < 60_000:
|
||||
return fmt.Sprintf("%.1f s", ms/1000)
|
||||
default:
|
||||
d := time.Duration(ms) * time.Millisecond
|
||||
return fmt.Sprintf("%dm %02ds", int(d.Minutes()), int(d.Seconds())%60)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user