func (self *myGenerator) Start() { var throttle <-chan time.Time if gen.lps > 0 { interval := time.Duration(1e9 / gen.lps) log.info("Setting throttle (%v)", interval) throttle = time.Tick(interval) } // set stop time go func() { time.AfterFunc(gen.durationNs, func() { log.Info("Stopping load generator...") gen.stopSign <- 0 }) }() gen.status = lib.STATUS_STARTED // started call gen.genLoad(throttle) //call end if len(gen.endSign) == 1 { gen.callCnt = <-gen.endSign } gen.status = lib.STATUS_STOPPED log.Info("Stopped. (callCnt=%d)\n", gen.callCnt) }
func NewMyGenerator( caller lib.ICall, timeoutNs time.Duration, lps unit32, durationNs time.Duration, retCh chan *lib.CallRet) (lib.IGenerator, error) { var errMsg string if caller == nil { errMsg = fmt.sprintln("Invalid caller!") } if timeoutNs == 0 { errMsg = fmt.sprintln("Invalid timeoutNs!") } if lps == 0 { errMsg = fmt.sprintln("Invalid lps!") } if durationNs == 0 { errMsg = fmt.sprintln("Invalid durationNs!") } if retCh == nil { errMsg = fmt.sprintln("Invalid retCh!") } if errMsg != "" { return nil, errors.New(errMsg) } gen := &myGenerator{ caller: caller, timeoutNs: timeoutNs, lps: lps, durationNs: durationNs, stopSign: make(chan byte, 1), cancelSign: 0, endSign: make(chan uint64, 1), status: lib.STATUS_ORIGINAL, callCnt: 0, retCh: retCh, } log.Info("Loader info: timeoutNs=%v, lps=%d, durationNs=%v", timeoutNs, lps, durationNs) if err := gen.init(); err != nil { return nil, err } return gen, nil }
func (self *myGenerator) handleStopSign() { gen.cancelSign = 1 log.Info("Closing retChan ...") close(gen.retChan) }