github.com/lazyledger/lazyledger-core@v0.35.0-dev.0.20210613111200-4c651f053571/light/client_benchmark_test.go (about)

     1  package light_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/lazyledger/lazyledger-core/libs/db/memdb"
     9  	"github.com/lazyledger/lazyledger-core/libs/log"
    10  	"github.com/lazyledger/lazyledger-core/light"
    11  	"github.com/lazyledger/lazyledger-core/light/provider"
    12  	mockp "github.com/lazyledger/lazyledger-core/light/provider/mock"
    13  	dbs "github.com/lazyledger/lazyledger-core/light/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  	genesisBlock, _   = benchmarkFullNode.LightBlock(context.Background(), 1)
    26  )
    27  
    28  func BenchmarkSequence(b *testing.B) {
    29  	c, err := light.NewClient(
    30  		context.Background(),
    31  		chainID,
    32  		light.TrustOptions{
    33  			Period: 24 * time.Hour,
    34  			Height: 1,
    35  			Hash:   genesisBlock.Hash(),
    36  		},
    37  		benchmarkFullNode,
    38  		[]provider.Provider{benchmarkFullNode},
    39  		dbs.New(memdb.NewDB(), chainID),
    40  		light.Logger(log.TestingLogger()),
    41  		light.SequentialVerification(),
    42  	)
    43  	if err != nil {
    44  		b.Fatal(err)
    45  	}
    46  	b.ResetTimer()
    47  
    48  	for n := 0; n < b.N; n++ {
    49  		_, err = c.VerifyLightBlockAtHeight(context.Background(), 1000, bTime.Add(1000*time.Minute))
    50  		if err != nil {
    51  			b.Fatal(err)
    52  		}
    53  	}
    54  }
    55  
    56  func BenchmarkBisection(b *testing.B) {
    57  	c, err := light.NewClient(
    58  		context.Background(),
    59  		chainID,
    60  		light.TrustOptions{
    61  			Period: 24 * time.Hour,
    62  			Height: 1,
    63  			Hash:   genesisBlock.Hash(),
    64  		},
    65  		benchmarkFullNode,
    66  		[]provider.Provider{benchmarkFullNode},
    67  		dbs.New(memdb.NewDB(), chainID),
    68  		light.Logger(log.TestingLogger()),
    69  	)
    70  	if err != nil {
    71  		b.Fatal(err)
    72  	}
    73  	b.ResetTimer()
    74  
    75  	for n := 0; n < b.N; n++ {
    76  		_, err = c.VerifyLightBlockAtHeight(context.Background(), 1000, bTime.Add(1000*time.Minute))
    77  		if err != nil {
    78  			b.Fatal(err)
    79  		}
    80  	}
    81  }
    82  
    83  func BenchmarkBackwards(b *testing.B) {
    84  	trustedBlock, _ := benchmarkFullNode.LightBlock(context.Background(), 0)
    85  	c, err := light.NewClient(
    86  		context.Background(),
    87  		chainID,
    88  		light.TrustOptions{
    89  			Period: 24 * time.Hour,
    90  			Height: trustedBlock.Height,
    91  			Hash:   trustedBlock.Hash(),
    92  		},
    93  		benchmarkFullNode,
    94  		[]provider.Provider{benchmarkFullNode},
    95  		dbs.New(memdb.NewDB(), chainID),
    96  		light.Logger(log.TestingLogger()),
    97  	)
    98  	if err != nil {
    99  		b.Fatal(err)
   100  	}
   101  	b.ResetTimer()
   102  
   103  	for n := 0; n < b.N; n++ {
   104  		_, err = c.VerifyLightBlockAtHeight(context.Background(), 1, bTime)
   105  		if err != nil {
   106  			b.Fatal(err)
   107  		}
   108  	}
   109  }