| 1 |
// Copyright 2020 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
// Package debug provides template functions to help debugging templates. |
| 15 |
package debug |
| 16 |
|
| 17 |
import ( |
| 18 |
"encoding/json" |
| 19 |
"sort" |
| 20 |
"sync" |
| 21 |
"time" |
| 22 |
|
| 23 |
"github.com/bep/logg" |
| 24 |
"github.com/spf13/cast" |
| 25 |
"github.com/yuin/goldmark/util" |
| 26 |
|
| 27 |
"github.com/gohugoio/hugo/common/hugo" |
| 28 |
"github.com/gohugoio/hugo/deps" |
| 29 |
) |
| 30 |
|
| 31 |
// New returns a new instance of the debug-namespaced template functions. |
| 32 |
func New(d *deps.Deps) *Namespace { |
| 33 |
ns := &Namespace{} |
| 34 |
if d.Log.Level() <= logg.LevelInfo { |
| 35 |
ns.timers = make(map[string][]*timer) |
| 36 |
} |
| 37 |
|
| 38 |
if ns.timers == nil { |
| 39 |
return ns |
| 40 |
} |
| 41 |
|
| 42 |
l := d.Log.InfoCommand("timer") |
| 43 |
|
| 44 |
d.BuildEndListeners.Add(func(...any) bool { |
| 45 |
type data struct { |
| 46 |
Name string |
| 47 |
Count int |
| 48 |
Average time.Duration |
| 49 |
Median time.Duration |
| 50 |
Duration time.Duration |
| 51 |
} |
| 52 |
|
| 53 |
var timersSorted []data |
| 54 |
|
| 55 |
for k, v := range ns.timers { |
| 56 |
var total time.Duration |
| 57 |
var median time.Duration |
| 58 |
sort.Slice(v, func(i, j int) bool { |
| 59 |
return v[i].elapsed < v[j].elapsed |
| 60 |
}) |
| 61 |
if len(v) > 0 { |
| 62 |
median = v[len(v)/2].elapsed |
| 63 |
} |
| 64 |
for _, t := range v { |
| 65 |
// Stop any running timers. |
| 66 |
t.Stop() |
| 67 |
total += t.elapsed |
| 68 |
|
| 69 |
} |
| 70 |
average := total / time.Duration(len(v)) |
| 71 |
timersSorted = append(timersSorted, data{k, len(v), average, median, total}) |
| 72 |
} |
| 73 |
|
| 74 |
sort.Slice(timersSorted, func(i, j int) bool { |
| 75 |
// Sort it so the slowest gets printed last. |
| 76 |
return timersSorted[i].Duration < timersSorted[j].Duration |
| 77 |
}) |
| 78 |
|
| 79 |
for _, t := range timersSorted { |
| 80 |
l.WithField("name", t.Name).WithField("count", t.Count). |
| 81 |
WithField("duration", t.Duration). |
| 82 |
WithField("average", t.Average). |
| 83 |
WithField("median", t.Median).Logf("") |
| 84 |
} |
| 85 |
|
| 86 |
ns.timers = make(map[string][]*timer) |
| 87 |
|
| 88 |
return false |
| 89 |
}) |
| 90 |
|
| 91 |
return ns |
| 92 |
} |
| 93 |
|
| 94 |
// Namespace provides template functions for the "debug" namespace. |
| 95 |
type Namespace struct { |
| 96 |
timersMu sync.Mutex |
| 97 |
timers map[string][]*timer |
| 98 |
} |
| 99 |
|
| 100 |
// Dump returns a object dump of val as a string. |
| 101 |
// Note that not every value passed to Dump will print so nicely, but |
| 102 |
// we'll improve on that. |
| 103 |
// |
| 104 |
// We recommend using the "go" Chroma lexer to format the output |
| 105 |
// nicely. |
| 106 |
// |
| 107 |
// Also note that the output from Dump may change from Hugo version to the next, |
| 108 |
// so don't depend on a specific output. |
| 109 |
func (ns *Namespace) Dump(val any) string { |
| 110 |
b, err := json.MarshalIndent(val, "", " ") |
| 111 |
if err != nil { |
| 112 |
return "" |
| 113 |
} |
| 114 |
return string(b) |
| 115 |
} |
| 116 |
|
| 117 |
// VisualizeSpaces returns a string with spaces replaced by a visible string. |
| 118 |
func (ns *Namespace) VisualizeSpaces(val any) string { |
| 119 |
s := cast.ToString(val) |
| 120 |
return string(util.VisualizeSpaces([]byte(s))) |
| 121 |
} |
| 122 |
|
| 123 |
func (ns *Namespace) Timer(name string) Timer { |
| 124 |
if ns.timers == nil { |
| 125 |
return nopTimer |
| 126 |
} |
| 127 |
ns.timersMu.Lock() |
| 128 |
defer ns.timersMu.Unlock() |
| 129 |
t := &timer{start: time.Now()} |
| 130 |
ns.timers[name] = append(ns.timers[name], t) |
| 131 |
return t |
| 132 |
} |
| 133 |
|
| 134 |
var nopTimer = nopTimerImpl{} |
| 135 |
|
| 136 |
type nopTimerImpl struct{} |
| 137 |
|
| 138 |
func (nopTimerImpl) Stop() string { |
| 139 |
return "" |
| 140 |
} |
| 141 |
|
| 142 |
// Timer is a timer that can be stopped. |
| 143 |
type Timer interface { |
| 144 |
// Stop stops the timer and returns an empty string. |
| 145 |
// Stop can be called multiple times, but only the first call will stop the timer. |
| 146 |
// If Stop is not called, the timer will be stopped when the build ends. |
| 147 |
Stop() string |
| 148 |
} |
| 149 |
|
| 150 |
type timer struct { |
| 151 |
start time.Time |
| 152 |
elapsed time.Duration |
| 153 |
stopOnce sync.Once |
| 154 |
} |
| 155 |
|
| 156 |
func (t *timer) Stop() string { |
| 157 |
t.stopOnce.Do(func() { |
| 158 |
t.elapsed = time.Since(t.start) |
| 159 |
}) |
| 160 |
// This is used in templates, we need to return something. |
| 161 |
return "" |
| 162 |
} |
| 163 |
|
| 164 |
// Internal template func, used in tests only. |
| 165 |
func (ns *Namespace) TestDeprecationInfo(item, alternative string) string { |
| 166 |
v := hugo.CurrentVersion |
| 167 |
hugo.Deprecate(item, alternative, v.String()) |
| 168 |
return "" |
| 169 |
} |
| 170 |
|
| 171 |
// Internal template func, used in tests only. |
| 172 |
func (ns *Namespace) TestDeprecationWarn(item, alternative string) string { |
| 173 |
v := hugo.CurrentVersion |
| 174 |
v.Minor -= 3 |
| 175 |
hugo.Deprecate(item, alternative, v.String()) |
| 176 |
return "" |
| 177 |
} |
| 178 |
|
| 179 |
// Internal template func, used in tests only. |
| 180 |
func (ns *Namespace) TestDeprecationErr(item, alternative string) string { |
| 181 |
v := hugo.CurrentVersion |
| 182 |
v.Minor -= 15 |
| 183 |
hugo.Deprecate(item, alternative, v.String()) |
| 184 |
return "" |
| 185 |
} |