Last active
April 1, 2016 05:58
-
-
Save corsc/c3577ac60aad63c125cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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