Skip to content

Commit

Permalink
benchmark for ssz vs json encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
metachris committed Oct 6, 2022
1 parent 5b07588 commit af14757
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions types/encoding_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package types

import (
"encoding/json"
"os"
"testing"

"github.com/stretchr/testify/require"
)

func BenchmarkJSONvsSSZEncoding(b *testing.B) {
var err error
jsonFile, err := os.Open("../testdata/signed-blinded-beacon-block-with-deposit.json")
require.NoError(b, err)
defer jsonFile.Close()
signedBlindedBeaconBlock := new(SignedBlindedBeaconBlock)
require.NoError(b, DecodeJSON(jsonFile, &signedBlindedBeaconBlock))

blindedBeaconBlock := signedBlindedBeaconBlock.Message
blindedBeaconBlockJSONBytes, err := json.Marshal(blindedBeaconBlock)
require.NoError(b, err)
blindedBeaconBlockSSZBytes, err := blindedBeaconBlock.MarshalSSZ()
require.NoError(b, err)

b.Run("Encode JSON", func(b *testing.B) {
for n := 0; n < b.N; n++ {
_, err = json.Marshal(blindedBeaconBlock)
require.NoError(b, err)
}
})

b.Run("Encode SSZ", func(b *testing.B) {
for n := 0; n < b.N; n++ {
_, err = blindedBeaconBlock.MarshalSSZ()
require.NoError(b, err)
}
})

b.Run("Decode JSON", func(b *testing.B) {
_blindedBeaconBlock := new(BlindedBeaconBlock)
for n := 0; n < b.N; n++ {
err = json.Unmarshal(blindedBeaconBlockJSONBytes, _blindedBeaconBlock)
require.NoError(b, err)
}
})

b.Run("Decode SSZ", func(b *testing.B) {
_blindedBeaconBlock := new(BlindedBeaconBlock)
for n := 0; n < b.N; n++ {
err = _blindedBeaconBlock.UnmarshalSSZ(blindedBeaconBlockSSZBytes)
require.NoError(b, err)
}
})
}

0 comments on commit af14757

Please sign in to comment.