Merge pull request #138 from prestonvanloon/benchmark-dat-serializer

Benchmarking Serializer

Former-commit-id: 15e19fc351b301beea895f6f135a4bfb7d97c5ff [formerly 0fb1ab8e8ae8d8713f87d558b89e0718837dd3c3]
Former-commit-id: 4aa813f4fb23f61edeb4abab07b757bbaf8c578a
This commit is contained in:
Nishant Das
2018-05-25 21:39:28 +08:00
committed by GitHub
2 changed files with 51 additions and 35 deletions

View File

@@ -111,26 +111,6 @@ func (c *Collation) CalculateChunkRoot() {
c.header.data.ChunkRoot = &chunkRoot
}
// CreateRawBlobs creates raw blobs from transactions.
func (c Collation) CreateRawBlobs() ([]*utils.RawBlob, error) {
// It does not skip evm execution by default
blobs := make([]*utils.RawBlob, len(c.transactions))
for i := 0; i < len(c.transactions); i++ {
err := error(nil)
blobs[i], err = utils.NewRawBlob(c.transactions[i], false)
if err != nil {
return nil, fmt.Errorf("Creation of raw blobs from transactions failed: %v", err)
}
}
return blobs, nil
}
// ConvertBackToTx converts raw blobs back to their original transactions.
func ConvertBackToTx(rawBlobs []utils.RawBlob) ([]*types.Transaction, error) {
@@ -149,33 +129,33 @@ func ConvertBackToTx(rawBlobs []utils.RawBlob) ([]*types.Transaction, error) {
}
// Serialize method serializes the collation body to a byte array.
func (c *Collation) Serialize() ([]byte, error) {
// SerializeTxToBlob method serializes the input tx
// and returns the blobs in byte array.
func SerializeTxToBlob(txs []*types.Transaction) ([]byte, error) {
blobs, err := c.CreateRawBlobs()
if err != nil {
return nil, fmt.Errorf("%v", err)
blobs := make([]*utils.RawBlob, len(txs))
for i := 0; i < len(txs); i++ {
err := error(nil)
blobs[i], err = utils.NewRawBlob(txs[i], false)
if err != nil {
return nil, fmt.Errorf("%v", err)
}
}
serializedTx, err := utils.Serialize(blobs)
if err != nil {
return nil, fmt.Errorf("%v", err)
}
if int64(len(serializedTx)) > collationSizelimit {
return nil, fmt.Errorf("The serialized body exceeded the collation size limit: %v", serializedTx)
}
return serializedTx, nil
}
// Deserialize takes a byte array and converts its back to its original transactions.
func Deserialize(serialisedBlob []byte) (*[]*types.Transaction, error) {
// DeserializeBlobToTx takes byte array blob and converts it back
// to original txs and returns the txs in tx array.
func DeserializeBlobToTx(serialisedBlob []byte) (*[]*types.Transaction, error) {
deserializedBlobs, err := utils.Deserialize(serialisedBlob)
if err != nil {

View File

@@ -2,6 +2,7 @@ package sharding
import (
"bytes"
"crypto/rand"
"math/big"
"reflect"
"testing"
@@ -56,13 +57,13 @@ func TestSerialize_Deserialize(t *testing.T) {
tx := c.transactions
results, err := c.Serialize()
results, err := SerializeTxToBlob(tx)
if err != nil {
t.Errorf("Unable to Serialize transactions, %v", err)
}
deserializedTxs, err := Deserialize(results)
deserializedTxs, err := DeserializeBlobToTx(results)
if err != nil {
t.Errorf("Unable to deserialize collation body, %v", err)
@@ -126,3 +127,38 @@ func TestSerialize_Deserialize(t *testing.T) {
func makeTxWithGasLimit(gl uint64) *types.Transaction {
return types.NewTransaction(0 /*nonce*/, common.HexToAddress("0x0") /*to*/, nil /*amount*/, gl, nil /*gasPrice*/, nil /*data*/)
}
// BENCHMARK TESTS
// Helper function to generate test that completes round trip serialization tests for a specific number of transactions.
func runBenchTest(b *testing.B, numTransactions int) {
var txs []*types.Transaction
for i := 0; i < numTransactions; i++ {
data := make([]byte, 650)
rand.Read(data)
txs = append(txs, types.NewTransaction(0 /*nonce*/, common.HexToAddress("0x0") /*to*/, nil /*amount*/, 0 /*gasLimit*/, nil /*gasPrice*/, data))
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
results, _ := SerializeTxToBlob(txs)
_, _ = DeserializeBlobToTx(results)
}
}
func BenchmarkSerialization10(b *testing.B) {
runBenchTest(b, 10)
}
func BenchmarkSerialization100(b *testing.B) {
runBenchTest(b, 100)
}
func BenchmarkSerialization1000(b *testing.B) {
runBenchTest(b, 1000)
}
func BenchmarkSerialization10000(b *testing.B) {
runBenchTest(b, 10000)
}