def __init__(self, test, users, iterations=0, timer=None): """ * Constructs a <code>LoadTest</code> to decorate * the specified test using the specified number * of concurrent users starting simultaneously and * the number of iterations per user. If a Timer is * indicated, then a delay is introduced * * @param test Test to decorate. * @param users Number of concurrent users. * @param iterations Number of iterations per user. * @param timer Delay timer. """ if iterations: test = RepeatedTest(test, iterations) if timer is None: timer = ConstantTimer(0) if users < 1: raise IllegalArgumentException("Number of users must be > 0") if timer is None: raise IllegalArgumentException("Delay timer is null") if test is None: raise IllegalArgumentException("Decorated test is null") self.users = users self.timer = timer self.setEnforceTestAtomicity(False) self.barrier = ThreadBarrier(users) self.group = ThreadedTestGroup(self, "LoadTest:ThreadedTestGroup") self.test = ThreadedTest(test, self.group, self.barrier)
class LoadTest(Test): def __init__(self, test, users, iterations=0, timer=None): """ * Constructs a <code>LoadTest</code> to decorate * the specified test using the specified number * of concurrent users starting simultaneously and * the number of iterations per user. If a Timer is * indicated, then a delay is introduced * * @param test Test to decorate. * @param users Number of concurrent users. * @param iterations Number of iterations per user. * @param timer Delay timer. """ if iterations: test = RepeatedTest(test, iterations) if timer is None: timer = ConstantTimer(0) if users < 1: raise IllegalArgumentException("Number of users must be > 0") if timer is None: raise IllegalArgumentException("Delay timer is null") if test is None: raise IllegalArgumentException("Decorated test is null") self.users = users self.timer = timer self.setEnforceTestAtomicity(False) self.barrier = ThreadBarrier(users) self.group = ThreadedTestGroup(self, "LoadTest:ThreadedTestGroup") self.test = ThreadedTest(test, self.group, self.barrier) def setEnforceTestAtomicity(self, isAtomic): """ * Indicates whether test atomicity should be enforced. * <p> * If threads are integral to the successful completion of * a decorated test, meaning that the decorated test should not be * treated as complete until all of its threads complete, then * <code>setEnforceTestAtomicity(true)</code> should be invoked to * enforce test atomicity. This effectively causes the load test to * wait for the completion of all threads belonging to the same * <code>ThreadGroup</code> as the thread running the decorated test. * * @param isAtomic <code>true</code> to enforce test atomicity; * <code>false</code> otherwise. """ self.enforceTestAtomicity = isAtomic def countTestCases(self): """ * Returns the number of tests in this load test. * * @return Number of tests. """ return self.users * self.test.countTestCases() def run(self, result): """ * Runs the test. * * @param result Test result. """ self.group.setTestResult(result) for i in range(self.users): #if result.shouldStop(): # self.barrier.cancelThreads(self.users - i) # break self.test.run(result) self.sleep(self.getDelay()) self.waitForTestCompletion() self.cleanup() def __call__(self, result): self.run(result) def waitForTestCompletion(self): """ // TODO: May require a strategy pattern // if other algorithms emerge. """ if self.enforceTestAtomicity: self.waitForAllThreadsToComplete() else: self.waitForThreadedTestThreadsToComplete() def waitForThreadedTestThreadsToComplete(self): while not self.barrier.isReached(): self.sleep(50) def waitForAllThreadsToComplete(self): while self.group.activeCount() > 0: self.sleep(50) def sleep(self, ms): try: time.sleep(ms*0.001) except: pass def cleanup(self): try: self.group.destroy() except: pass def __str__(self): if self.enforceTestAtomicity: return "LoadTest (ATOMIC): " + str(self.test) else: return "LoadTest (NON-ATOMIC): " + str(self.test) def getDelay(self): return self.timer.getDelay()