From 08bc190512646fd9eb839ab86377f544d6c2c4c3 Mon Sep 17 00:00:00 2001 From: huangsimin Date: Thu, 5 Mar 2020 11:46:10 +0800 Subject: [PATCH] add: insert code --- insert_running.go | 77 +++++++++++++++++++++++++++++++++++++++++++++++ main.go | 3 +- update_running.go | 12 ++++---- 3 files changed, 85 insertions(+), 7 deletions(-) create mode 100644 insert_running.go diff --git a/insert_running.go b/insert_running.go new file mode 100644 index 0000000..1a6abeb --- /dev/null +++ b/insert_running.go @@ -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) +} diff --git a/main.go b/main.go index 9d75a7a..5db74ca 100644 --- a/main.go +++ b/main.go @@ -13,5 +13,6 @@ func main() { } log.SetOutput(f) - Updating() + // Updating() + InsertData() } diff --git a/update_running.go b/update_running.go index 6360b7f..c55b98f 100644 --- a/update_running.go +++ b/update_running.go @@ -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() 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") } -func Task(port int, wg *sync.WaitGroup) { +func updateTask(port int, wg *sync.WaitGroup) { defer wg.Done() db := GetConnect(port) - DBUpdate(db, RandomSelectNames(db)) + dbUpdate(db, RandomSelectNames(db)) } func RandomSelectNames(db *sql.DB) []string { var names []string - COUNT := 10 + COUNT := 1000 for i := 0; i < COUNT; i++ { age := randomdata.Number(0, 105) @@ -88,12 +88,12 @@ func RandomSelectNames(db *sql.DB) []string { } func Updating() { - gcount := 1 + gcount := 50 wg := &sync.WaitGroup{} wg.Add(gcount) for i := 0; i < gcount; i++ { - go Task(3306, wg) + go updateTask(3306, wg) } // DBInsert(23306) wg.Wait()