Last active
May 1, 2018 13:40
-
-
Save yinyin/e792f270bb6bc93a2a671bb7519d5417 to your computer and use it in GitHub Desktop.
Test mysql link speed over various DSN with SELECT NOW()
This file contains hidden or 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
// Test mysql speed over various DSN with SELECT NOW() | |
// | |
// Build with: | |
// // make an empty folder and cd into | |
// export GOPATH=`pwd` | |
// go get gist.github.com/e792f270bb6bc93a2a671bb7519d5417.git | |
// // or, for cross-build: | |
// env GOOS=linux GOARCH=amd64 go build -v gist.github.com/e792f270bb6bc93a2a671bb7519d5417.git | |
package main | |
import "os" | |
import "time" | |
import "fmt" | |
import "strconv" | |
import "database/sql" | |
import _ "github.com/go-sql-driver/mysql" | |
func runBench01Now(loopCount int, dsnText string) (queryLoopCost time.Duration, err error) { | |
dbconn, err := sql.Open("mysql", dsnText) | |
if nil != err { | |
return 0, err | |
} | |
defer dbconn.Close() | |
t0 := time.Now() | |
for ; loopCount >= 0; loopCount-- { | |
var result []byte | |
if err = dbconn.QueryRow("SELECT NOW()").Scan(&result); nil != err { | |
return 0, err | |
} | |
// fmt.Println("T:", result) | |
} | |
t1 := time.Now() | |
return t1.Sub(t0), nil | |
} | |
func getLoopCount() (loopCount int) { | |
loopCountText := os.Getenv("MNLOOPCNT") | |
if len(loopCountText) > 0 { | |
loopCount64, err := strconv.ParseInt(loopCountText, 0, 0) | |
if (nil == err) && (loopCount64 > 0) { | |
return int(loopCount64) | |
} | |
} | |
return 10000000 | |
} | |
func main() { | |
if len(os.Args) < 2 { | |
fmt.Println("export MNLOOPCNT=10000000") | |
fmt.Println("user:password@tcp(127.0.0.1:3306)/MyDatabase") | |
fmt.Println("user:password@unix(/tmp/mysql.sock)/MyDatabase") | |
return | |
} | |
loopCount := getLoopCount() | |
fmt.Println("loop:", loopCount) | |
for _, dsnText := range os.Args[1:] { | |
t0 := time.Now() | |
queryLoopCost, err := runBench01Now(loopCount, dsnText) | |
t1 := time.Now() | |
totalCost := t1.Sub(t0) | |
if nil != err { | |
fmt.Println("ERR:", err) | |
} else { | |
fmt.Println(">", dsnText) | |
fmt.Println("- query:", queryLoopCost, "total:", totalCost) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment