Class LoadTest
- java.lang.Object
-
- com.clarkware.junitperf.LoadTest
-
- All Implemented Interfaces:
junit.framework.Test
public class LoadTest extends java.lang.Object implements junit.framework.TestTheLoadTestis a test decorator that runs a test with a simulated number of concurrent users and iterations.In its simplest form, a
LoadTestis constructed with a test to decorate and the number of concurrent users.For example, to create a load test of 10 concurrent users with each user running
ExampleTestonce and all users started simultaneously, use:Test loadTest = new LoadTest(new TestSuite(ExampleTest.class), 10);
or, to load test a single test method, use:
Test loadTest = new LoadTest(new ExampleTest("testSomething"), 10);The load can be ramped by specifying a pluggable
Timerinstance which prescribes the delay between the addition of each concurrent user. AConstantTimerhas a constant delay, with a zero value indicating that all users will be started simultaneously. ARandomTimerhas a random delay with a uniformly distributed variation.For example, to create a load test of 10 concurrent users with each user running
ExampleTest.testSomething()once and with a one second delay between the addition of users, use:Timer timer = new ConstantTimer(1000); Test loadTest = new LoadTest(new ExampleTest("testSomething"), 10, timer);In order to simulate each concurrent user running a test for a specified number of iterations, a
LoadTestcan be constructed to decorate aRepeatedTest. Alternatively, aLoadTestconvenience constructor specifying the number of iterations is provided which creates aRepeatedTest.For example, to create a load test of 10 concurrent users with each user running
ExampleTest.testSomething()for 20 iterations, and with a one second delay between the addition of users, use:Timer timer = new ConstantTimer(1000); Test repeatedTest = new RepeatedTest(new ExampleTest("testSomething"), 20); Test loadTest = new LoadTest(repeatedTest, 10, timer);or, alternatively, use:
Timer timer = new ConstantTimer(1000); Test loadTest = new LoadTest(new ExampleTest("testSomething"), 10, 20, timer);A
LoadTestcan be decorated as aTimedTestto test the elapsed time of the load test. For example, to decorate the load test constructed above as a timed test with a maximum elapsed time of 2 seconds, use:Test timedTest = new TimedTest(loadTest, 2000);
By default, a
LoadTestdoes not enforce test atomicity (as defined in transaction processing) if its decorated test spawns threads, either directly or indirectly. In other words, if a decorated test spawns a thread and then returns control without waiting for its spawned thread to complete, then the test is assumed to be transactionally complete.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
setEnforceTestAtomicity(true)should be invoked to enforce test atomicity. This effectively causes the load test to wait for the completion of all threads belonging to the sameThreadGroupas the thread running the decorated test.- Author:
- Mike Clark, Clarkware Consulting, Inc., Ervin Varga
-
-
Constructor Summary
Constructors Constructor Description LoadTest(junit.framework.Test test, int users)Constructs aLoadTestto decorate the specified test using the specified number of concurrent users starting simultaneously.LoadTest(junit.framework.Test test, int users, int iterations)Constructs aLoadTestto decorate the specified test using the specified number of concurrent users starting simultaneously and the number of iterations per user.LoadTest(junit.framework.Test test, int users, int iterations, Timer timer)Constructs aLoadTestto decorate the specified test using the specified number of concurrent users, number of iterations per user, and delay timer.LoadTest(junit.framework.Test test, int users, Timer timer)Constructs aLoadTestto decorate the specified test using the specified number of concurrent users and delay timer.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidcleanup()intcountTestCases()Returns the number of tests in this load test.protected longgetDelay()voidrun(junit.framework.TestResult result)Runs the test.voidsetEnforceTestAtomicity(boolean isAtomic)Indicates whether test atomicity should be enforced.protected voidsleep(long time)java.lang.StringtoString()protected voidwaitForAllThreadsToComplete()protected voidwaitForTestCompletion()protected voidwaitForThreadedTestThreadsToComplete()
-
-
-
Constructor Detail
-
LoadTest
public LoadTest(junit.framework.Test test, int users)Constructs aLoadTestto decorate the specified test using the specified number of concurrent users starting simultaneously.- Parameters:
test- Test to decorate.users- Number of concurrent users.
-
LoadTest
public LoadTest(junit.framework.Test test, int users, int iterations)Constructs aLoadTestto decorate the specified test using the specified number of concurrent users starting simultaneously and the number of iterations per user.- Parameters:
test- Test to decorate.users- Number of concurrent users.iterations- Number of iterations per user.
-
LoadTest
public LoadTest(junit.framework.Test test, int users, int iterations, Timer timer)Constructs aLoadTestto decorate the specified test using the specified number of concurrent users, number of iterations per user, and delay timer.- Parameters:
test- Test to decorate.users- Number of concurrent users.iterations- Number of iterations per user.timer- Delay timer.
-
LoadTest
public LoadTest(junit.framework.Test test, int users, Timer timer)Constructs aLoadTestto decorate the specified test using the specified number of concurrent users and delay timer.- Parameters:
test- Test to decorate.users- Number of concurrent users.timer- Delay timer.
-
-
Method Detail
-
setEnforceTestAtomicity
public void setEnforceTestAtomicity(boolean isAtomic)
Indicates whether test atomicity should be enforced.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
setEnforceTestAtomicity(true)should be invoked to enforce test atomicity. This effectively causes the load test to wait for the completion of all threads belonging to the sameThreadGroupas the thread running the decorated test.- Parameters:
isAtomic-trueto enforce test atomicity;falseotherwise.
-
countTestCases
public int countTestCases()
Returns the number of tests in this load test.- Specified by:
countTestCasesin interfacejunit.framework.Test- Returns:
- Number of tests.
-
run
public void run(junit.framework.TestResult result)
Runs the test.- Specified by:
runin interfacejunit.framework.Test- Parameters:
result- Test result.
-
waitForTestCompletion
protected void waitForTestCompletion()
-
waitForThreadedTestThreadsToComplete
protected void waitForThreadedTestThreadsToComplete()
-
waitForAllThreadsToComplete
protected void waitForAllThreadsToComplete()
-
sleep
protected void sleep(long time)
-
cleanup
protected void cleanup()
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
getDelay
protected long getDelay()
-
-