You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
2.7 KiB

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