From: Andy Pan Date: Sat, 20 Aug 2022 11:21:39 +0000 (+0800) Subject: encoding/json: move some misplaced benchmark tests to bench_test.go X-Git-Tag: go1.20rc1~1434 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=1a8dfadbfe26978cba0d6ce57bf437a93f796da6;p=gostls13.git encoding/json: move some misplaced benchmark tests to bench_test.go Change-Id: I5987eed00ee825421abe62699a06e9b66499f35f Reviewed-on: https://go-review.googlesource.com/c/go/+/425016 Reviewed-by: Daniel Martí Reviewed-by: Ian Lance Taylor Run-TryBot: Ian Lance Taylor Reviewed-by: David Chase Auto-Submit: Ian Lance Taylor TryBot-Result: Gopher Robot --- diff --git a/src/encoding/json/bench_test.go b/src/encoding/json/bench_test.go index 133084976b..d3af0dc0ed 100644 --- a/src/encoding/json/bench_test.go +++ b/src/encoding/json/bench_test.go @@ -18,6 +18,7 @@ import ( "io" "os" "reflect" + "regexp" "runtime" "strings" "sync" @@ -508,3 +509,33 @@ func BenchmarkEncodeMarshaler(b *testing.B) { } }) } + +func BenchmarkEncoderEncode(b *testing.B) { + b.ReportAllocs() + type T struct { + X, Y string + } + v := &T{"foo", "bar"} + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + if err := NewEncoder(io.Discard).Encode(v); err != nil { + b.Fatal(err) + } + } + }) +} + +func BenchmarkNumberIsValid(b *testing.B) { + s := "-61657.61667E+61673" + for i := 0; i < b.N; i++ { + isValidNumber(s) + } +} + +func BenchmarkNumberIsValidRegexp(b *testing.B) { + var jsonNumberRegexp = regexp.MustCompile(`^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$`) + s := "-61657.61667E+61673" + for i := 0; i < b.N; i++ { + jsonNumberRegexp.MatchString(s) + } +} diff --git a/src/encoding/json/number_test.go b/src/encoding/json/number_test.go index cc6701814f..c82e6deb83 100644 --- a/src/encoding/json/number_test.go +++ b/src/encoding/json/number_test.go @@ -116,18 +116,3 @@ func TestNumberIsValid(t *testing.T) { } } } - -func BenchmarkNumberIsValid(b *testing.B) { - s := "-61657.61667E+61673" - for i := 0; i < b.N; i++ { - isValidNumber(s) - } -} - -func BenchmarkNumberIsValidRegexp(b *testing.B) { - var jsonNumberRegexp = regexp.MustCompile(`^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$`) - s := "-61657.61667E+61673" - for i := 0; i < b.N; i++ { - jsonNumberRegexp.MatchString(s) - } -} diff --git a/src/encoding/json/stream_test.go b/src/encoding/json/stream_test.go index 1f40c79670..712293de0f 100644 --- a/src/encoding/json/stream_test.go +++ b/src/encoding/json/stream_test.go @@ -347,21 +347,6 @@ func TestBlocking(t *testing.T) { } } -func BenchmarkEncoderEncode(b *testing.B) { - b.ReportAllocs() - type T struct { - X, Y string - } - v := &T{"foo", "bar"} - b.RunParallel(func(pb *testing.PB) { - for pb.Next() { - if err := NewEncoder(io.Discard).Encode(v); err != nil { - b.Fatal(err) - } - } - }) -} - type tokenStreamCase struct { json string expTokens []any