Files
tendermint/libs/pubsub/query/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

73 lines
1.2 KiB
Go

package query_test
import (
"testing"
"github.com/tendermint/tendermint/libs/pubsub/query"
oldquery "github.com/tendermint/tendermint/libs/pubsub/query/oldquery"
)
const testQuery = `tm.events.type='NewBlock' AND abci.account.name='Igor'`
var testEvents = map[string][]string{
"tm.events.index": {
"25",
},
"tm.events.type": {
"NewBlock",
},
"abci.account.name": {
"Anya", "Igor",
},
}
func BenchmarkParsePEG(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := oldquery.New(testQuery)
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkParseCustom(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := query.New(testQuery)
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkMatchPEG(b *testing.B) {
q, err := oldquery.New(testQuery)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
ok, err := q.Matches(testEvents)
if err != nil {
b.Fatal(err)
} else if !ok {
b.Error("no match")
}
}
}
func BenchmarkMatchCustom(b *testing.B) {
q, err := query.New(testQuery)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
ok, err := q.Matches(testEvents)
if err != nil {
b.Fatal(err)
} else if !ok {
b.Error("no match")
}
}
}