mirror of
https://github.com/gohugoio/hugo.git
synced 2025-04-27 06:00:25 +03:00
tpl/debug: Add average and median to timer output
This commit is contained in:
parent
5160c7efa5
commit
46bdc03885
2 changed files with 20 additions and 6 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue