-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql_test.go
65 lines (56 loc) · 1.59 KB
/
mysql_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package mysql_cache_performance
import (
"testing"
"database/sql"
"math/rand"
_ "github.com/go-sql-driver/mysql"
)
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func randSeq(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
func BenchmarkMySqlGet(b *testing.B) {
var id int64
db, err := sql.Open("mysql", "user:pass@tcp(mysql-master:3306)/cached_db?autocommit=true")
if err != nil {
b.Fatalf("couldn't connect to mysql")
}
stmtInsert, err := db.Prepare("INSERT INTO cached (data) VALUES( ? )")
if err != nil {
panic(err.Error())
}
data, err := stmtInsert.Exec(randSeq(30))
if err != nil {
panic(err.Error())
}
id, err = data.LastInsertId()
if err != nil {
panic(err.Error())
}
dbr, err := sql.Open("mysql", "user:pass@tcp(mysql-slave:3306)/cached_db?autocommit=true")
if err != nil {
b.Fatalf("couldn't connect to mysql")
}
stmtGet, err := dbr.Prepare("SELECT data FROM cached WHERE id = ?")
if err != nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
//make sure it's on reader
var scanData string
for {
err = stmtGet.QueryRow(id).Scan(&scanData)
if err == nil {
break
}
}
for i := 0; i < b.N; i++ {
err = stmtGet.QueryRow(id).Scan(&scanData)
if err != nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
}
}