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.

129 lines
2.9 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"
  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: crypto.GenPrivKeyEd25519()}
  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: []string{"SOMESTRING", "OTHERSTRING"},
  24. },
  25. SyncInfo: ctypes.SyncInfo{
  26. LatestBlockHash: []byte("SOMEBYTES"),
  27. LatestBlockHeight: 123,
  28. LatestBlockTime: time.Unix(0, 1234),
  29. },
  30. ValidatorInfo: ctypes.ValidatorInfo{
  31. PubKey: nodeKey.PubKey(),
  32. },
  33. }
  34. b.StartTimer()
  35. counter := 0
  36. for i := 0; i < b.N; i++ {
  37. jsonBytes, err := cdc.MarshalJSON(status)
  38. if err != nil {
  39. panic(err)
  40. }
  41. counter += len(jsonBytes)
  42. }
  43. }
  44. func BenchmarkEncodeNodeInfoWire(b *testing.B) {
  45. b.StopTimer()
  46. cdc := amino.NewCodec()
  47. ctypes.RegisterAmino(cdc)
  48. nodeKey := p2p.NodeKey{PrivKey: crypto.GenPrivKeyEd25519()}
  49. nodeInfo := p2p.NodeInfo{
  50. ID: nodeKey.ID(),
  51. Moniker: "SOMENAME",
  52. Network: "SOMENAME",
  53. ListenAddr: "SOMEADDR",
  54. Version: "SOMEVER",
  55. Other: []string{"SOMESTRING", "OTHERSTRING"},
  56. }
  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: crypto.GenPrivKeyEd25519()}
  72. nodeInfo := p2p.NodeInfo{
  73. ID: nodeKey.ID(),
  74. Moniker: "SOMENAME",
  75. Network: "SOMENAME",
  76. ListenAddr: "SOMEADDR",
  77. Version: "SOMEVER",
  78. Other: []string{"SOMESTRING", "OTHERSTRING"},
  79. }
  80. b.StartTimer()
  81. counter := 0
  82. for i := 0; i < b.N; i++ {
  83. jsonBytes := cdc.MustMarshalBinaryBare(nodeInfo)
  84. counter += len(jsonBytes)
  85. }
  86. }
  87. func BenchmarkEncodeNodeInfoProto(b *testing.B) {
  88. b.StopTimer()
  89. nodeKey := p2p.NodeKey{PrivKey: crypto.GenPrivKeyEd25519()}
  90. nodeID := string(nodeKey.ID())
  91. someName := "SOMENAME"
  92. someAddr := "SOMEADDR"
  93. someVer := "SOMEVER"
  94. someString := "SOMESTRING"
  95. otherString := "OTHERSTRING"
  96. nodeInfo := proto.NodeInfo{
  97. Id: &proto.ID{Id: &nodeID},
  98. Moniker: &someName,
  99. Network: &someName,
  100. ListenAddr: &someAddr,
  101. Version: &someVer,
  102. Other: []string{someString, otherString},
  103. }
  104. b.StartTimer()
  105. counter := 0
  106. for i := 0; i < b.N; i++ {
  107. bytes, err := nodeInfo.Marshal()
  108. if err != nil {
  109. b.Fatal(err)
  110. return
  111. }
  112. //jsonBytes := wire.JSONBytes(nodeInfo)
  113. counter += len(bytes)
  114. }
  115. }