From 86c3c3a29d9e38773e43e5d12bd392e214d8f50e Mon Sep 17 00:00:00 2001 From: Oleg Kovalov Date: Mon, 11 Mar 2024 21:17:16 +0100 Subject: [PATCH] Update benchmark syntax (#34) --- builq_bench_test.go | 64 +++++++++++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 23 deletions(-) diff --git a/builq_bench_test.go b/builq_bench_test.go index b86da32..39610f5 100644 --- a/builq_bench_test.go +++ b/builq_bench_test.go @@ -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: @@ -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: @@ -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++ { @@ -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") + } + } +}