81 lines
1.5 KiB
Go
81 lines
1.5 KiB
Go
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)
|
|
if i%10000 == 0 {
|
|
log.Println(name)
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|