Skip to content

Commit c49f778

Browse files
committedOct 23, 2013
Add an Exec Benchmark
Very useful for profiling and comparing to Query
1 parent bb7642c commit c49f778

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed
 

‎benchmark_test.go

+33-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ func initDB(b *testing.B, queries ...string) *sql.DB {
5151
return db
5252
}
5353

54-
// by Brad Fitzpatrick
5554
const concurrencyLevel = 10
5655

5756
func BenchmarkQuery(b *testing.B) {
@@ -93,6 +92,39 @@ func BenchmarkQuery(b *testing.B) {
9392
}
9493
}
9594

95+
func BenchmarkExec(b *testing.B) {
96+
tb := (*TB)(b)
97+
b.StopTimer()
98+
b.ReportAllocs()
99+
db := tb.checkDB(sql.Open("mysql", dsn))
100+
db.SetMaxIdleConns(concurrencyLevel)
101+
defer db.Close()
102+
103+
stmt := tb.checkStmt(db.Prepare("DO 1"))
104+
defer stmt.Close()
105+
106+
remain := int64(b.N)
107+
var wg sync.WaitGroup
108+
wg.Add(concurrencyLevel)
109+
defer wg.Wait()
110+
b.StartTimer()
111+
112+
for i := 0; i < concurrencyLevel; i++ {
113+
go func() {
114+
for {
115+
if atomic.AddInt64(&remain, -1) < 0 {
116+
wg.Done()
117+
return
118+
}
119+
120+
if _, err := stmt.Exec(); err != nil {
121+
b.Fatal(err.Error())
122+
}
123+
}
124+
}()
125+
}
126+
}
127+
96128
// data, but no db writes
97129
var roundtripSample []byte
98130

0 commit comments

Comments
 (0)