func AppConcurrence() { jq := common.NewJsonQuery(path.CONFIG_PATH + "testing" + path.DS + "app_concurrence.json") db := common.NewMySqlInstance("testdata") name := jq.String("group", "name") parameter := jq.String("group", "parameter") data := map[string]interface{}{ "Name": name, "SettingParameters": parameter, "LogTime": common.Date("Y-m-d H:i:s"), } schemaName := jq.String("schema", "name") amount := jq.Int("schema", "amount") start := jq.Int("schema", "start") offset := jq.Int("schema", "offset") max := jq.Int("schema", "max") //处理掉重复的记录 sql := "select app.Id, app.GroupId from app inner join app_group on app.GroupId=app_group.Id where app.Name=? and app_group.Name=?" result, err := db.GetRow(sql, schemaName, name) if err != nil { log.Fatalln(err.Error()) } if !common.Empty(result) { db.Delete("app_group", map[string]interface{}{"Id": result["GroupId"]}) db.Delete("app", map[string]interface{}{"Id": result["Id"]}) } lastid, err := db.Insert("app_group", data) if err != nil { panic(err.Error()) } c := start fmt.Println("max concurrence is ", max) for c < max { fmt.Println("Now, concurrence = ", c) appconcurrence(lastid, schemaName, amount, c) c = c + offset - (c % offset) } }
func (this *Testing) Parser() { jq := common.NewJsonQuery(path.CONFIG_PATH + "testing" + path.DS + "db_concurrence.json") name := jq.String("group", "name") parameter := jq.String("group", "parameter") //名称格式 schemaName := jq.String("schema", "name") //测试执行次数 和并发数 amount := jq.Int("schema", "amount") concurrency := jq.String("schema", "concurrency") concurrencyList := common.StringToList(concurrency) is_query := jq.String("is_query") results, err := common.ReadLine("result.info") if err != nil { log.Fatalln(err.Error()) } size := len(results) if size > 0 && len(results) != len(concurrencyList) { fmt.Println("Error, the number if concurrency is not matched the number of results.") return } //写入数据 db := common.NewMySqlInstance("testdata") //处理掉重复的记录 sql := "select db.GroupId from db inner join db_group on db.GroupId=db_group.Id where db.Name=? and db_group.Name=?" result, err := db.GetRow(sql, schemaName, name) if err != nil { log.Fatalln(err.Error()) } if !common.Empty(result) { db.Delete("db_group", map[string]interface{}{"Id": result["GroupId"]}) db.Delete("db", map[string]interface{}{"GroupId": result["GroupId"]}) } //写入分组数据 data := map[string]interface{}{ "Name": name, "SettingParameters": parameter, "LogTime": common.Date("Y-m-d H:i:s"), } groupId, err := db.Insert("db_group", data) if err != nil { panic(err.Error()) } for i := 0; i < size; i++ { c, err := strconv.Atoi(concurrencyList[i]) if err != nil { panic(err.Error()) } elapse, err1 := strconv.ParseFloat(strings.TrimSpace(results[i]), 64) if err1 != nil { panic(err1.Error()) } n := amount //convert to ms elapse = elapse * 1000 if is_query == "1" { //查询的情况,小于50的并发,约定取并发数的50倍 if c < 50 { n = c * 50 } } result := map[string]interface{}{ "GroupId": groupId, "Name": schemaName, "Total": n, "Concurrence": c, "ElapseTime": elapse, "QPS": common.Round(float64(n)*1000/float64(elapse), 2), //每秒处理请求数 "TPQ": common.Round(float64(elapse)/float64(n), 2), //平均每个请求用时多少ms "LogTime": common.Date("Y-m-d H:i:s"), } db.Insert("db", result) } }
//数据库测试并发执行逻辑 func DatabaseConcurrence() { jq := common.NewJsonQuery(path.CONFIG_PATH + "testing" + path.DS + "db_concurrence.json") name := jq.String("group", "name") parameter := jq.String("group", "parameter") schemaNameFormat := jq.String("schema", "name") targetSchemaDb := jq.String("schema", "db") is_query := jq.Int("is_query") //测试重启执行次数 times := jq.Int("schema", "times") amount := jq.Int("schema", "amount") start := jq.Int("schema", "start") offset := jq.Int("schema", "offset") max := jq.Int("schema", "max") var schemaName string for seq := 1; seq <= times; seq++ { //给测试方案名称加上顺号,标识不同的测试次数 if times == 1 { schemaName = strings.Replace(schemaNameFormat, "(%d)", "", 1) } else { fmt.Println(">>>>>>database pressure test>>>>>>", seq) schemaName = fmt.Sprintf(schemaNameFormat, seq) } db := common.NewMySqlInstance("testdata") //处理掉重复的记录 sql := "select db.GroupId from db inner join db_group on db.GroupId=db_group.Id where db.Name=? and db_group.Name=?" result, err := db.GetRow(sql, schemaName, name) if err != nil { log.Fatalln(err.Error()) } if !common.Empty(result) { db.Delete("db_group", map[string]interface{}{"Id": result["GroupId"]}) db.Delete("db", map[string]interface{}{"GroupId": result["GroupId"]}) } //写入分组数据 data := map[string]interface{}{ "Name": name, "SettingParameters": parameter, "LogTime": common.Date("Y-m-d H:i:s"), } lastid, err := db.Insert("db_group", data) if err != nil { panic(err.Error()) } c := start fmt.Println("max concurrence is ", max) for c < max { fmt.Println("Now, concurrence = ", c) if is_query == 0 { dbconcurrence(lastid, schemaName, targetSchemaDb, amount, c) } else if is_query == 1 { queryconcurrence(lastid, schemaName, targetSchemaDb, amount, c) } else { //example is_query=2 //slow query adjust := c * 32 if adjust < amount { fmt.Println("Adjust amount to ", adjust) queryconcurrence(lastid, schemaName, targetSchemaDb, adjust, c) } else { queryconcurrence(lastid, schemaName, targetSchemaDb, amount, c) } } if c < 100 { c = c + 10 - (c % 10) } else { c = c + offset - (c % offset) } } } }