otter_update_test/insert_running.go

81 lines
1.5 KiB
Go
Raw Permalink Normal View History

2020-03-05 03:46:10 +00:00
package main
import (
"database/sql"
"log"
"strconv"
"sync"
"time"
"github.com/Pallinder/go-randomdata"
_ "github.com/go-sql-driver/mysql"
)
func dbInsert(port int) {
mysqluri := "root:yame123456@tcp(localhost:" + strconv.Itoa(port) + ")/yame"
db, err := sql.Open("mysql", mysqluri)
if err != nil {
panic(err)
}
err = db.Ping()
if err != nil {
panic(err)
}
N := 100
COUNT := 5000
now := time.Now()
for n := 0; n < N; n++ {
tx, err := db.Begin()
if err != nil {
panic(err)
}
for i := 0; i < COUNT; i++ {
name := strconv.Itoa(i) + "-" + randomdata.FirstName(-1) + "-" + randomdata.FullName(-1) + strconv.Itoa(randomdata.Number(0, 10000000))
age := strconv.Itoa(randomdata.Number(0, 105))
level := strconv.Itoa(randomdata.Number(0, 10))
task := randomdata.Email() + randomdata.Adjective() + randomdata.City()
attr := randomdata.Address()
taskid := i
tx.Exec("insert into user(name, age,level, task, attr, taskid) values(?, ?, ?, ?, ?, ?)", name, age, level, task, attr, taskid)
2020-03-05 06:38:21 +00:00
if i%10000 == 0 {
log.Println(name)
}
2020-03-05 03:46:10 +00:00
}
err = tx.Commit()
if err != nil {
panic(err)
}
}
log.Println(mysqluri,
int64(N*COUNT)/(time.Now().Unix()-now.Unix()), "qps")
}
func insertTask(port int, wg *sync.WaitGroup) {
defer wg.Done()
dbInsert(port)
}
func InsertData() {
gcount := 10
wg := &sync.WaitGroup{}
wg.Add(gcount)
for i := 0; i < gcount; i++ {
go insertTask(3306, wg)
} // DBInsert(23306)
wg.Wait()
log.Println("gthread: ", gcount)
}