github.com/vipernet-xyz/tendermint-core@v0.32.0/lite2/client_benchmark_test.go (about)

     1  package lite_test
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	dbm "github.com/tendermint/tm-db"
     8  
     9  	"github.com/tendermint/tendermint/libs/log"
    10  	lite "github.com/tendermint/tendermint/lite2"
    11  	"github.com/tendermint/tendermint/lite2/provider"
    12  	mockp "github.com/tendermint/tendermint/lite2/provider/mock"
    13  	dbs "github.com/tendermint/tendermint/lite2/store/db"
    14  )
    15  
    16  // NOTE: block is produced every minute. Make sure the verification time
    17  // provided in the function call is correct for the size of the blockchain. The
    18  // benchmarking may take some time hence it can be more useful to set the time
    19  // or the amount of iterations use the flag -benchtime t -> i.e. -benchtime 5m
    20  // or -benchtime 100x.
    21  //
    22  // Remember that none of these benchmarks account for network latency.
    23  var (
    24  	benchmarkFullNode = mockp.New(GenMockNode(chainID, 1000, 100, 1, bTime))
    25  	genesisHeader, _  = benchmarkFullNode.SignedHeader(1)
    26  )
    27  
    28  func BenchmarkSequence(b *testing.B) {
    29  	c, err := lite.NewClient(
    30  		chainID,
    31  		lite.TrustOptions{
    32  			Period: 24 * time.Hour,
    33  			Height: 1,
    34  			Hash:   genesisHeader.Hash(),
    35  		},
    36  		benchmarkFullNode,
    37  		[]provider.Provider{benchmarkFullNode},
    38  		dbs.New(dbm.NewMemDB(), chainID),
    39  		lite.Logger(log.TestingLogger()),
    40  		lite.SequentialVerification(),
    41  	)
    42  	if err != nil {
    43  		b.Fatal(err)
    44  	}
    45  	b.ResetTimer()
    46  
    47  	for n := 0; n < b.N; n++ {
    48  		_, err = c.VerifyHeaderAtHeight(1000, bTime.Add(1000*time.Minute))
    49  		if err != nil {
    50  			b.Fatal(err)
    51  		}
    52  	}
    53  }
    54  
    55  func BenchmarkBisection(b *testing.B) {
    56  	c, err := lite.NewClient(
    57  		chainID,
    58  		lite.TrustOptions{
    59  			Period: 24 * time.Hour,
    60  			Height: 1,
    61  			Hash:   genesisHeader.Hash(),
    62  		},
    63  		benchmarkFullNode,
    64  		[]provider.Provider{benchmarkFullNode},
    65  		dbs.New(dbm.NewMemDB(), chainID),
    66  		lite.Logger(log.TestingLogger()),
    67  	)
    68  	if err != nil {
    69  		b.Fatal(err)
    70  	}
    71  	b.ResetTimer()
    72  
    73  	for n := 0; n < b.N; n++ {
    74  		_, err = c.VerifyHeaderAtHeight(1000, bTime.Add(1000*time.Minute))
    75  		if err != nil {
    76  			b.Fatal(err)
    77  		}
    78  	}
    79  }
    80  
    81  func BenchmarkBackwards(b *testing.B) {
    82  	trustedHeader, _ := benchmarkFullNode.SignedHeader(0)
    83  	c, err := lite.NewClient(
    84  		chainID,
    85  		lite.TrustOptions{
    86  			Period: 24 * time.Hour,
    87  			Height: trustedHeader.Height,
    88  			Hash:   trustedHeader.Hash(),
    89  		},
    90  		benchmarkFullNode,
    91  		[]provider.Provider{benchmarkFullNode},
    92  		dbs.New(dbm.NewMemDB(), chainID),
    93  		lite.Logger(log.TestingLogger()),
    94  	)
    95  	if err != nil {
    96  		b.Fatal(err)
    97  	}
    98  	b.ResetTimer()
    99  
   100  	for n := 0; n < b.N; n++ {
   101  		_, err = c.VerifyHeaderAtHeight(1, bTime)
   102  		if err != nil {
   103  			b.Fatal(err)
   104  		}
   105  	}
   106  }