Skip to content

Commit

Permalink
Update benchmark syntax (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg authored Mar 11, 2024
1 parent 30c86ae commit 86c3c3a
Showing 1 changed file with 41 additions and 23 deletions.
64 changes: 41 additions & 23 deletions builq_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (

func BenchmarkBuildSimple(b *testing.B) {
for i := 0; i < b.N; i++ {
var bb builq.Builder
bb.Addf("SELECT %s FROM %s", "foo,bar", "table")
bb.Addf("WHERE id = %$", 123)
query, args, err := bb.Build()
q := builq.New()
q("SELECT %s FROM %s", "foo,bar", "table")
q("WHERE id = %$", 123)
query, args, err := q.Build()

switch {
case err != nil:
Expand All @@ -26,16 +26,16 @@ func BenchmarkBuildSimple(b *testing.B) {

func BenchmarkBuildManyArgs(b *testing.B) {
for i := 0; i < b.N; i++ {
var bb builq.Builder
bb.Addf("SELECT %s FROM %s", "foo,bar", "table")
bb.Addf("WHERE id = %$", 123)
bb.Addf("AND user_id = %$ OR referral_id = %$", 123, 456)
bb.Addf("OR status = %$ OR name = %$", "admin", "nice")
bb.Addf("AND password LIKE %$ OR password = %$", "str0ng", "qwerty")
bb.Addf("GROUP BY %$", "foo")
bb.Addf("ORDER BY %$", "bar")
bb.Addf("LIMIT %$;", 123)
query, args, err := bb.Build()
q := builq.New()
q("SELECT %s FROM %s", "foo,bar", "table")
q("WHERE id = %$", 123)
q("AND user_id = %$ OR referral_id = %$", 123, 456)
q("OR status = %$ OR name = %$", "admin", "nice")
q("AND password LIKE %$ OR password = %$", "str0ng", "qwerty")
q("GROUP BY %$", "foo")
q("ORDER BY %$", "bar")
q("LIMIT %$;", 123)
query, args, err := q.Build()

switch {
case err != nil:
Expand All @@ -49,15 +49,15 @@ func BenchmarkBuildManyArgs(b *testing.B) {
}

func BenchmarkBuildCached(b *testing.B) {
var bb builq.Builder
bb.Addf("SELECT %s FROM %s", "foo,bar", "table")
bb.Addf("WHERE id = %$", 123)
bb.Addf("AND user_id = %$ OR referral_id = %$", 123, 456)
bb.Addf("OR status = %$ OR name = %$", "admin", "nice")
bb.Addf("GROUP BY %$", "foo")
bb.Addf("ORDER BY %$", "bar")
bb.Addf("LIMIT %$;", 123)
query, args, err := bb.Build()
q := builq.New()
q("SELECT %s FROM %s", "foo,bar", "table")
q("WHERE id = %$", 123)
q("AND user_id = %$ OR referral_id = %$", 123, 456)
q("OR status = %$ OR name = %$", "admin", "nice")
q("GROUP BY %$", "foo")
q("ORDER BY %$", "bar")
q("LIMIT %$;", 123)
query, args, err := q.Build()
b.ResetTimer()

for i := 0; i < b.N; i++ {
Expand All @@ -71,3 +71,21 @@ func BenchmarkBuildCached(b *testing.B) {
}
}
}

func BenchmarkBuilder(b *testing.B) {
for i := 0; i < b.N; i++ {
var bb builq.Builder
bb.Addf("SELECT %s FROM %s", "foo,bar", "table")
bb.Addf("WHERE id = %$", 123)
query, args, err := bb.Build()

switch {
case err != nil:
b.Fatal(err)
case query == "":
b.Fatal("empty query")
case len(args) == 0:
b.Fatal("empty args")
}
}
}

0 comments on commit 86c3c3a

Please sign in to comment.