Example #1
0
//app并发测试
//n 测试总次数
//c 并发量
func appconcurrence(groupid int64, schemaname string, n int, c int) {
	if c > n {
		panic("error: c>n")
	}
	timer := common.NewTimer()
	timer.Start()

	//每个线程执行多少次
	segs := n / c

	var chs []chan int = make([]chan int, c)
	var cycleN int
	for i := 0; i < c; i++ {
		cycleN = segs
		if i == c-1 {
			cycleN += n % c
		}
		chs[i] = make(chan int)
		go func(cycleN int, ch chan int) {
			for cyc := 0; cyc < cycleN; cyc++ {
				resp, err := http.Get("http://localhost:8080")
				if err != nil {
					log.Fatalln(err.Error())
				}
				resp.Body.Close()
			}
			ch <- 1
		}(cycleN, chs[i])
	}

	fmt.Println("wait...")
	for _, ch := range chs {
		<-ch
	}

	elapse := timer.Elapse("ms")

	fmt.Println("finished once concurrence, elapse ", elapse, "ms")

	result := map[string]interface{}{
		"GroupId":     groupid,
		"Name":        schemaname,
		"Total":       n,
		"Concurrence": c,
		"ElapseTime":  elapse,
		"QPS":         common.Round(float64(n)/(float64(elapse)/1000), 2), //每秒处理请求数
		"TPQ":         common.Round(float64(elapse)/float64(n), 2),        //平均每个请求用时多少ms
		"LogTime":     common.Date("Y-m-d H:i:s"),
	}
	tdb := common.NewMySqlInstance("testdata")
	tdb.Insert("app", result)
}
Example #2
0
//DB并发测试
//n 测试写入总次数
//c 并发量
func dbconcurrence(groupid int64, schemaname string, targetdbschema string, n int, c int) {
	if c > n {
		panic("error: c>n")
	}

	//先清空写入的目标数据库
	db := common.NewMySqlInstance(targetdbschema)
	_, err := db.UDExec("truncate table target")
	if err != nil {
		log.Fatalln(err.Error())
	}

	timer := common.NewTimer()
	timer.Start()

	//每个线程执行多少次
	segs := n / c

	var chs chan int = make(chan int, c)
	var cycleN int
	for i := 0; i < c; i++ {
		cycleN = segs
		if i == c-1 {
			cycleN += n % c
		}
		go func(cycleN int, chs chan int) {
			defer func() {
				if r := recover(); r != nil {
					fmt.Printf("%v", r)
					chs <- 1
				}
			}()
			db := common.NewMySqlInstance(targetdbschema)
			date := common.Date("Y-m-d H:i:s")
			for cyc := 0; cyc < cycleN; cyc++ {
				_, err := db.InsertExec("insert into target(Num, String, LogTime) values(?,?,?)", 98, "helloworld", date)
				if err != nil {
					fmt.Println(err.Error())
				}
			}
			chs <- 1
		}(cycleN, chs)
	}

	fmt.Println("wait...")
	for i := 0; i < c; i++ {
		<-chs
	}

	elapse := timer.Elapse("ms")

	fmt.Println("finished once concurrence, elapse ", elapse, "ms")

	result := map[string]interface{}{
		"GroupId":     groupid,
		"Name":        schemaname,
		"Total":       n,
		"Concurrence": c,
		"ElapseTime":  elapse,
		"QPS":         common.Round(float64(n)/(float64(elapse)/1000), 2), //每秒处理请求数
		"TPQ":         common.Round(float64(elapse)/float64(n), 2),        //平均每个请求用时多少ms
		"LogTime":     common.Date("Y-m-d H:i:s"),
	}
	tdb := common.NewMySqlInstance("testdata")
	tdb.Insert("db", result)
}
Example #3
0
//DB并发测试
//n 测试查询总次数
//c 并发量
func queryconcurrence(groupid int64, schemaname string, targetdbschema string, n int, c int) {
	if c > n {
		panic("error: c>n")
	}

	//获取要查询的目标数据表和字段
	fmt.Println("Initialization data...")
	jq := common.NewJsonQuery(path.CONFIG_PATH + "testing" + path.DS + "db_concurrence.json")
	table := jq.String("query", "table")
	field := jq.String("query", "condition_field")

	db := common.NewMySqlInstance(targetdbschema)
	keylist, err := db.GetAll("select distinct "+field+" from "+table+" limit ?", n)
	if err != nil {
		log.Fatalln(err.Error())
	}
	if len(keylist) < n {
		log.Fatalln("Failed! Test data is less than ", n)
	}

	seeds := make([]string, n)
	for i, itm := range keylist {
		seeds[i] = itm[field]
	}

	timer := common.NewTimer()
	timer.Start()

	//每个线程执行多少次
	segs := n / c

	var chs chan int = make(chan int, c)
	var cycleN int
	for i := 0; i < c; i++ {
		cycleN = segs
		if i == c-1 {
			cycleN += n % c
		}
		go func(table string, field string, seeds []string, page int, segs int, cycleN int, chs chan int) {
			defer func() {
				if r := recover(); r != nil {
					fmt.Printf("%v", r)
					chs <- 1
				}
			}()
			db := common.NewMySqlInstance(targetdbschema)
			//开始查询
			for k := 0; k < cycleN; k++ {
				key := segs * page
				//res, err := db.GetAll("select * from "+table+" where "+field+"=?", seeds[key])
				sql := "select count(EventTypeId) as cnt, EventTypeId from server_alarm_notice_item where CreateTime between '2014-06-01' and '2014-08-21' group by EventTypeId"
				_, err := db.GetAll(sql)
				if err != nil {
					log.Fatalln(err.Error())
				}
				/*if common.Empty(res) {
					log.Fatalln("No data fetched. " + field + "=" + seeds[key])
				}*/
				key++
			}

			chs <- 1
		}(table, field, seeds, i, segs, cycleN, chs)
	}

	fmt.Println("wait...")
	for i := 0; i < c; i++ {
		<-chs
	}

	elapse := timer.Elapse("ms")

	fmt.Println("finished once query concurrence, elapse ", elapse, "ms")

	result := map[string]interface{}{
		"GroupId":     groupid,
		"Name":        schemaname,
		"Total":       n,
		"Concurrence": c,
		"ElapseTime":  elapse,
		"QPS":         common.Round(float64(n)/(float64(elapse)/1000), 2), //每秒处理请求数
		"TPQ":         common.Round(float64(elapse)/float64(n), 2),        //平均每个请求用时多少ms
		"LogTime":     common.Date("Y-m-d H:i:s"),
	}
	tdb := common.NewMySqlInstance("testdata")
	tdb.Insert("db", result)
}