Skip to content

Commit

Permalink
Benchmark many filters.
Browse files Browse the repository at this point in the history
  • Loading branch information
winder committed Aug 28, 2023
1 parent dbaba32 commit e65e4b0
Showing 1 changed file with 185 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"

sdk "github.com/algorand/go-algorand-sdk/v2/types"

Expand Down Expand Up @@ -191,3 +192,187 @@ filters:
})
}
}

type builder struct {
all map[string][]SubConfig
any map[string][]SubConfig
none map[string][]SubConfig
}

func (c *builder) add(t string, sc SubConfig) {
var m map[string][]SubConfig
switch t {
case "all":
if c.all == nil {
c.all = make(map[string][]SubConfig)
}
m = c.all
case "none":
if c.none == nil {
c.none = make(map[string][]SubConfig)
}
m = c.none
case "any":
if c.any == nil {
c.any = make(map[string][]SubConfig)
}
m = c.any
default:
panic(fmt.Sprintf("unknown type: %s", t))
}

m[t] = append(m[t], sc)
}

func (c *builder) build() *Config {
ret := &Config{
Filters: []map[string][]SubConfig{},
}

if c.all != nil {
ret.Filters = append(ret.Filters, c.all)
}
if c.any != nil {
ret.Filters = append(ret.Filters, c.any)
}
if c.none != nil {
ret.Filters = append(ret.Filters, c.none)
}
return ret
}

func BenchmarkProcess2(b *testing.B) {
// Block:
// * 25000 top level transactions
// * 5000x app call, payment, asset cfg, keyreg, asset xfer
// * 25000 inner payment transactions
bd := emptyBlockData()
// 5000 app calls
bd = appendTxns(bd, 5000, []byte("note data to match on"), sdk.ApplicationCallTx)
// Add an inner payment to each of these
bd = appendInnerTxns(bd, 5, sdk.PaymentTx)
// 20000 payments, asset, keyreg, and asset transfers
bd = appendTxns(bd, 20000, []byte("payment txn"), sdk.PaymentTx, sdk.AssetConfigTx, sdk.KeyRegistrationTx, sdk.AssetTransferTx)

cfgWithFilters := func(numEqual int, eqExp string, numRegex int, reExp string, numNum int, numExp string) *Config {
bldr := &builder{}
for i := 0; i < numEqual; i++ {
sc := SubConfig{
FilterTag: "sgnr",
ExpressionType: "equal",
Expression: eqExp,
}
bldr.add("all", sc)
}
for i := 0; i < numRegex; i++ {
sc := SubConfig{
FilterTag: "txn.note",
ExpressionType: "regex",
Expression: reExp,
}
bldr.add("all", sc)
}
for i := 0; i < numNum; i++ {
sc := SubConfig{
FilterTag: "txn.fee",
ExpressionType: "equal",
Expression: numExp,
}
bldr.add("all", sc)
}

return bldr.build()
}

var table = []struct {
name string
config string
numTxn int
numEq int
numRe int
numNum int
}{
// equal filter
{
name: "equal",
numTxn: 25000,
numEq: 1000,
},
{
name: "equal",
numTxn: 1,
numEq: 1000,
},
{
name: "equal",
numTxn: 25000,
numEq: 1,
},
{
name: "equal",
numTxn: 1,
numEq: 1,
},

// regex filter
{
name: "regex",
numTxn: 25000,
numRe: 1000,
},
{
name: "regex",
numTxn: 1,
numRe: 1000,
},
{
name: "regex",
numTxn: 25000,
numRe: 1,
},
{
name: "regex",
numTxn: 1,
numRe: 1,
},

// numeric filter
{
name: "equal",
numTxn: 25000,
numNum: 1000,
},
{
name: "equal",
numTxn: 25000,
numNum: 1,
},
{
name: "equal",
numTxn: 1,
numNum: 1000,
},
{
name: "equal",
numTxn: 1,
numNum: 1,
},
}
for _, v := range table {
b.Run(fmt.Sprintf("txn_%d_equal_%d_regex_%d_numeric_%d", v.numTxn, v.numEq, v.numRe, v.numNum), func(b *testing.B) {
cfg := cfgWithFilters(v.numEq, "addr", v.numRe, "note", v.numNum, "1000")
cfgStr, err := yaml.Marshal(cfg)
require.NoError(b, err)

fp := &FilterProcessor{}
err = fp.Init(context.Background(), &conduit.PipelineInitProvider{}, plugins.MakePluginConfig(string(cfgStr)), logrus.New())
assert.NoError(b, err)

// Ignore the setup cost above.
b.ResetTimer()
for i := 0; i < b.N; i++ {
fp.Process(bd)
}
})
}
}

0 comments on commit e65e4b0

Please sign in to comment.