github.com/ethersphere/bee/v2@v2.2.0/pkg/feeds/sequence/lookup_benchmark_test.go (about)

     1  // Copyright 2021 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package sequence_test
     6  
     7  import (
     8  	"context"
     9  	"fmt"
    10  	"testing"
    11  
    12  	"github.com/ethersphere/bee/v2/pkg/crypto"
    13  	"github.com/ethersphere/bee/v2/pkg/feeds"
    14  	"github.com/ethersphere/bee/v2/pkg/feeds/sequence"
    15  	feedstesting "github.com/ethersphere/bee/v2/pkg/feeds/testing"
    16  	"github.com/ethersphere/bee/v2/pkg/storage/inmemchunkstore"
    17  )
    18  
    19  func BenchmarkFinder(b *testing.B) {
    20  	for _, prefill := range []int64{1, 100, 1000, 5000} {
    21  		storer := &feedstesting.Timeout{ChunkStore: inmemchunkstore.New()}
    22  		topicStr := "testtopic"
    23  		topic, err := crypto.LegacyKeccak256([]byte(topicStr))
    24  		if err != nil {
    25  			b.Fatal(err)
    26  		}
    27  
    28  		pk, _ := crypto.GenerateSecp256k1Key()
    29  		signer := crypto.NewDefaultSigner(pk)
    30  
    31  		updater, err := sequence.NewUpdater(storer, signer, topic)
    32  		if err != nil {
    33  			b.Fatal(err)
    34  		}
    35  		payload := []byte("payload")
    36  
    37  		ctx := context.Background()
    38  
    39  		for at := int64(0); at < prefill; at++ {
    40  			err = updater.Update(ctx, at, payload)
    41  			if err != nil {
    42  				b.Fatal(err)
    43  			}
    44  		}
    45  		latest := prefill
    46  		err = updater.Update(ctx, latest, payload)
    47  		if err != nil {
    48  			b.Fatal(err)
    49  		}
    50  		now := prefill
    51  		for k, finder := range []feeds.Lookup{
    52  			sequence.NewFinder(storer, updater.Feed()),
    53  			sequence.NewAsyncFinder(storer, updater.Feed()),
    54  		} {
    55  			names := []string{"sync", "async"}
    56  			b.Run(fmt.Sprintf("%s:prefill=%d, latest/now=%d", names[k], prefill, now), func(b *testing.B) {
    57  				for n := 0; n < b.N; n++ {
    58  					_, _, _, err := finder.At(ctx, now, 0)
    59  					if err != nil {
    60  						b.Fatal(err)
    61  					}
    62  				}
    63  			})
    64  		}
    65  	}
    66  }