mirror of
https://github.com/golang/go
synced 2024-11-14 22:50:27 -07:00
6a44a3aa9f
The go1 benchmark suite does a lot of work at package init time, which makes it take quite a while to run even if you're not running any of the benchmarks, or if you're only running a subset of them. This leads to an awkward workaround in dist test to compile but not run the package, unlike roughly all other packages. It also reduces isolation between benchmarks by affecting the starting heap size of all benchmarks. Fix this by initializing all data required by a benchmark when that benchmark runs, and keeping it local so it gets freed by the GC and doesn't leak between benchmarks. Now, none of the benchmarks depend on global state. Re-initializing the data on each benchmark run does add overhead to an actual benchmark run, as each benchmark function is called several times with different values of b.N. A full run of all benchmarks at the default -benchtime=1s now takes ~10% longer; higher -benchtimes would be less. It would be quite difficult to cache this data between invocations of the same benchmark function without leaking between different benchmarks and affecting GC overheads, as the testing package doesn't provide any mechanism for this. This reduces the time to run the binary with no benchmarks from 1.5 seconds to 10 ms, and also reduces the memory required to do this from 342 MiB to 17 MiB. To make sure data was not leaking between different benchmarks, I ran the benchmarks with -shuffle=on. The variance remained low: mostly under 3%. A few benchmarks had higher variance, but in all cases it was similar to the variance between this change. This CL naturally changes the measured performance of several of the benchmarks because it dramatically changes the heap size and hence GC overheads. However, going forward the benchmarks should be much better isolated. For #37486. Change-Id: I252ebea703a9560706cc1990dc5ad22d1927c7a0 Reviewed-on: https://go-review.googlesource.com/c/go/+/443336 TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Michael Pratt <mpratt@google.com> Run-TryBot: Austin Clements <austin@google.com>
82 lines
1.7 KiB
Go
82 lines
1.7 KiB
Go
// Copyright 2011 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
// This benchmark tests text/template throughput,
|
|
// converting a large data structure with a simple template.
|
|
|
|
package go1
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"strings"
|
|
"testing"
|
|
"text/template"
|
|
)
|
|
|
|
// After removing \t and \n this generates identical output to
|
|
// json.Marshal, making it easy to test for correctness.
|
|
const tmplText = `
|
|
{
|
|
"tree":{{template "node" .Tree}},
|
|
"username":"{{.Username}}"
|
|
}
|
|
{{define "node"}}
|
|
{
|
|
"name":"{{.Name}}",
|
|
"kids":[
|
|
{{range $i, $k := .Kids}}
|
|
{{if $i}}
|
|
,
|
|
{{end}}
|
|
{{template "node" $k}}
|
|
{{end}}
|
|
],
|
|
"cl_weight":{{.CLWeight}},
|
|
"touches":{{.Touches}},
|
|
"min_t":{{.MinT}},
|
|
"max_t":{{.MaxT}},
|
|
"mean_t":{{.MeanT}}
|
|
}
|
|
{{end}}
|
|
`
|
|
|
|
func stripTabNL(r rune) rune {
|
|
if r == '\t' || r == '\n' {
|
|
return -1
|
|
}
|
|
return r
|
|
}
|
|
|
|
func makeTemplate(jsonbytes []byte, jsondata *JSONResponse) *template.Template {
|
|
tmpl := template.Must(template.New("main").Parse(strings.Map(stripTabNL, tmplText)))
|
|
|
|
var buf bytes.Buffer
|
|
if err := tmpl.Execute(&buf, &jsondata); err != nil {
|
|
panic(err)
|
|
}
|
|
if !bytes.Equal(buf.Bytes(), jsonbytes) {
|
|
println(buf.Len(), len(jsonbytes))
|
|
panic("wrong output")
|
|
}
|
|
return tmpl
|
|
}
|
|
|
|
func tmplexec(tmpl *template.Template, jsondata *JSONResponse) {
|
|
if err := tmpl.Execute(io.Discard, jsondata); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func BenchmarkTemplate(b *testing.B) {
|
|
jsonbytes := makeJsonBytes()
|
|
jsondata := makeJsonData(jsonbytes)
|
|
tmpl := makeTemplate(jsonbytes, jsondata)
|
|
b.ResetTimer()
|
|
b.SetBytes(int64(len(jsonbytes)))
|
|
for i := 0; i < b.N; i++ {
|
|
tmplexec(tmpl, jsondata)
|
|
}
|
|
}
|