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.

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