neoneo-go/pkg/core/state/native_state_test.go
Evgeniy Stratonikov b210a34b1e state: optimize NEP17Balance deserialization
```
BenchmarkNEP17BalanceFromBytes/stackitem-8         	 2402318	       503.3 ns/op	     208 B/op	      10 allocs/op
BenchmarkNEP17BalanceFromBytes/from_bytes-8        	 7623139	       160.7 ns/op	      72 B/op	       3 allocs/op
```

Signed-off-by: Evgeniy Stratonikov <evgeniy@nspcc.ru>
2021-08-09 11:09:49 +03:00

95 lines
2.2 KiB
Go

package state
import (
"testing"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/stretchr/testify/require"
)
func TestNEP17Balance_Bytes(t *testing.T) {
var b NEP17Balance
b.Balance.SetInt64(0x12345678910)
data, err := stackitem.SerializeConvertible(&b)
require.NoError(t, err)
require.Equal(t, data, b.Bytes(nil))
t.Run("reuse buffer", func(t *testing.T) {
buf := make([]byte, 100)
ret := b.Bytes(buf[:0])
require.Equal(t, ret, buf[:len(ret)])
})
actual, err := NEP17BalanceFromBytes(data)
require.NoError(t, err)
require.Equal(t, &b, actual)
}
func TestNEP17BalanceFromBytesInvalid(t *testing.T) {
b, err := NEP17BalanceFromBytes(nil) // 0 is ok
require.NoError(t, err)
require.Equal(t, int64(0), b.Balance.Int64())
_, err = NEP17BalanceFromBytes([]byte{byte(stackitem.StructT)})
require.Error(t, err)
_, err = NEP17BalanceFromBytes([]byte{byte(stackitem.IntegerT), 4, 0, 1, 2, 3})
require.Error(t, err)
_, err = NEP17BalanceFromBytes([]byte{byte(stackitem.StructT), 0, byte(stackitem.IntegerT), 1, 1})
require.Error(t, err)
_, err = NEP17BalanceFromBytes([]byte{byte(stackitem.StructT), 1, byte(stackitem.ByteArrayT), 1, 1})
require.Error(t, err)
_, err = NEP17BalanceFromBytes([]byte{byte(stackitem.StructT), 1, byte(stackitem.IntegerT), 2, 1})
require.Error(t, err)
}
func BenchmarkNEP17BalanceBytes(b *testing.B) {
var bl NEP17Balance
bl.Balance.SetInt64(0x12345678910)
b.Run("stackitem", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = stackitem.SerializeConvertible(&bl)
}
})
b.Run("bytes", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = bl.Bytes(nil)
}
})
b.Run("bytes, prealloc", func(b *testing.B) {
bs := bl.Bytes(nil)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = bl.Bytes(bs[:0])
}
})
}
func BenchmarkNEP17BalanceFromBytes(b *testing.B) {
var bl NEP17Balance
bl.Balance.SetInt64(0x12345678910)
buf := bl.Bytes(nil)
b.Run("stackitem", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = stackitem.DeserializeConvertible(buf, new(NEP17Balance))
}
})
b.Run("from bytes", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = NEP17BalanceFromBytes(buf)
}
})
}