github.com/soomindae/tendermint@v0.0.5-0.20210528140126-84a0c70c8162/light/client_benchmark_test.go (about)

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