Add benchmark tests for clause
This commit is contained in:
		
							parent
							
								
									1f38ec4410
								
							
						
					
					
						commit
						c1afe19728
					
				
							
								
								
									
										56
									
								
								clause/benchmarks_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								clause/benchmarks_test.go
									
									
									
									
									
										Normal file
									
								
							| @ -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() | ||||
| 	} | ||||
| } | ||||
| @ -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(')') | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @ -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)) | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Jinzhu
						Jinzhu