Skip to content

Instantly share code, notes, and snippets.

@corsc
Last active April 1, 2016 05:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save corsc/c3577ac60aad63c125cd to your computer and use it in GitHub Desktop.
Save corsc/c3577ac60aad63c125cd to your computer and use it in GitHub Desktop.
benchmark iterations time/iteration delta delta%
--------- ---------- -------------- ----- ------
BenchmarkV1 50000 380653 ns/op
BenchmarkV2 100000 355788 ns/op -24865 -6.5%
BenchmarkV3 100000 357934 ns/op -22719 -5.9%
func v1() {
db := getDb()
tx, _ := db.Begin()
result, _ := tx.Exec("INSERT INTO orders (created) VALUES (?)", time.Now())
orderID, _ := result.LastInsertId()
_, _ = tx.Exec("INSERT INTO orderItems (order_id, added) VALUES (?, ?)", orderID, time.Now())
tx.Commit()
}
func v2() {
db := getDb()
result, _ := db.Exec("INSERT INTO orders (created) VALUES (?)", time.Now())
orderID, _ := result.LastInsertId()
_, _ = db.Exec("INSERT INTO orderItems (order_id, added) VALUES (?, ?)", orderID, time.Now())
}
func v3() {
db := getDb()
result, _ := db.Exec("INSERT INTO orders (created) VALUES (?)", time.Now())
orderID, _ := result.LastInsertId()
// introduce "fake" errors from the first INSERT statement
if isError(1) {
_, _ = db.Exec("DELETE FROM orders WHERE id = ?", orderID)
return
}
_, _ = db.Exec("INSERT INTO orderItems (order_id, added) VALUES (?, ?)", orderID, time.Now())
}
func isError(percentage int) bool {
return rand.Intn(100) < percentage
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment