commit d14eb5355c35dc2a3595d45e2c7aa9f037134727 Author: huangsimin Date: Thu Mar 5 10:14:50 2020 +0800 update diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..2a2daff --- /dev/null +++ b/go.mod @@ -0,0 +1,11 @@ +module test + +go 1.13 + +require ( + github.com/Pallinder/go-randomdata v1.2.0 + github.com/elazarl/goproxy v0.0.0-20191011121108-aa519ddbe484 + github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2 + github.com/go-sql-driver/mysql v1.5.0 + golang.org/x/text v0.3.2 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..c321115 --- /dev/null +++ b/go.sum @@ -0,0 +1,12 @@ +github.com/Pallinder/go-randomdata v1.2.0 h1:DZ41wBchNRb/0GfsePLiSwb0PHZmT67XY00lCDlaYPg= +github.com/Pallinder/go-randomdata v1.2.0/go.mod h1:yHmJgulpD2Nfrm0cR9tI/+oAgRqCQQixsA8HyRZfV9Y= +github.com/elazarl/goproxy v0.0.0-20191011121108-aa519ddbe484 h1:pEtiCjIXx3RvGjlUJuCNxNOw0MNblyR9Wi+vJGBFh+8= +github.com/elazarl/goproxy v0.0.0-20191011121108-aa519ddbe484/go.mod h1:Ro8st/ElPeALwNFlcTpWmkr6IoMFfkjXAvTHpevnDsM= +github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2 h1:dWB6v3RcOy03t/bUadywsbyrQwCqZeNIEX6M1OtSZOM= +github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2/go.mod h1:gNh8nYJoAm43RfaxurUnxr+N1PwuFV3ZMl/efxlIlY8= +github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= +github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= +github.com/rogpeppe/go-charset v0.0.0-20180617210344-2471d30d28b4/go.mod h1:qgYeAmZ5ZIpBWTGllZSQnw97Dj+woV0toclVaRGI8pc= +golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/main.go b/main.go new file mode 100644 index 0000000..9d75a7a --- /dev/null +++ b/main.go @@ -0,0 +1,17 @@ +package main + +import ( + "log" + "os" +) + +func main() { + + f, err := os.OpenFile("./log", os.O_TRUNC|os.O_CREATE|os.O_RDWR, 0660) + if err != nil { + panic(err) + } + log.SetOutput(f) + + Updating() +} diff --git a/update_running.go b/update_running.go new file mode 100644 index 0000000..6360b7f --- /dev/null +++ b/update_running.go @@ -0,0 +1,101 @@ +package main + +import ( + "database/sql" + "log" + "strconv" + "sync" + "time" + + "github.com/Pallinder/go-randomdata" + _ "github.com/go-sql-driver/mysql" +) + +func GetConnect(port int) *sql.DB { + + 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) + } + return db + +} + +func DBUpdate(db *sql.DB, names []string) { + + now := time.Now() + N := 1 + + for i := 0; i < N; i++ { + for _, name := range names { + tx, err := db.Begin() + if err != nil { + panic(err) + } + + age := strconv.Itoa(randomdata.Number(0, 105)) + level := strconv.Itoa(randomdata.Number(0, 10)) + attr := randomdata.Address() + tx.Exec("update user set age = ?, level = ?, attr = ? where name = ?", age, level, attr, name) + + err = tx.Commit() + if err != nil { + panic(err) + } + + } + } + + log.Println(int64(len(names)*N)/(time.Now().Unix()-now.Unix()), " update qps") +} + +func Task(port int, wg *sync.WaitGroup) { + defer wg.Done() + db := GetConnect(port) + DBUpdate(db, RandomSelectNames(db)) +} + +func RandomSelectNames(db *sql.DB) []string { + + var names []string + COUNT := 10 + + for i := 0; i < COUNT; i++ { + age := randomdata.Number(0, 105) + taskid := randomdata.Number(0, 1000) + offset := randomdata.Number(0, 100) + + rows, err := db.Query("select * from (select name from user where taskid = ? and age = ? limit 1000 OFFSET ?)t1 order by rand() ", taskid, age, offset) + if err != nil { + panic(err) + } + + for rows.Next() { + var name string + rows.Scan(&name) + names = append(names, name) + } + } + + log.Println(len(names)) + return names +} + +func Updating() { + gcount := 1 + wg := &sync.WaitGroup{} + wg.Add(gcount) + + for i := 0; i < gcount; i++ { + go Task(3306, wg) + } // DBInsert(23306) + + wg.Wait() + log.Println("gthread: ", gcount) +}