metrics: Add cached count tracking

Track cached partial executions and display more useful info when
showing the hints output. Also group cache hints and counters together
to the right of the timing measurements.

Sample output:

     cumulative       average       maximum      cache  percent  cached  total
       duration      duration      duration  potential   cached   count  count  template
     ----------      --------      --------  ---------  -------  ------  -----  --------
       2.6973ms     674.325µs       857.3µs          0        0       0      4  _internal/_default/rss.xml
       2.6295ms     657.375µs       861.3µs          0        0       0      4  _default/single.html
       1.2563ms     314.075µs       381.5µs          0        0       0      4  news/single.html
        799.2µs       399.6µs       573.3µs          0        0       0      2  _internal/_default/sitemap.xml
        667.3µs       66.73µs       238.6µs         84        0       0     10  partials/header.html
        656.7µs      328.35µs       459.4µs          0        0       0      2  _default/list.html
        479.7µs       47.97µs         158µs         92        0       0     10  partials/head.html
        323.3µs       323.3µs       323.3µs          0        0       0      1  _internal/alias.html
        200.7µs       200.7µs       200.7µs          0        0       0      1  _internal/_default/sitemapindex.xml
         36.1µs       6.016µs        32.6µs        100       33       2      6  partials/footer.html
         27.4µs        13.7µs        26.7µs          0        0       0      2  index.html

Fixes #8375
This commit is contained in:
Cameron Moore
2021-03-30 10:47:34 -05:00
committed by Bjørn Erik Pedersen
parent ea6bcd6944
commit d16228334d
3 changed files with 125 additions and 30 deletions

View File

@@ -93,23 +93,41 @@ func (c *contextWrapper) Set(in interface{}) string {
// Else, the rendered output will be returned:
// A string if the partial is a text/template, or template.HTML when html/template.
func (ns *Namespace) Include(name string, contextList ...interface{}) (interface{}, error) {
name = strings.TrimPrefix(name, "partials/")
name, result, err := ns.include(name, contextList...)
if err != nil {
return result, err
}
if ns.deps.Metrics != nil {
ns.deps.Metrics.TrackValue(name, result, false)
}
return result, nil
}
// include is a helper function that lookups and executes the named partial.
// Returns the final template name and the rendered output.
func (ns *Namespace) include(name string, contextList ...interface{}) (string, interface{}, error) {
var context interface{}
if len(contextList) > 0 {
context = contextList[0]
}
n := "partials/" + name
templ, found := ns.deps.Tmpl().Lookup(n)
var n string
if strings.HasPrefix(name, "partials/") {
n = name
} else {
n = "partials/" + name
}
templ, found := ns.deps.Tmpl().Lookup(n)
if !found {
// For legacy reasons.
templ, found = ns.deps.Tmpl().Lookup(n + ".html")
}
if !found {
return "", fmt.Errorf("partial %q not found", name)
return "", "", fmt.Errorf("partial %q not found", name)
}
var info tpl.ParseInfo
@@ -136,7 +154,7 @@ func (ns *Namespace) Include(name string, contextList ...interface{}) (interface
}
if err := ns.deps.Tmpl().Execute(templ, w, context); err != nil {
return "", err
return "", "", err
}
var result interface{}
@@ -149,11 +167,7 @@ func (ns *Namespace) Include(name string, contextList ...interface{}) (interface
result = template.HTML(w.(fmt.Stringer).String())
}
if ns.deps.Metrics != nil {
ns.deps.Metrics.TrackValue(templ.Name(), result)
}
return result, nil
return templ.Name(), result, nil
}
// IncludeCached executes and caches partial templates. The cache is created with name+variants as the key.
@@ -215,11 +229,16 @@ func (ns *Namespace) getOrCreate(key partialCacheKey, context interface{}) (resu
return p, nil
}
p, err = ns.Include(key.name, context)
var name string
name, p, err = ns.include(key.name, context)
if err != nil {
return nil, err
}
if ns.deps.Metrics != nil {
ns.deps.Metrics.TrackValue(name, p, true)
}
ns.cachedPartials.Lock()
defer ns.cachedPartials.Unlock()
// Double-check.