Skip to content

Add an Exec Benchmark #133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 23, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like you to drop the defer and move wg.Wait() below the loop - but that's not a blocker.

... Besides, it reads and feels very weird to use atomic.AddInt64 and sync.WaitGroup (which uses AddInt32 internally) here, but I can't think of a better way right now (and probably ever)...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll change this in a later commit maybe

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

Expand Down