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.

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