-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.go
63 lines (51 loc) · 1.25 KB
/
generator.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
package main
import (
"bytes"
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"os"
"sync"
)
// env: CONNECTION
func main() {
connection := os.Getenv("CONNECTION")
threads := 8
db, err := sql.Open("mysql", connection)
if err != nil {
fmt.Printf("Could not connect to MySQL: %s.", err)
}
db.SetMaxOpenConns(threads)
w := new(sync.WaitGroup)
w.Add(1)
fmt.Printf("Running go rountines\n")
abuf := new(bytes.Buffer)
bbuf := new(bytes.Buffer)
for a := 0; a <= 1000; a++ {
// assume that b_id is mostly null
if a < 1 {
abuf.WriteString("(REPLACE(UUID(),'-',''), REPLACE(UUID(),'-','')),")
} else {
abuf.WriteString("(REPLACE(UUID(),'-',''), NULL),")
}
bbuf.WriteString("(REPLACE(UUID(),'-','')),")
}
tblA := fmt.Sprintf("INSERT INTO a (id, b_id) VALUES %s (REPLACE(UUID(),'-',''), REPLACE(UUID(),'-',''));", abuf.String())
tblB := fmt.Sprintf("INSERT INTO b (id) VALUES %s (REPLACE(UUID(),'-',''));", bbuf.String())
for i := 0; i < threads; i++ {
go insertOnLoop(db, tblA)
go insertOnLoop(db, tblB)
}
fmt.Printf("Waiting for go routines\n")
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(".")
}
}
}