001: package com.clarkware.junitperf;
002:
003: import junit.framework.Test;
004: import junit.framework.TestSuite;
005: import junit.extensions.RepeatedTest;
006:
007: /**
008: * The <code>ExampleLoadTest</code> demonstrates how to
009: * decorate a <code>Test</code> as a <code>LoadTest</code>.
010: *
011: * @author <b>Mike Clark</b>
012: * @author Clarkware Consulting, Inc.
013: *
014: * @see com.clarkware.junitperf.LoadTest
015: * @see com.clarkware.junitperf.TimedTest
016: */
017:
018: public class ExampleLoadTest {
019:
020: public static final long toleranceInMillis = 100;
021:
022: public static Test suite() {
023: TestSuite suite = new TestSuite();
024:
025: //
026: // Pick any example tests below.
027: //
028: suite.addTest(makeStateful10UserLoadTest());
029: suite.addTest(makeStateful10UserLoadTestMethod());
030: suite
031: .addTest(make1SecondResponse10UserLoad1SecondDelayIterationTest());
032: //suite.addTest(make1SecondResponse1UserLoadTest());
033: //suite.addTest(make1SecondResponse2UserLoadTest());
034: //suite.addTest(make1SecondResponse1UserLoadIterationTest());
035: //suite.addTest(make1SecondResponse1UserLoadRepeatedTest());
036: //suite.addTest(make1SecondResponse2UserLoad2SecondDelayTest());
037:
038: return suite;
039: }
040:
041: /**
042: * Decorates a stateful test as a 10 user load test,
043: * providing each user with a different test instance
044: * to ensure thread safety.
045: *
046: * @return Test.
047: */
048: protected static Test makeStateful10UserLoadTest() {
049:
050: int users = 10;
051: int iterations = 1;
052:
053: Test factory = new TestFactory(ExampleStatefulTestCase.class);
054:
055: Test loadTest = new LoadTest(factory, users, iterations);
056:
057: return loadTest;
058: }
059:
060: /**
061: * Decorates a stateful test method as a 10 user load test,
062: * providing each user with a different test instance
063: * to ensure thread safety.
064: *
065: * @return Test.
066: */
067: protected static Test makeStateful10UserLoadTestMethod() {
068:
069: int users = 10;
070: int iterations = 1;
071:
072: Test factory = new TestMethodFactory(
073: ExampleStatefulTestCase.class, "testState");
074:
075: Test loadTest = new LoadTest(factory, users, iterations);
076:
077: return loadTest;
078: }
079:
080: /**
081: * Decorates a one second response time test as a one
082: * user load test with a maximum elapsed time of 1 second
083: * and a 0 second delay between users.
084: *
085: * @return Test.
086: */
087: protected static Test make1SecondResponse1UserLoadTest() {
088:
089: int users = 1;
090: long maxElapsedTimeInMillis = 1000 + toleranceInMillis;
091:
092: Test testCase = new ExampleTestCase("testOneSecondResponse");
093:
094: Test loadTest = new LoadTest(testCase, users);
095: Test timedTest = new TimedTest(loadTest, maxElapsedTimeInMillis);
096:
097: return timedTest;
098: }
099:
100: /**
101: * Decorates a one second response time test as a two
102: * user load test with a maximum elapsed time of 1.5
103: * seconds and a 0 second delay between users.
104: *
105: * @return Test.
106: */
107: protected static Test make1SecondResponse2UserLoadTest() {
108:
109: int users = 2;
110: long maxElapsedTimeInMillis = 1500 + toleranceInMillis;
111:
112: Test testCase = new ExampleTestCase("testOneSecondResponse");
113:
114: Test loadTest = new LoadTest(testCase, users);
115: Test timedTest = new TimedTest(loadTest, maxElapsedTimeInMillis);
116:
117: return timedTest;
118: }
119:
120: /**
121: * Decorates a one second response time test as a one
122: * user load test with 10 iterations per user, a maximum
123: * elapsed time of 12 seconds, and a 0 second delay
124: * between users.
125: *
126: * @see testOneSecondResponseOneUserLoadRepeatedTest
127: * @return Test.
128: */
129: protected static Test make1SecondResponse1UserLoadIterationTest() {
130:
131: int users = 1;
132: int iterations = 10;
133: long maxElapsedTimeInMillis = 10000 + toleranceInMillis;
134:
135: Test testCase = new ExampleTestCase("testOneSecondResponse");
136:
137: Test loadTest = new LoadTest(testCase, users, iterations);
138: Test timedTest = new TimedTest(loadTest, maxElapsedTimeInMillis);
139:
140: return timedTest;
141: }
142:
143: /**
144: * Decorates a one second response time test as a one
145: * user load test with 10 iterations per user, a maximum
146: * elapsed time of 12 seconds, and a 0 second delay
147: * between users.
148: *
149: * @see testOneSecondResponseOneUserLoadIterationTest
150: * @return Test.
151: */
152: protected static Test make1SecondResponse1UserLoadRepeatedTest() {
153:
154: int users = 1;
155: int iterations = 10;
156: long maxElapsedTimeInMillis = 10000 + toleranceInMillis;
157:
158: Test testCase = new ExampleTestCase("testOneSecondResponse");
159:
160: Test repeatedTest = new RepeatedTest(testCase, iterations);
161: Test loadTest = new LoadTest(repeatedTest, users);
162: Test timedTest = new TimedTest(loadTest, maxElapsedTimeInMillis);
163:
164: return timedTest;
165: }
166:
167: /**
168: * Decorates a one second response time test as a two
169: * user load test with a maximum elapsed time of 4 seconds
170: * and a 2 second delay between users.
171: *
172: * @return Test.
173: */
174: protected static Test make1SecondResponse2UserLoad2SecondDelayTest() {
175:
176: int users = 2;
177: Timer timer = new ConstantTimer(2000);
178: long maxElapsedTimeInMillis = 4000 + toleranceInMillis;
179:
180: Test testCase = new ExampleTestCase("testOneSecondResponse");
181:
182: Test loadTest = new LoadTest(testCase, users, timer);
183: Test timedTest = new TimedTest(loadTest, maxElapsedTimeInMillis);
184:
185: return timedTest;
186: }
187:
188: /**
189: * Decorates a one second response time test as a 10
190: * user load test with 10 iterations per user, a maximum
191: * elapsed time of 20 seconds, and a 1 second delay
192: * between users.
193: *
194: * @return Test.
195: */
196: protected static Test make1SecondResponse10UserLoad1SecondDelayIterationTest() {
197:
198: int users = 10;
199: int iterations = 10;
200: Timer timer = new ConstantTimer(1000);
201: long maxElapsedTimeInMillis = 20000 + toleranceInMillis;
202:
203: Test testCase = new ExampleTestCase("testOneSecondResponse");
204:
205: Test loadTest = new LoadTest(testCase, users, iterations, timer);
206: Test timedTest = new TimedTest(loadTest, maxElapsedTimeInMillis);
207:
208: return timedTest;
209: }
210:
211: public static void main(String args[]) {
212: junit.textui.TestRunner.run(suite());
213: }
214: }
|