Files
tendermint/state/txindex/kv/kv_bench_test.go
mmsqe e80dd00894 backport: performance improvements for the event query API (#7319) (#9334)
* Performance improvements for the event query API (#7319)

Rework the implementation of event query parsing and execution to
improve performance and reduce memory usage.

Previous memory and CPU profiles of the pubsub service showed query
processing as a significant hotspot. While we don't have evidence that
this is visibly hurting users, fixing it is fairly easy and self-contained.

Updates #6439.

Typical benchmark results comparing the original implementation (PEG) with the reworked implementation (Custom):
```
TEST                        TIME/OP  BYTES/OP  ALLOCS/OP  SPEEDUP   MEM SAVING
BenchmarkParsePEG-12       51716 ns  526832    27
BenchmarkParseCustom-12     2167 ns    4616    17         23.8x     99.1%
BenchmarkMatchPEG-12        3086 ns    1097    22
BenchmarkMatchCustom-12    294.2 ns      64     3         10.5x     94.1%
```
2022-09-13 10:42:14 +02:00

75 lines
1.6 KiB
Go

package kv
import (
"context"
"crypto/rand"
"fmt"
"os"
"testing"
dbm "github.com/tendermint/tm-db"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/pubsub/query"
"github.com/tendermint/tendermint/types"
)
func BenchmarkTxSearch(b *testing.B) {
dbDir, err := os.MkdirTemp("", "benchmark_tx_search_test")
if err != nil {
b.Errorf("failed to create temporary directory: %s", err)
}
db, err := dbm.NewGoLevelDB("benchmark_tx_search_test", dbDir)
if err != nil {
b.Errorf("failed to create database: %s", err)
}
indexer := NewTxIndex(db)
for i := 0; i < 35000; i++ {
events := []abci.Event{
{
Type: "transfer",
Attributes: []abci.EventAttribute{
{Key: "address", Value: fmt.Sprintf("address_%d", i%100), Index: true},
{Key: "amount", Value: "50", Index: true},
},
},
}
txBz := make([]byte, 8)
if _, err := rand.Read(txBz); err != nil {
b.Errorf("failed produce random bytes: %s", err)
}
txResult := &abci.TxResult{
Height: int64(i),
Index: 0,
Tx: types.Tx(string(txBz)),
Result: abci.ResponseDeliverTx{
Data: []byte{0},
Code: abci.CodeTypeOK,
Log: "",
Events: events,
},
}
if err := indexer.Index(txResult); err != nil {
b.Errorf("failed to index tx: %s", err)
}
}
txQuery := query.MustCompile(`transfer.address = 'address_43' AND transfer.amount = 50`)
b.ResetTimer()
ctx := context.Background()
for i := 0; i < b.N; i++ {
if _, err := indexer.Search(ctx, txQuery); err != nil {
b.Errorf("failed to query for txs: %s", err)
}
}
}