github.com/evdatsion/aphelion-dpos-bft@v0.32.1/benchmarks/codec_test.go (about)

     1  package benchmarks
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	amino "github.com/evdatsion/go-amino"
     8  
     9  	proto "github.com/evdatsion/aphelion-dpos-bft/benchmarks/proto"
    10  	"github.com/evdatsion/aphelion-dpos-bft/crypto/ed25519"
    11  	"github.com/evdatsion/aphelion-dpos-bft/p2p"
    12  	ctypes "github.com/evdatsion/aphelion-dpos-bft/rpc/core/types"
    13  )
    14  
    15  func testNodeInfo(id p2p.ID) p2p.DefaultNodeInfo {
    16  	return p2p.DefaultNodeInfo{
    17  		ProtocolVersion: p2p.ProtocolVersion{P2P: 1, Block: 2, App: 3},
    18  		ID_:             id,
    19  		Moniker:         "SOMENAME",
    20  		Network:         "SOMENAME",
    21  		ListenAddr:      "SOMEADDR",
    22  		Version:         "SOMEVER",
    23  		Other: p2p.DefaultNodeInfoOther{
    24  			TxIndex:    "on",
    25  			RPCAddress: "0.0.0.0:26657",
    26  		},
    27  	}
    28  }
    29  
    30  func BenchmarkEncodeStatusWire(b *testing.B) {
    31  	b.StopTimer()
    32  	cdc := amino.NewCodec()
    33  	ctypes.RegisterAmino(cdc)
    34  	nodeKey := p2p.NodeKey{PrivKey: ed25519.GenPrivKey()}
    35  	status := &ctypes.ResultStatus{
    36  		NodeInfo: testNodeInfo(nodeKey.ID()),
    37  		SyncInfo: ctypes.SyncInfo{
    38  			LatestBlockHash:   []byte("SOMEBYTES"),
    39  			LatestBlockHeight: 123,
    40  			LatestBlockTime:   time.Unix(0, 1234),
    41  		},
    42  		ValidatorInfo: ctypes.ValidatorInfo{
    43  			PubKey: nodeKey.PubKey(),
    44  		},
    45  	}
    46  	b.StartTimer()
    47  
    48  	counter := 0
    49  	for i := 0; i < b.N; i++ {
    50  		jsonBytes, err := cdc.MarshalJSON(status)
    51  		if err != nil {
    52  			panic(err)
    53  		}
    54  		counter += len(jsonBytes)
    55  	}
    56  
    57  }
    58  
    59  func BenchmarkEncodeNodeInfoWire(b *testing.B) {
    60  	b.StopTimer()
    61  	cdc := amino.NewCodec()
    62  	ctypes.RegisterAmino(cdc)
    63  	nodeKey := p2p.NodeKey{PrivKey: ed25519.GenPrivKey()}
    64  	nodeInfo := testNodeInfo(nodeKey.ID())
    65  	b.StartTimer()
    66  
    67  	counter := 0
    68  	for i := 0; i < b.N; i++ {
    69  		jsonBytes, err := cdc.MarshalJSON(nodeInfo)
    70  		if err != nil {
    71  			panic(err)
    72  		}
    73  		counter += len(jsonBytes)
    74  	}
    75  }
    76  
    77  func BenchmarkEncodeNodeInfoBinary(b *testing.B) {
    78  	b.StopTimer()
    79  	cdc := amino.NewCodec()
    80  	ctypes.RegisterAmino(cdc)
    81  	nodeKey := p2p.NodeKey{PrivKey: ed25519.GenPrivKey()}
    82  	nodeInfo := testNodeInfo(nodeKey.ID())
    83  	b.StartTimer()
    84  
    85  	counter := 0
    86  	for i := 0; i < b.N; i++ {
    87  		jsonBytes := cdc.MustMarshalBinaryBare(nodeInfo)
    88  		counter += len(jsonBytes)
    89  	}
    90  
    91  }
    92  
    93  func BenchmarkEncodeNodeInfoProto(b *testing.B) {
    94  	b.StopTimer()
    95  	nodeKey := p2p.NodeKey{PrivKey: ed25519.GenPrivKey()}
    96  	nodeID := string(nodeKey.ID())
    97  	someName := "SOMENAME"
    98  	someAddr := "SOMEADDR"
    99  	someVer := "SOMEVER"
   100  	someString := "SOMESTRING"
   101  	otherString := "OTHERSTRING"
   102  	nodeInfo := proto.NodeInfo{
   103  		Id:         &proto.ID{Id: &nodeID},
   104  		Moniker:    &someName,
   105  		Network:    &someName,
   106  		ListenAddr: &someAddr,
   107  		Version:    &someVer,
   108  		Other:      []string{someString, otherString},
   109  	}
   110  	b.StartTimer()
   111  
   112  	counter := 0
   113  	for i := 0; i < b.N; i++ {
   114  		bytes, err := nodeInfo.Marshal()
   115  		if err != nil {
   116  			b.Fatal(err)
   117  			return
   118  		}
   119  		//jsonBytes := wire.JSONBytes(nodeInfo)
   120  		counter += len(bytes)
   121  	}
   122  
   123  }