neoneo-go/pkg/rpc/request/types_test.go
Evgeniy Stratonikov 4ab18d084a rpc/request: add unmarshal benchmark
Signed-off-by: Evgeniy Stratonikov <evgeniy@nspcc.ru>
2021-11-01 17:21:04 +03:00

49 lines
1.1 KiB
Go

package request
import (
"bytes"
"encoding/json"
"io"
"testing"
)
type readCloser struct {
io.Reader
}
func (readCloser) Close() error { return nil }
func BenchmarkUnmarshal(b *testing.B) {
req := []byte(`{"jsonrpc":"2.0", "method":"invokefunction","params":["0x50befd26fdf6e4d957c11e078b24ebce6291456f", "someMethod", [{"type": "String", "value": "50befd26fdf6e4d957c11e078b24ebce6291456f"}, {"type": "Integer", "value": "42"}, {"type": "Boolean", "value": false}]]}`)
b.Run("single", func(b *testing.B) {
b.ReportAllocs()
b.Run("unmarshal", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
b.StopTimer()
in := new(In)
b.StartTimer()
err := json.Unmarshal(req, in)
if err != nil {
b.FailNow()
}
}
})
b.Run("decode data", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
b.StopTimer()
r := NewRequest()
r.In = new(In)
rd := bytes.NewReader(req)
b.StartTimer()
err := r.DecodeData(readCloser{rd})
if err != nil {
b.FailNow()
}
}
})
})
}