github.com/line/ostracon@v1.0.10-0.20230328032236-7f20145f065d/light/client_benchmark_test.go (about)

     1  package light_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/line/ostracon/libs/sync"
     9  
    10  	dbm "github.com/tendermint/tm-db"
    11  
    12  	"github.com/line/ostracon/libs/log"
    13  	"github.com/line/ostracon/light"
    14  	"github.com/line/ostracon/light/provider"
    15  	mockp "github.com/line/ostracon/light/provider/mock"
    16  	dbs "github.com/line/ostracon/light/store/db"
    17  	"github.com/line/ostracon/types"
    18  )
    19  
    20  // NOTE: block is produced every minute. Make sure the verification time
    21  // provided in the function call is correct for the size of the blockchain. The
    22  // benchmarking may take some time hence it can be more useful to set the time
    23  // or the amount of iterations use the flag -benchtime t -> i.e. -benchtime 5m
    24  // or -benchtime 100x.
    25  //
    26  // Remember that none of these benchmarks account for network latency.
    27  var (
    28  	mu sync.Mutex
    29  	// Shouldn't initialize variables here since affecting test (this is for benchmark test)
    30  	benchmarkFullNode *mockp.Mock
    31  	genesisBlock      *types.LightBlock
    32  )
    33  
    34  func setupData() {
    35  	mu.Lock()
    36  	defer mu.Unlock()
    37  	if benchmarkFullNode == nil || genesisBlock == nil {
    38  		benchmarkFullNode = mockp.New(genMockNode(chainID, 1000, 100, 1, bTime))
    39  		genesisBlock, _ = benchmarkFullNode.LightBlock(context.Background(), 1)
    40  	}
    41  }
    42  
    43  func BenchmarkSequence(b *testing.B) {
    44  	setupData()
    45  	c, err := light.NewClient(
    46  		context.Background(),
    47  		chainID,
    48  		light.TrustOptions{
    49  			Period: 24 * time.Hour,
    50  			Height: 1,
    51  			Hash:   genesisBlock.Hash(),
    52  		},
    53  		benchmarkFullNode,
    54  		[]provider.Provider{benchmarkFullNode},
    55  		dbs.New(dbm.NewMemDB(), chainID),
    56  		light.Logger(log.TestingLogger()),
    57  		light.SequentialVerification(),
    58  	)
    59  	if err != nil {
    60  		b.Fatal(err)
    61  	}
    62  	b.ResetTimer()
    63  
    64  	for n := 0; n < b.N; n++ {
    65  		_, err = c.VerifyLightBlockAtHeight(context.Background(), 1000, bTime.Add(1000*time.Minute))
    66  		if err != nil {
    67  			b.Fatal(err)
    68  		}
    69  	}
    70  }
    71  
    72  func BenchmarkBisection(b *testing.B) {
    73  	setupData()
    74  	c, err := light.NewClient(
    75  		context.Background(),
    76  		chainID,
    77  		light.TrustOptions{
    78  			Period: 24 * time.Hour,
    79  			Height: 1,
    80  			Hash:   genesisBlock.Hash(),
    81  		},
    82  		benchmarkFullNode,
    83  		[]provider.Provider{benchmarkFullNode},
    84  		dbs.New(dbm.NewMemDB(), chainID),
    85  		light.Logger(log.TestingLogger()),
    86  	)
    87  	if err != nil {
    88  		b.Fatal(err)
    89  	}
    90  	b.ResetTimer()
    91  
    92  	for n := 0; n < b.N; n++ {
    93  		_, err = c.VerifyLightBlockAtHeight(context.Background(), 1000, bTime.Add(1000*time.Minute))
    94  		if err != nil {
    95  			b.Fatal(err)
    96  		}
    97  	}
    98  }
    99  
   100  func BenchmarkBackwards(b *testing.B) {
   101  	setupData()
   102  	trustedBlock, _ := benchmarkFullNode.LightBlock(context.Background(), 0)
   103  	c, err := light.NewClient(
   104  		context.Background(),
   105  		chainID,
   106  		light.TrustOptions{
   107  			Period: 24 * time.Hour,
   108  			Height: trustedBlock.Height,
   109  			Hash:   trustedBlock.Hash(),
   110  		},
   111  		benchmarkFullNode,
   112  		[]provider.Provider{benchmarkFullNode},
   113  		dbs.New(dbm.NewMemDB(), chainID),
   114  		light.Logger(log.TestingLogger()),
   115  	)
   116  	if err != nil {
   117  		b.Fatal(err)
   118  	}
   119  	b.ResetTimer()
   120  
   121  	for n := 0; n < b.N; n++ {
   122  		_, err = c.VerifyLightBlockAtHeight(context.Background(), 1, bTime)
   123  		if err != nil {
   124  			b.Fatal(err)
   125  		}
   126  	}
   127  }