Created
August 6, 2017 13:41
-
-
Save c16a/c46b6a936021ede79e9cdfeb583c7ce5 to your computer and use it in GitHub Desktop.
Concurrent MySQL with Golang
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
package main | |
import "database/sql" | |
import ( | |
"fmt" | |
_ "github.com/go-sql-driver/mysql" | |
"github.com/satori/go.uuid" | |
"math/rand" | |
"sync" | |
) | |
const ( | |
DB_HOST = "tcp(127.0.0.1:3307)" | |
DB_NAME = "one" | |
DB_USER = /*"root"*/ "root" | |
DB_PASS = /*""*/ "fofx" | |
MAX_ROWS_PER_THREAD = 100000 | |
NUMBER_OF_THREADS = 10000 | |
) | |
var wg sync.WaitGroup | |
func main() { | |
dsn := DB_USER + ":" + DB_PASS + "@" + DB_HOST + "/" + DB_NAME + "?charset=utf8" | |
db, err := sql.Open("mysql", dsn) | |
if err != nil { | |
panic(err) | |
} | |
tx, transactionError := db.Begin() | |
if transactionError != nil { | |
panic(transactionError) | |
} | |
stmt, stmtError := tx.Prepare(`INSERT INTO users VALUES (?, ?, ?)`) | |
if stmtError != nil { | |
panic(stmtError) | |
} | |
wg.Add(NUMBER_OF_THREADS) | |
for t := 0; t < NUMBER_OF_THREADS; t++ { | |
go stmtExec(t, stmt, tx) | |
} | |
wg.Wait() | |
} | |
func stmtExec(threadNo int, stmt *sql.Stmt, tx *sql.Tx) { | |
defer wg.Done() | |
for i := 0; i < MAX_ROWS_PER_THREAD; i++ { | |
stmt.Exec(uuid.NewV4().String(), randSeq(50), randSeq(255)) | |
if i%10000 == 0 && i != 0 { | |
fmt.Println("ThreadNumber:", threadNo, ", row:", i) | |
} | |
} | |
tx.Commit() | |
} | |
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
tx.Commit()
should move behind totx.Commit()
?