Created
August 19, 2019 21:01
-
-
Save morgo/79e4a7b30536409061025bbd79716ca4 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
package main | |
import ( | |
"bytes" | |
"database/sql" | |
"fmt" | |
_ "github.com/go-sql-driver/mysql" | |
"sync" | |
"time" | |
) | |
/* test #1 */ | |
var ( | |
connection = "msandbox:msandbox@tcp(127.0.0.1:8017)/test" | |
threads = 32 | |
insertRow = "(NOW(), RANDOM_BYTES(512))" | |
createTable = "CREATE TABLE t1 (id INT NOT NULL PRIMARY KEY auto_increment, b DATETIME NOT NULL, c VARBINARY(512) NOT NULL, INDEX(b))" | |
) | |
/* test #2 */ | |
/* | |
var ( | |
connection = "msandbox:msandbox@tcp(127.0.0.1:8017)/test" | |
threads = 32 | |
insertRow = "(NOW(), RANDOM_BYTES(512))" | |
createTable = "CREATE TABLE t1 (id INT NOT NULL PRIMARY KEY auto_increment, b DATETIME NOT NULL, c VARBINARY(512) NOT NULL, UNIQUE (c))" | |
) | |
*/ | |
var ( | |
w = new(sync.WaitGroup) | |
) | |
func main() { | |
db, err := sql.Open("mysql", connection) | |
if err != nil { | |
fmt.Printf("Could not connect to MySQL: %s.", err) | |
} | |
buf := new(bytes.Buffer) | |
for i := 0; i < 100; i++ { | |
buf.WriteString(insertRow) | |
buf.WriteString(",") | |
} | |
batchedInsert := fmt.Sprintf("INSERT INTO t1 (b,c) VALUES %s %s;", buf.String(), insertRow) | |
db.Exec("DROP TABLE IF EXISTS t1") | |
db.Exec(createTable) | |
w.Add(1) | |
go monitor(db) | |
for i := 0; i < threads; i++ { | |
go insertOnLoop(db, batchedInsert) | |
} | |
w.Wait() | |
} | |
func insertOnLoop(db *sql.DB, sql string) { | |
for { | |
_, err := db.Exec(sql) | |
if err != nil { | |
fmt.Printf("error: %s\n", err) | |
} else { | |
// fmt.Printf(".") | |
} | |
} | |
} | |
func monitor(db *sql.DB) { | |
prevcount, newcount := 0, 0 | |
var fake string | |
fmt.Printf("Time,Rows\n") | |
for i := 0; i <= 3600; i++ { | |
rows, err := db.Query("SHOW GLOBAL STATUS LIKE 'Innodb_rows_inserted'") | |
if err != nil { | |
fmt.Printf("could not dump count: %s", err) | |
} | |
for rows.Next() { | |
err = rows.Scan(&fake, &newcount) | |
if err != nil { | |
fmt.Printf("could not dump count: %s", err) | |
} | |
} | |
if prevcount > 0 { | |
fmt.Printf("%d,%d\n", i, (newcount - prevcount)) | |
} | |
prevcount = newcount | |
rows.Close() | |
time.Sleep(time.Second) | |
} | |
w.Done() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment