diff --git a/modules/robot/statisticalcomp.go b/modules/robot/statisticalcomp.go index d15b7fbe3..3a37792a8 100644 --- a/modules/robot/statisticalcomp.go +++ b/modules/robot/statisticalcomp.go @@ -8,6 +8,7 @@ import ( "os" "sort" "sync" + "time" ) /* @@ -18,7 +19,6 @@ type statisticalComp struct { module *RobotModule succclientNum int32 //链接成功客户端数 failclientNum int32 //链接失败客户端数 - endClientnum int32 //结束 totalmessage int32 //总消息两 robotdata map[string][]*RobotStatistics //机器人统计数据 lock sync.RWMutex @@ -32,15 +32,20 @@ func (this *statisticalComp) Init(service core.IService, module core.IModule, co return } +func (this *statisticalComp) Start() (err error) { + err = this.ModuleCompBase.Start() + go this.run() + return +} + //添加成功客户端数 func (this *statisticalComp) AddSuccClient(robot *Robot) { - this.succclientNum++ + } //添加失败客户端数 func (this *statisticalComp) AddFailClient(robot *Robot, err error) { - this.succclientNum++ - this.endClientnum++ + this.failclientNum++ } //机器人测试结束 @@ -48,11 +53,7 @@ func (this *statisticalComp) RobotFinishedTest(robot *Robot) { this.lock.Lock() this.robotdata[robot.Account()] = robot.statistics this.lock.Unlock() - this.endClientnum++ - if this.endClientnum >= this.module.options.RobotTotalNum { //压测结束 - this.OutReport() - } - log.Debugf("完成测试:%s", robot.account) + this.succclientNum++ } //输出报表 @@ -61,7 +62,7 @@ func (this *statisticalComp) OutReport() { messages map[string][]int64 = make(map[string][]int64) ok bool ) - + this.lock.Lock() for _, datas := range this.robotdata { for _, v := range datas { this.totalmessage++ @@ -71,8 +72,8 @@ func (this *statisticalComp) OutReport() { messages[v.message] = append(messages[v.message], v.time) } } - - file, err := os.Create(this.module.options.OutFilePath) + this.lock.Unlock() + file, err := os.OpenFile(this.module.options.OutFilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) if err != nil { fmt.Println(err) return @@ -104,3 +105,14 @@ func (this *statisticalComp) OutReport() { } log.Debugf("机器人报表已输出!") } + +func (this *statisticalComp) run() { + timer := time.NewTicker(time.Second * 10) + for { + select { + case <-timer.C: + this.OutReport() + } + } + +}