From c1afe197289c4abb99f440af7ad003d6d6224f24 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Fri, 14 Feb 2020 00:09:44 +0800 Subject: [PATCH] Add benchmark tests for clause --- clause/benchmarks_test.go | 56 +++++++++++++++++++++++++++++++++++++++ clause/where.go | 12 ++++----- statement.go | 6 ++--- 3 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 clause/benchmarks_test.go diff --git a/clause/benchmarks_test.go b/clause/benchmarks_test.go new file mode 100644 index 00000000..33d3430a --- /dev/null +++ b/clause/benchmarks_test.go @@ -0,0 +1,56 @@ +package clause_test + +import ( + "sync" + "testing" + + "github.com/jinzhu/gorm" + "github.com/jinzhu/gorm/clause" + "github.com/jinzhu/gorm/schema" + "github.com/jinzhu/gorm/tests" +) + +func BenchmarkSelect(b *testing.B) { + user, _ := schema.Parse(&tests.User{}, &sync.Map{}, db.NamingStrategy) + + for i := 0; i < b.N; i++ { + stmt := gorm.Statement{DB: db, Table: user.Table, Schema: user, Clauses: map[string]clause.Clause{}} + clauses := []clause.Interface{clause.Select{}, clause.From{}, clause.Where{Exprs: []clause.Expression{clause.Eq{Column: clause.PrimaryColumn, Value: "1"}, clause.Gt{Column: "age", Value: 18}, clause.Or(clause.Neq{Column: "name", Value: "jinzhu"})}}} + + for _, clause := range clauses { + stmt.AddClause(clause) + } + + stmt.Build("SELECT", "FROM", "WHERE") + _ = stmt.SQL.String() + } +} + +func BenchmarkComplexSelect(b *testing.B) { + user, _ := schema.Parse(&tests.User{}, &sync.Map{}, db.NamingStrategy) + + for i := 0; i < b.N; i++ { + stmt := gorm.Statement{DB: db, Table: user.Table, Schema: user, Clauses: map[string]clause.Clause{}} + clauses := []clause.Interface{ + clause.Select{}, clause.From{}, + clause.Where{Exprs: []clause.Expression{ + clause.Eq{Column: clause.PrimaryColumn, Value: "1"}, + clause.Gt{Column: "age", Value: 18}, + clause.Or(clause.Neq{Column: "name", Value: "jinzhu"}), + }}, + clause.Where{Exprs: []clause.Expression{ + clause.Or(clause.Gt{Column: "score", Value: 100}, clause.Like{Column: "name", Value: "%linus%"}), + }}, + clause.GroupBy{Columns: []clause.Column{{Name: "role"}}, Having: clause.Where{[]clause.Expression{clause.Eq{"role", "admin"}}}}, + clause.Limit{Limit: 10, Offset: 20}, + clause.OrderBy{Columns: []clause.OrderByColumn{{Column: clause.PrimaryColumn, Desc: true}}}, + } + + for _, clause := range clauses { + stmt.AddClause(clause) + } + + stmt.Build("SELECT", "FROM", "WHERE", "GROUP BY", "LIMIT", "ORDER BY") + _ = stmt.SQL.String() + } +} diff --git a/clause/where.go b/clause/where.go index d0f57ed1..0ee1a141 100644 --- a/clause/where.go +++ b/clause/where.go @@ -61,7 +61,7 @@ type AndConditions struct { func (and AndConditions) Build(builder Builder) { if len(and.Exprs) > 1 { - builder.Write("(") + builder.WriteByte('(') } for idx, c := range and.Exprs { if idx > 0 { @@ -70,7 +70,7 @@ func (and AndConditions) Build(builder Builder) { c.Build(builder) } if len(and.Exprs) > 1 { - builder.Write(")") + builder.WriteByte(')') } } @@ -87,7 +87,7 @@ type OrConditions struct { func (or OrConditions) Build(builder Builder) { if len(or.Exprs) > 1 { - builder.Write("(") + builder.WriteByte('(') } for idx, c := range or.Exprs { if idx > 0 { @@ -96,7 +96,7 @@ func (or OrConditions) Build(builder Builder) { c.Build(builder) } if len(or.Exprs) > 1 { - builder.Write(")") + builder.WriteByte(')') } } @@ -113,7 +113,7 @@ type NotConditions struct { func (not NotConditions) Build(builder Builder) { if len(not.Exprs) > 1 { - builder.Write("(") + builder.WriteByte('(') } for idx, c := range not.Exprs { if idx > 0 { @@ -128,6 +128,6 @@ func (not NotConditions) Build(builder Builder) { } } if len(not.Exprs) > 1 { - builder.Write(")") + builder.WriteByte(')') } } diff --git a/statement.go b/statement.go index 5dd49623..1c3934c1 100644 --- a/statement.go +++ b/statement.go @@ -153,13 +153,13 @@ func (stmt *Statement) AddVar(vars ...interface{}) string { case clause.Column: placeholders.WriteString(stmt.Quote(v)) case []interface{}: - placeholders.WriteByte('(') if len(v) > 0 { + placeholders.WriteByte('(') placeholders.WriteString(stmt.AddVar(v...)) + placeholders.WriteByte(')') } else { - placeholders.WriteString("NULL") + placeholders.WriteString("(NULL)") } - placeholders.WriteByte(')') default: stmt.Vars = append(stmt.Vars, v) placeholders.WriteString(stmt.DB.Dialector.BindVar(stmt, v))