1
0
mirror of https://github.com/golang/go synced 2024-11-14 22:30:26 -07:00
go/test/bench/go1/json_test.go
Austin Clements 6a44a3aa9f test/bench/go1: eliminate start-up time
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>
2022-11-01 17:07:14 +00:00

87 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 JSON encoding and decoding performance.
package go1
import (
"bytes"
"compress/bzip2"
"encoding/base64"
"encoding/json"
"io"
"testing"
)
func makeJsonBytes() []byte {
var r io.Reader
r = bytes.NewReader(bytes.Replace(jsonbz2_base64, []byte{'\n'}, nil, -1))
r = base64.NewDecoder(base64.StdEncoding, r)
r = bzip2.NewReader(r)
b, err := io.ReadAll(r)
if err != nil {
panic(err)
}
return b
}
func makeJsonData(jsonbytes []byte) *JSONResponse {
var v JSONResponse
if err := json.Unmarshal(jsonbytes, &v); err != nil {
panic(err)
}
return &v
}
type JSONResponse struct {
Tree *JSONNode `json:"tree"`
Username string `json:"username"`
}
type JSONNode struct {
Name string `json:"name"`
Kids []*JSONNode `json:"kids"`
CLWeight float64 `json:"cl_weight"`
Touches int `json:"touches"`
MinT int64 `json:"min_t"`
MaxT int64 `json:"max_t"`
MeanT int64 `json:"mean_t"`
}
func jsondec(bytes []byte) {
var r JSONResponse
if err := json.Unmarshal(bytes, &r); err != nil {
panic(err)
}
_ = r
}
func jsonenc(data *JSONResponse) {
buf, err := json.Marshal(data)
if err != nil {
panic(err)
}
_ = buf
}
func BenchmarkJSONEncode(b *testing.B) {
jsonbytes := makeJsonBytes()
jsondata := makeJsonData(jsonbytes)
b.ResetTimer()
b.SetBytes(int64(len(jsonbytes)))
for i := 0; i < b.N; i++ {
jsonenc(jsondata)
}
}
func BenchmarkJSONDecode(b *testing.B) {
jsonbytes := makeJsonBytes()
b.ResetTimer()
b.SetBytes(int64(len(jsonbytes)))
for i := 0; i < b.N; i++ {
jsondec(jsonbytes)
}
}