github.com/ethersphere/bee/v2@v2.2.0/pkg/feeds/epochs/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 epochs_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/epochs"
    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 _, i := range []int{0, 8, 30} {
    21  		for _, prefill := range []int64{1, 50} {
    22  			after := uint64(50)
    23  			storer := &feedstesting.Timeout{ChunkStore: inmemchunkstore.New()}
    24  			topicStr := "testtopic"
    25  			topic, err := crypto.LegacyKeccak256([]byte(topicStr))
    26  			if err != nil {
    27  				b.Fatal(err)
    28  			}
    29  
    30  			pk, _ := crypto.GenerateSecp256k1Key()
    31  			signer := crypto.NewDefaultSigner(pk)
    32  
    33  			updater, err := epochs.NewUpdater(storer, signer, topic)
    34  			if err != nil {
    35  				b.Fatal(err)
    36  			}
    37  			payload := []byte("payload")
    38  
    39  			ctx := context.Background()
    40  
    41  			for at := int64(0); at < prefill; at++ {
    42  				err = updater.Update(ctx, at, payload)
    43  				if err != nil {
    44  					b.Fatal(err)
    45  				}
    46  			}
    47  			latest := after + (1 << i)
    48  			err = updater.Update(ctx, int64(latest), payload)
    49  			if err != nil {
    50  				b.Fatal(err)
    51  			}
    52  
    53  			for _, j := range []int64{0, 8, 30} {
    54  				now := latest + 1<<j
    55  				for k, finder := range []feeds.Lookup{
    56  					epochs.NewFinder(storer, updater.Feed()),
    57  					epochs.NewAsyncFinder(storer, updater.Feed()),
    58  				} {
    59  					names := []string{"sync", "async"}
    60  					b.Run(fmt.Sprintf("%s:prefill=%d, latest=%d, now=%d", names[k], prefill, latest, now), func(b *testing.B) {
    61  						for n := 0; n < b.N; n++ {
    62  							_, _, _, err := finder.At(ctx, int64(now), after)
    63  							if err != nil {
    64  								b.Fatal(err)
    65  							}
    66  						}
    67  					})
    68  				}
    69  			}
    70  		}
    71  	}
    72  }