github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/topicsdb/search_test.go (about)

     1  package topicsdb
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  	"github.com/unicornultrafoundation/go-helios/native/idx"
     9  	"github.com/unicornultrafoundation/go-u2u/common"
    10  	"github.com/unicornultrafoundation/go-u2u/core/types"
    11  )
    12  
    13  func BenchmarkSearch(b *testing.B) {
    14  	topics, recs, topics4rec := genTestData(1000)
    15  
    16  	index := newTestIndex()
    17  
    18  	for _, rec := range recs {
    19  		err := index.Push(rec)
    20  		require.NoError(b, err)
    21  	}
    22  
    23  	var query [][][]common.Hash
    24  	for i := 0; i < len(topics); i++ {
    25  		from, to := topics4rec(i)
    26  		tt := topics[from : to-1]
    27  
    28  		qq := make([][]common.Hash, len(tt))
    29  		for pos, t := range tt {
    30  			qq[pos] = []common.Hash{t}
    31  		}
    32  
    33  		query = append(query, qq)
    34  	}
    35  
    36  	pooled := withThreadPool{index}
    37  
    38  	for dsc, method := range map[string]func(context.Context, idx.Block, idx.Block, [][]common.Hash) ([]*types.Log, error){
    39  		"index":  index.FindInBlocks,
    40  		"pooled": pooled.FindInBlocks,
    41  	} {
    42  		b.Run(dsc, func(b *testing.B) {
    43  			b.ResetTimer()
    44  
    45  			for i := 0; i < b.N; i++ {
    46  				qq := query[i%len(query)]
    47  				_, err := method(nil, 0, 0xffffffff, qq)
    48  				require.NoError(b, err)
    49  			}
    50  		})
    51  	}
    52  }