def next(self): ''' Get next sleep interval If we get -1, that means that we re-read the file for next interval to do If file reads something else than numbers, it done. ''' changed=False self._count = self._count - 1 if self._count<0: # it's NOT finished - reread file for next interval. myFile = open(self.filename, 'r') newTPS = float(myFile.readline(10)) myFile.close() if newTPS != self.tps : print('### Requested TPS changed from %s to %s' % (self.tps, newTPS)) self.tps = newTPS changed=True print ("################ Number of scenario per seconds : %s ", self.tps ) print ("################ Number of count for 1 second : %s ", self._count ) if self.tps==0 : print("############### Got request to stop injecting ") return -1 self._interval = 1000/self.tps # Number of interval ( minimum is 1 second for now ) +1 to the count to handle tps lower than 1 second. self._count=self.scanInterval*self.tps + 1 return ThroughputToken(self._interval),changed
def next(self): self.index+=self.batchSize # This is the last interval if self.index>self.nbIntervals: return None,-1 token = ThroughputToken(self.interval) if self.batchSize==1 else InternalBatchToken(self.batchSize,self.interval) return token , self.tps
def next(self): if logger.isInfoEnabled(): if self.breaking: logger.info('FastCadencer - next interval (%d) - %s' % (self.current, self.interval[self.current])) self.breaking=False tokens, tps = self.interval[self.current].next() # break - interval change if not tokens: self.breaking=True self.current+=1 if self.current>=self.max: logger.info('FastCadencer - Stopping - no more interval') return ThroughputToken( -1 ), -1 return self.interval[self.current].next() return tokens,tps
def next(self): ''' Get next sleep interval from RythmCadencer If we get -1, that means that we change interval, so we move to next RythmCadencer If we were at last RythmCadencer - OK - all is finished ''' if logger.isInfoEnabled(): if self.breaking: logger.info( 'RampingCadencer - Break - %s' % (self.rc[self.inx])) self.breaking=False token,tps = self.rc[self.inx].next() # We have finished one interval - switch to another ? if token.getInterval() <0: self.inx += 1 # last one if self.inx >= self.max: logger.info('RampingCadencer - Stopping - [inx=%d],[max=%d]' % (self.inx,self.max)) return ThroughputToken(-1), -1 self.breaking=True return self.rc[self.inx].next() return token,tps
def next(self): isChanged = self.has_changed self.has_changed=False return ThroughputToken(self._value),isChanged
def next(self): self._count -= 1 if self._count<0: return ThroughputToken(-1), -1 return ThroughputToken(self._interval), self.tps
class __Producer(Runnable): ''' A runnable inner class that just product tempo (Oject()) every Cadencer tempo ''' def __init__( self, func, nbConsumers, targetRuns, reporter=None, ): self._queue = GrinderQueue.getQueue() self.targetRuns = 999999999 if targetRuns == -1 else targetRuns self.foo = func self.nbConsumers = nbConsumers self.reporter = reporter self._inc = 0 logger.info('Producer - INIT - [class=%s][id=%s]' % (self.foo.__class__.__name__, id(self.foo))) def __stopAll(self): logger.info("Producer stop BEGIN") # TODO #------- # MANAGE A CLEVER GRACEFUL PERIOD BASED ON GrinderQueue() waiting content # # Graceful period for Async if GrinderQueue. async: logger.debug( '[%s.__stopAll] Async graceful period [%d millisec]' % (self.__class__.__name__, GrinderQueue.graceful_period)) print 'Async graceful period [%d millisec] started ...' % ( GrinderQueue.graceful_period) Thread.sleep(GrinderQueue.graceful_period) # Clean up any tokens still in the queue size = len(self._queue.queue) if size > 0: logger.error( '(stopAll) Producer - Potential issue in your test (Thread starvation) - removing approximatively "%d" tokens' % (size)) # Cleaning up anyway self._queue.queue.clear() # The trick is to poison all consumer to force them to stop for _ in range(self.nbConsumers): self._queue.put(Token(-1)) Thread.sleep(5) logger.info("Producer stop END") def __producer__(self): logger.info('Producer start - [target=%d]' % (self.targetRuns)) old_tps = 0 if self.reporter: self.reporter.setTPS(0) # To set first TPS try: (token, tps) = self.foo.next() except Exception, e: print 'self.foo: %s failed, reason: %s' % ( self.foo.__class__.__name__, e) logger.error('self.foo: %s failed, reason: %s' % (type(self.foo), e)) raise self._inc += token.getIncrement() logger.info('Producer first run - [TPS=%s][target=%d]' % (tps, self.targetRuns)) if self.reporter: self.reporter.setTPS(tps) while (self._inc <= self.targetRuns): (token, tps) = self.foo.next() interval = long(token.getInterval()) if logger.isTraceEnabled(): logger.trace('Producer - [token=%s]' % (str(token))) # Condition to stop : -1 if interval < 0: logger.info('Producer stop asked - TPS=%s' % (tps)) break # Changing rate if tps != old_tps: old_tps = tps logger.info('Producer Break - [TPS=%s]%s' % (tps, token)) # Reset queue self._queue.queue.clear() # Tell reporter we have a new TPS if self.reporter: self.reporter.setTPS(tps) # store the token to the grinder queue self._inc += token.getIncrement() # to be accurate we share sleep with all consumers thread if isinstance(token, InternalBatchToken): [ self._queue.put(ThroughputToken(k, True)) for k in token.data ] else: self._queue.put(token) # sleep between 2 tokens or batch of token Thread.sleep(interval)