summaryrefslogtreecommitdiffstats
path: root/tpl
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2023-10-21 15:41:21 +0200
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2023-10-21 15:41:21 +0200
commit46bdc03885f18a82f282c442b7aa44c42cf20ced (patch)
tree0ca938a07f822d684bf791305c8b17a5f676415e /tpl
parent5160c7efa5771d74b560f9c2ea761f09e08a9216 (diff)
tpl/debug: Add average and median to timer output
Diffstat (limited to 'tpl')
-rw-r--r--tpl/debug/debug.go22
-rw-r--r--tpl/debug/integration_test.go4
2 files changed, 20 insertions, 6 deletions
diff --git a/tpl/debug/debug.go b/tpl/debug/debug.go
index 02fe562c3..dd0593e27 100644
--- a/tpl/debug/debug.go
+++ b/tpl/debug/debug.go
@@ -44,22 +44,33 @@ func New(d *deps.Deps) *Namespace {
l := d.Log.InfoCommand("timer")
d.BuildEndListeners.Add(func() {
- type nameCountDuration struct {
+ type data struct {
Name string
Count int
+ Average time.Duration
+ Median time.Duration
Duration time.Duration
}
- var timersSorted []nameCountDuration
+ var timersSorted []data
for k, v := range timers {
var total time.Duration
+ var median time.Duration
+ sort.Slice(v, func(i, j int) bool {
+ return v[i].elapsed < v[j].elapsed
+ })
+ if len(v) > 0 {
+ median = v[len(v)/2].elapsed
+ }
for _, t := range v {
// Stop any running timers.
t.Stop()
total += t.elapsed
+
}
- timersSorted = append(timersSorted, nameCountDuration{k, len(v), total})
+ average := total / time.Duration(len(v))
+ timersSorted = append(timersSorted, data{k, len(v), average, median, total})
}
sort.Slice(timersSorted, func(i, j int) bool {
@@ -68,7 +79,10 @@ func New(d *deps.Deps) *Namespace {
})
for _, t := range timersSorted {
- l.WithField("name", t.Name).WithField("count", t.Count).WithField("duration", t.Duration).Logf("")
+ l.WithField("name", t.Name).WithField("count", t.Count).
+ WithField("duration", t.Duration).
+ WithField("average", t.Average).
+ WithField("median", t.Median).Logf("")
}
ns.timers = make(map[string][]*timer)
diff --git a/tpl/debug/integration_test.go b/tpl/debug/integration_test.go
index 6520e60fb..3d120580d 100644
--- a/tpl/debug/integration_test.go
+++ b/tpl/debug/integration_test.go
@@ -26,7 +26,7 @@ func TestTimer(t *testing.T) {
baseURL = "https://example.org/"
disableKinds = ["taxonomy", "term"]
-- layouts/index.html --
-{{ range seq 2 }}
+{{ range seq 5 }}
{{ $t := debug.Timer "foo" }}
{{ seq 1 1000 }}
{{ $t.Stop }}
@@ -41,5 +41,5 @@ disableKinds = ["taxonomy", "term"]
},
).Build()
- b.AssertLogContains("imer: name \"foo\" count '\\x02' duration")
+ b.AssertLogContains("imer: name \"foo\" count '\\x05' duration")
}