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.

164 lines
4.0 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. logger := log.NewTestingLogger(b)
  59. c, err := light.NewClient(
  60. context.Background(),
  61. chainID,
  62. light.TrustOptions{
  63. Period: 24 * time.Hour,
  64. Height: 1,
  65. Hash: genesisBlock.Hash(),
  66. },
  67. benchmarkFullNode,
  68. []provider.Provider{benchmarkFullNode},
  69. dbs.New(dbm.NewMemDB()),
  70. light.Logger(logger),
  71. light.SequentialVerification(),
  72. )
  73. if err != nil {
  74. b.Fatal(err)
  75. }
  76. b.ResetTimer()
  77. for n := 0; n < b.N; n++ {
  78. _, err = c.VerifyLightBlockAtHeight(context.Background(), 1000, bTime.Add(1000*time.Minute))
  79. if err != nil {
  80. b.Fatal(err)
  81. }
  82. }
  83. }
  84. func BenchmarkBisection(b *testing.B) {
  85. headers, vals, _ := genLightBlocksWithKeys(chainID, 1000, 100, 1, bTime)
  86. benchmarkFullNode := newProviderBenchmarkImpl(headers, vals)
  87. genesisBlock, _ := benchmarkFullNode.LightBlock(context.Background(), 1)
  88. logger := log.NewTestingLogger(b)
  89. c, err := light.NewClient(
  90. context.Background(),
  91. chainID,
  92. light.TrustOptions{
  93. Period: 24 * time.Hour,
  94. Height: 1,
  95. Hash: genesisBlock.Hash(),
  96. },
  97. benchmarkFullNode,
  98. []provider.Provider{benchmarkFullNode},
  99. dbs.New(dbm.NewMemDB()),
  100. light.Logger(logger),
  101. )
  102. if err != nil {
  103. b.Fatal(err)
  104. }
  105. b.ResetTimer()
  106. for n := 0; n < b.N; n++ {
  107. _, err = c.VerifyLightBlockAtHeight(context.Background(), 1000, bTime.Add(1000*time.Minute))
  108. if err != nil {
  109. b.Fatal(err)
  110. }
  111. }
  112. }
  113. func BenchmarkBackwards(b *testing.B) {
  114. ctx, cancel := context.WithCancel(context.Background())
  115. defer cancel()
  116. headers, vals, _ := genLightBlocksWithKeys(chainID, 1000, 100, 1, bTime)
  117. benchmarkFullNode := newProviderBenchmarkImpl(headers, vals)
  118. trustedBlock, _ := benchmarkFullNode.LightBlock(ctx, 0)
  119. logger := log.NewTestingLogger(b)
  120. c, err := light.NewClient(
  121. ctx,
  122. chainID,
  123. light.TrustOptions{
  124. Period: 24 * time.Hour,
  125. Height: trustedBlock.Height,
  126. Hash: trustedBlock.Hash(),
  127. },
  128. benchmarkFullNode,
  129. []provider.Provider{benchmarkFullNode},
  130. dbs.New(dbm.NewMemDB()),
  131. light.Logger(logger),
  132. )
  133. if err != nil {
  134. b.Fatal(err)
  135. }
  136. b.ResetTimer()
  137. for n := 0; n < b.N; n++ {
  138. _, err = c.VerifyLightBlockAtHeight(ctx, 1, bTime)
  139. if err != nil {
  140. b.Fatal(err)
  141. }
  142. }
  143. }