func GenerateMetricValues(agentNum int) []*model.MetricValue {
	testCount := g.Config().TestCount

	sec := g.Config().Transfer.Interval
	hostname := g.Config().Hostname + strconv.Itoa(agentNum)

	mvs := []*model.MetricValue{}
	for i := 0; i < testCount; i++ {
		mv := generateMetricValue(i)
		mvs = append(mvs, mv)
	}

	now := time.Now().Unix()
	for j := 0; j < len(mvs); j++ {
		mvs[j].Step = int64(sec)
		mvs[j].Endpoint = hostname
		mvs[j].Timestamp = now

		log.Println(mvs[j].String())
	}
	log.Println("generate metricValues success.")
	return mvs
}
Exemplo n.º 2
0
func main() {
	cfg := flag.String("c", "cfg.json", "configuration file")
	version := flag.Bool("v", false, "show version")

	flag.Parse()

	if *version {
		fmt.Println(g.VERSION)
		os.Exit(0)
	}

	g.ParseConfig(*cfg)

	//	g.InitLocalIps()
	//	g.InitRpcClients()

	threadCount := g.Config().ThreadCount
	log.Println("threadcout:", threadCount)
	var mvs []*model.MetricValue
	//	var seed *rand.Rand
	//	var sec int
	//	for i := 0; i < threadCount; i++ {
	//		seed = rand.New(rand.NewSource(time.Now().UnixNano()))
	//		//此处的60是使各个agent client在不同的时间内启动,模拟真实场景
	//		sec = seed.Intn(60)
	//		log.Println("sec:", sec)
	//		time.Sleep(time.Duration(sec) * time.Second)
	//		go func() {
	//			j := i
	//			for {
	//				log.Println("generator is running")
	//				mvs = funcs.GenerateMetricValues(j)
	//				//g.SendToTransfer(mvs)
	//				//此处的60是每个agent client的step
	//				time.Sleep(time.Duration(60) * time.Second)
	//			}
	//		}()
	//	}

	startupTime := generateRandomStartupTime(100)
	count := 0
	for i := 0; i < len(startupTime); i++ {
		fmt.Println(i, ": ", startupTime[i])
		num := startupTime[i]
		for j := 0; j < num; j++ {
			count++
			fmt.Println("count:", count)
			k := count
			go func(agentNum int) {
				client := &g.Client{}
				client.InitRpcClients()
				for {
					log.Println("generator is running")
					mvs = funcs.GenerateMetricValues(agentNum)
					client.SendToTransfer(mvs)
					//此处的60是每个agent client的step
					time.Sleep(time.Duration(60) * time.Second)
				}
			}(k)
		}
		time.Sleep(time.Duration(1) * time.Second)
	}

	for {
		select {
		case <-time.After(time.Duration(60 * time.Second)):
			errNum := atomic.LoadInt64(&g.ErrNum) * int64(g.Config().TestCount)
			log.Println("ErrNum: ", errNum)
			//每一分钟统计一次错误数,然后重置为0
			atomic.StoreInt64(&g.ErrNum, 0)
		}
	}
}