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.

155 lines
3.9 KiB

  1. package light_test
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. dbm "github.com/tendermint/tm-db"
  7. "github.com/tendermint/tendermint/libs/log"
  8. "github.com/tendermint/tendermint/light"
  9. "github.com/tendermint/tendermint/light/provider"
  10. dbs "github.com/tendermint/tendermint/light/store/db"
  11. "github.com/tendermint/tendermint/types"
  12. )
  13. // NOTE: block is produced every minute. Make sure the verification time
  14. // provided in the function call is correct for the size of the blockchain. The
  15. // benchmarking may take some time hence it can be more useful to set the time
  16. // or the amount of iterations use the flag -benchtime t -> i.e. -benchtime 5m
  17. // or -benchtime 100x.
  18. //
  19. // Remember that none of these benchmarks account for network latency.
  20. var ()
  21. type providerBenchmarkImpl struct {
  22. currentHeight int64
  23. blocks map[int64]*types.LightBlock
  24. }
  25. func newProviderBenchmarkImpl(headers map[int64]*types.SignedHeader,
  26. vals map[int64]*types.ValidatorSet) provider.Provider {
  27. impl := providerBenchmarkImpl{
  28. blocks: make(map[int64]*types.LightBlock, len(headers)),
  29. }
  30. for height, header := range headers {
  31. if height > impl.currentHeight {
  32. impl.currentHeight = height
  33. }
  34. impl.blocks[height] = &types.LightBlock{
  35. SignedHeader: header,
  36. ValidatorSet: vals[height],
  37. }
  38. }
  39. return &impl
  40. }
  41. func (impl *providerBenchmarkImpl) LightBlock(ctx context.Context, height int64) (*types.LightBlock, error) {
  42. if height == 0 {
  43. return impl.blocks[impl.currentHeight], nil
  44. }
  45. lb, ok := impl.blocks[height]
  46. if !ok {
  47. return nil, provider.ErrLightBlockNotFound
  48. }
  49. return lb, nil
  50. }
  51. func (impl *providerBenchmarkImpl) ReportEvidence(_ context.Context, _ types.Evidence) error {
  52. panic("not implemented")
  53. }
  54. func BenchmarkSequence(b *testing.B) {
  55. headers, vals, _ := genLightBlocksWithKeys(chainID, 1000, 100, 1, bTime)
  56. benchmarkFullNode := newProviderBenchmarkImpl(headers, vals)
  57. genesisBlock, _ := benchmarkFullNode.LightBlock(context.Background(), 1)
  58. c, err := light.NewClient(
  59. context.Background(),
  60. chainID,
  61. light.TrustOptions{
  62. Period: 24 * time.Hour,
  63. Height: 1,
  64. Hash: genesisBlock.Hash(),
  65. },
  66. benchmarkFullNode,
  67. []provider.Provider{benchmarkFullNode},
  68. dbs.New(dbm.NewMemDB()),
  69. light.Logger(log.TestingLogger()),
  70. light.SequentialVerification(),
  71. )
  72. if err != nil {
  73. b.Fatal(err)
  74. }
  75. b.ResetTimer()
  76. for n := 0; n < b.N; n++ {
  77. _, err = c.VerifyLightBlockAtHeight(context.Background(), 1000, bTime.Add(1000*time.Minute))
  78. if err != nil {
  79. b.Fatal(err)
  80. }
  81. }
  82. }
  83. func BenchmarkBisection(b *testing.B) {
  84. headers, vals, _ := genLightBlocksWithKeys(chainID, 1000, 100, 1, bTime)
  85. benchmarkFullNode := newProviderBenchmarkImpl(headers, vals)
  86. genesisBlock, _ := benchmarkFullNode.LightBlock(context.Background(), 1)
  87. c, err := light.NewClient(
  88. context.Background(),
  89. chainID,
  90. light.TrustOptions{
  91. Period: 24 * time.Hour,
  92. Height: 1,
  93. Hash: genesisBlock.Hash(),
  94. },
  95. benchmarkFullNode,
  96. []provider.Provider{benchmarkFullNode},
  97. dbs.New(dbm.NewMemDB()),
  98. light.Logger(log.TestingLogger()),
  99. )
  100. if err != nil {
  101. b.Fatal(err)
  102. }
  103. b.ResetTimer()
  104. for n := 0; n < b.N; n++ {
  105. _, err = c.VerifyLightBlockAtHeight(context.Background(), 1000, bTime.Add(1000*time.Minute))
  106. if err != nil {
  107. b.Fatal(err)
  108. }
  109. }
  110. }
  111. func BenchmarkBackwards(b *testing.B) {
  112. headers, vals, _ := genLightBlocksWithKeys(chainID, 1000, 100, 1, bTime)
  113. benchmarkFullNode := newProviderBenchmarkImpl(headers, vals)
  114. trustedBlock, _ := benchmarkFullNode.LightBlock(context.Background(), 0)
  115. c, err := light.NewClient(
  116. context.Background(),
  117. chainID,
  118. light.TrustOptions{
  119. Period: 24 * time.Hour,
  120. Height: trustedBlock.Height,
  121. Hash: trustedBlock.Hash(),
  122. },
  123. benchmarkFullNode,
  124. []provider.Provider{benchmarkFullNode},
  125. dbs.New(dbm.NewMemDB()),
  126. light.Logger(log.TestingLogger()),
  127. )
  128. if err != nil {
  129. b.Fatal(err)
  130. }
  131. b.ResetTimer()
  132. for n := 0; n < b.N; n++ {
  133. _, err = c.VerifyLightBlockAtHeight(context.Background(), 1, bTime)
  134. if err != nil {
  135. b.Fatal(err)
  136. }
  137. }
  138. }