add: insert code
This commit is contained in:
parent
d14eb5355c
commit
08bc190512
77
insert_running.go
Normal file
77
insert_running.go
Normal file
|
@ -0,0 +1,77 @@
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
3
main.go
3
main.go
|
@ -13,5 +13,6 @@ func main() {
|
||||||
}
|
}
|
||||||
log.SetOutput(f)
|
log.SetOutput(f)
|
||||||
|
|
||||||
Updating()
|
// Updating()
|
||||||
|
InsertData()
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,7 @@ func GetConnect(port int) *sql.DB {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func DBUpdate(db *sql.DB, names []string) {
|
func dbUpdate(db *sql.DB, names []string) {
|
||||||
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
N := 1
|
N := 1
|
||||||
|
@ -55,16 +55,16 @@ func DBUpdate(db *sql.DB, names []string) {
|
||||||
log.Println(int64(len(names)*N)/(time.Now().Unix()-now.Unix()), " update qps")
|
log.Println(int64(len(names)*N)/(time.Now().Unix()-now.Unix()), " update qps")
|
||||||
}
|
}
|
||||||
|
|
||||||
func Task(port int, wg *sync.WaitGroup) {
|
func updateTask(port int, wg *sync.WaitGroup) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
db := GetConnect(port)
|
db := GetConnect(port)
|
||||||
DBUpdate(db, RandomSelectNames(db))
|
dbUpdate(db, RandomSelectNames(db))
|
||||||
}
|
}
|
||||||
|
|
||||||
func RandomSelectNames(db *sql.DB) []string {
|
func RandomSelectNames(db *sql.DB) []string {
|
||||||
|
|
||||||
var names []string
|
var names []string
|
||||||
COUNT := 10
|
COUNT := 1000
|
||||||
|
|
||||||
for i := 0; i < COUNT; i++ {
|
for i := 0; i < COUNT; i++ {
|
||||||
age := randomdata.Number(0, 105)
|
age := randomdata.Number(0, 105)
|
||||||
|
@ -88,12 +88,12 @@ func RandomSelectNames(db *sql.DB) []string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Updating() {
|
func Updating() {
|
||||||
gcount := 1
|
gcount := 50
|
||||||
wg := &sync.WaitGroup{}
|
wg := &sync.WaitGroup{}
|
||||||
wg.Add(gcount)
|
wg.Add(gcount)
|
||||||
|
|
||||||
for i := 0; i < gcount; i++ {
|
for i := 0; i < gcount; i++ {
|
||||||
go Task(3306, wg)
|
go updateTask(3306, wg)
|
||||||
} // DBInsert(23306)
|
} // DBInsert(23306)
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user