diff --git a/pkg/vm/stackitem/json_test.go b/pkg/vm/stackitem/json_test.go index 9536d403e..d1ce23fc0 100644 --- a/pkg/vm/stackitem/json_test.go +++ b/pkg/vm/stackitem/json_test.go @@ -105,6 +105,28 @@ func TestFromToJSON(t *testing.T) { }) } +// getBigArray returns array takes up a lot of storage when serialized. +func getBigArray(depth int) *Array { + arr := NewArray([]Item{}) + for i := 0; i < depth; i++ { + arr = NewArray([]Item{arr, arr}) + } + return arr +} + +func BenchmarkToJSON(b *testing.B) { + arr := getBigArray(15) + + b.ResetTimer() + b.ReportAllocs() + for i := 0; i < b.N; i++ { + _, err := ToJSON(arr) + if err != nil { + b.FailNow() + } + } +} + // This test is taken from the C# code // https://github.com/neo-project/neo/blob/master/tests/neo.UnitTests/VM/UT_Helper.cs#L30 func TestToJSONWithTypeCompat(t *testing.T) { diff --git a/pkg/vm/stackitem/serialization_test.go b/pkg/vm/stackitem/serialization_test.go index 02b07cb24..34f38820a 100644 --- a/pkg/vm/stackitem/serialization_test.go +++ b/pkg/vm/stackitem/serialization_test.go @@ -4,6 +4,7 @@ import ( "errors" "testing" + "github.com/nspcc-dev/neo-go/pkg/io" "github.com/stretchr/testify/require" ) @@ -21,3 +22,19 @@ func TestSerializationMaxErr(t *testing.T) { _, err = Serialize(aitem) require.True(t, errors.Is(err, ErrTooBig), err) } + +func BenchmarkEncodeBinary(b *testing.B) { + arr := getBigArray(15) + + w := io.NewBufBinWriter() + + b.ResetTimer() + b.ReportAllocs() + for i := 0; i < b.N; i++ { + w.Reset() + EncodeBinary(arr, w.BinWriter) + if w.Err != nil { + b.FailNow() + } + } +}