From c49f77850fdbfe3cfb7edafb4e165a6397ea1291 Mon Sep 17 00:00:00 2001 From: Julien Schmidt Date: Wed, 23 Oct 2013 13:20:14 +0200 Subject: [PATCH] Add an Exec Benchmark Very useful for profiling and comparing to Query --- benchmark_test.go | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/benchmark_test.go b/benchmark_test.go index 2fb3f6b2b..144a8b678 100644 --- a/benchmark_test.go +++ b/benchmark_test.go @@ -51,7 +51,6 @@ func initDB(b *testing.B, queries ...string) *sql.DB { return db } -// by Brad Fitzpatrick const concurrencyLevel = 10 func BenchmarkQuery(b *testing.B) { @@ -93,6 +92,39 @@ func BenchmarkQuery(b *testing.B) { } } +func BenchmarkExec(b *testing.B) { + tb := (*TB)(b) + b.StopTimer() + b.ReportAllocs() + db := tb.checkDB(sql.Open("mysql", dsn)) + db.SetMaxIdleConns(concurrencyLevel) + defer db.Close() + + stmt := tb.checkStmt(db.Prepare("DO 1")) + defer stmt.Close() + + remain := int64(b.N) + var wg sync.WaitGroup + wg.Add(concurrencyLevel) + defer wg.Wait() + b.StartTimer() + + for i := 0; i < concurrencyLevel; i++ { + go func() { + for { + if atomic.AddInt64(&remain, -1) < 0 { + wg.Done() + return + } + + if _, err := stmt.Exec(); err != nil { + b.Fatal(err.Error()) + } + } + }() + } +} + // data, but no db writes var roundtripSample []byte