public class LoadTest
extends java.lang.Object
implements junit.framework.Test
LoadTest
is a test decorator that runs
a test with a simulated number of concurrent users and
iterations.
In its simplest form, a LoadTest
is 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 ExampleTest
once and
all users started simultaneously, use:
or, to load test a single test method, use:Test loadTest = new LoadTest(new TestSuite(ExampleTest.class), 10);
Test loadTest = new LoadTest(new ExampleTest("testSomething"), 10);
The load can be ramped by specifying a pluggable
Timer
instance which prescribes the delay
between the addition of each concurrent user. A
ConstantTimer
has a constant delay, with
a zero value indicating that all users will be started
simultaneously. A RandomTimer
has 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 LoadTest
can be
constructed to decorate a RepeatedTest
.
Alternatively, a LoadTest
convenience constructor
specifying the number of iterations is provided which creates a
RepeatedTest
.
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:
or, alternatively, use:Timer timer = new ConstantTimer(1000); Test repeatedTest = new RepeatedTest(new ExampleTest("testSomething"), 20); Test loadTest = new LoadTest(repeatedTest, 10, timer);
ATimer timer = new ConstantTimer(1000); Test loadTest = new LoadTest(new ExampleTest("testSomething"), 10, 20, timer);
LoadTest
can be decorated as a TimedTest
to 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 LoadTest
does 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 same
ThreadGroup
as the thread running the decorated test.
Constructor and Description |
---|
LoadTest(junit.framework.Test test,
int users)
Constructs a
LoadTest to decorate
the specified test using the specified number
of concurrent users starting simultaneously. |
LoadTest(junit.framework.Test test,
int users,
int iterations)
Constructs a
LoadTest to 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 a
LoadTest to 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 a
LoadTest to decorate
the specified test using the specified number
of concurrent users and delay timer. |
Modifier and Type | Method and Description |
---|---|
protected void |
cleanup() |
int |
countTestCases()
Returns the number of tests in this load test.
|
protected long |
getDelay() |
void |
run(junit.framework.TestResult result)
Runs the test.
|
void |
setEnforceTestAtomicity(boolean isAtomic)
Indicates whether test atomicity should be enforced.
|
protected void |
sleep(long time) |
java.lang.String |
toString() |
protected void |
waitForAllThreadsToComplete() |
protected void |
waitForTestCompletion() |
protected void |
waitForThreadedTestThreadsToComplete() |
public LoadTest(junit.framework.Test test, int users)
LoadTest
to decorate
the specified test using the specified number
of concurrent users starting simultaneously.test
- Test to decorate.users
- Number of concurrent users.public LoadTest(junit.framework.Test test, int users, int iterations)
LoadTest
to decorate
the specified test using the specified number
of concurrent users starting simultaneously and
the number of iterations per user.test
- Test to decorate.users
- Number of concurrent users.iterations
- Number of iterations per user.public LoadTest(junit.framework.Test test, int users, int iterations, Timer timer)
LoadTest
to decorate
the specified test using the specified number
of concurrent users, number of iterations per
user, and delay timer.test
- Test to decorate.users
- Number of concurrent users.iterations
- Number of iterations per user.timer
- Delay timer.public LoadTest(junit.framework.Test test, int users, Timer timer)
LoadTest
to decorate
the specified test using the specified number
of concurrent users and delay timer.test
- Test to decorate.users
- Number of concurrent users.timer
- Delay timer.public void setEnforceTestAtomicity(boolean isAtomic)
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 same
ThreadGroup
as the thread running the decorated test.
isAtomic
- true
to enforce test atomicity;
false
otherwise.public int countTestCases()
countTestCases
in interface junit.framework.Test
public void run(junit.framework.TestResult result)
run
in interface junit.framework.Test
result
- Test result.protected void waitForTestCompletion()
protected void waitForThreadedTestThreadsToComplete()
protected void waitForAllThreadsToComplete()
protected void sleep(long time)
protected void cleanup()
public java.lang.String toString()
toString
in class java.lang.Object
protected long getDelay()
Copyright ? 1999-2005 Clarkware Consulting, Inc.