Skip to content

Commit

Permalink
tpl/collections: Add BenchmarkWhereOps
Browse files Browse the repository at this point in the history
```
BenchmarkWhereOps/eq-10 	    8702	    120410 ns/op	   52280 B/op	    2515 allocs/op
BenchmarkWhereOps/ne-10 	    9829	    120759 ns/op	   52280 B/op	    2515 allocs/op
BenchmarkWhereOps/like-10           6754	    176377 ns/op	   52917 B/op	    2515 allocs/op
```
  • Loading branch information
bep committed Jul 28, 2023
1 parent f4598a0 commit ef6e813
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions tpl/collections/where_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"context"
"fmt"
"html/template"
"math/rand"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -859,3 +860,46 @@ func TestEvaluateSubElem(t *testing.T) {
}
}
}

func BenchmarkWhereOps(b *testing.B) {
ns := newNs()
var seq []map[string]string
ctx := context.Background()
for i := 0; i < 500; i++ {
seq = append(seq, map[string]string{"foo": "bar"})
}
for i := 0; i < 500; i++ {
seq = append(seq, map[string]string{"foo": "baz"})
}
// Shuffle the sequence.
for i := range seq {
j := rand.Intn(i + 1)
seq[i], seq[j] = seq[j], seq[i]
}
//results, err = ns.Where(context.Background(), test.seq, test.key, test.op, test.match)
runOps := func(b *testing.B, op, match string) {
_, err := ns.Where(ctx, seq, "foo", op, match)
if err != nil {
b.Fatal(err)
}
}

b.Run("eq", func(b *testing.B) {
for i := 0; i < b.N; i++ {
runOps(b, "eq", "bar")
}
})

b.Run("ne", func(b *testing.B) {
for i := 0; i < b.N; i++ {
runOps(b, "ne", "baz")
}
})

b.Run("like", func(b *testing.B) {
for i := 0; i < b.N; i++ {
runOps(b, "like", "^bar")
}
})

}

0 comments on commit ef6e813

Please sign in to comment.