github.com/noirx94/tendermintmp@v0.0.1/state/txindex/kv/kv_bench_test.go (about)

     1  package kv
     2  
     3  import (
     4  	"context"
     5  	"crypto/rand"
     6  	"fmt"
     7  	"io/ioutil"
     8  	"testing"
     9  
    10  	dbm "github.com/tendermint/tm-db"
    11  
    12  	abci "github.com/tendermint/tendermint/abci/types"
    13  	"github.com/tendermint/tendermint/libs/pubsub/query"
    14  	"github.com/tendermint/tendermint/types"
    15  )
    16  
    17  func BenchmarkTxSearch(b *testing.B) {
    18  	dbDir, err := ioutil.TempDir("", "benchmark_tx_search_test")
    19  	if err != nil {
    20  		b.Errorf("failed to create temporary directory: %s", err)
    21  	}
    22  
    23  	db, err := dbm.NewGoLevelDB("benchmark_tx_search_test", dbDir)
    24  	if err != nil {
    25  		b.Errorf("failed to create database: %s", err)
    26  	}
    27  
    28  	indexer := NewTxIndex(db)
    29  
    30  	for i := 0; i < 35000; i++ {
    31  		events := []abci.Event{
    32  			{
    33  				Type: "transfer",
    34  				Attributes: []abci.EventAttribute{
    35  					{Key: []byte("address"), Value: []byte(fmt.Sprintf("address_%d", i%100)), Index: true},
    36  					{Key: []byte("amount"), Value: []byte("50"), Index: true},
    37  				},
    38  			},
    39  		}
    40  
    41  		txBz := make([]byte, 8)
    42  		if _, err := rand.Read(txBz); err != nil {
    43  			b.Errorf("failed produce random bytes: %s", err)
    44  		}
    45  
    46  		txResult := &abci.TxResult{
    47  			Height: int64(i),
    48  			Index:  0,
    49  			Tx:     types.Tx(string(txBz)),
    50  			Result: abci.ResponseDeliverTx{
    51  				Data:   []byte{0},
    52  				Code:   abci.CodeTypeOK,
    53  				Log:    "",
    54  				Events: events,
    55  			},
    56  		}
    57  
    58  		if err := indexer.Index(txResult); err != nil {
    59  			b.Errorf("failed to index tx: %s", err)
    60  		}
    61  	}
    62  
    63  	txQuery := query.MustParse("transfer.address = 'address_43' AND transfer.amount = 50")
    64  
    65  	b.ResetTimer()
    66  
    67  	ctx := context.Background()
    68  
    69  	for i := 0; i < b.N; i++ {
    70  		if _, err := indexer.Search(ctx, txQuery); err != nil {
    71  			b.Errorf("failed to query for txs: %s", err)
    72  		}
    73  	}
    74  }