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.

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