001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.control;
020:
021: import org.apache.jmeter.junit.JMeterTestCase;
022: import org.apache.jmeter.junit.stubs.TestSampler;
023: import org.apache.jmeter.testelement.TestElement;
024:
025: /**
026: * This class represents a controller that can controll the number of times that
027: * it is executed, either by the total number of times the user wants the
028: * controller executed (BYNUMBER) or by the percentage of time it is called
029: * (BYPERCENT)
030: *
031: */
032: public class TestThroughputController extends JMeterTestCase {
033: public TestThroughputController(String name) {
034: super (name);
035: }
036:
037: public void testByNumber() throws Exception {
038: ThroughputController sub_1 = new ThroughputController();
039: sub_1.setStyle(ThroughputController.BYNUMBER);
040: sub_1.setMaxThroughput(2);
041: sub_1.addTestElement(new TestSampler("one"));
042: sub_1.addTestElement(new TestSampler("two"));
043:
044: LoopController loop = new LoopController();
045: loop.setLoops(5);
046: loop.addTestElement(new TestSampler("zero"));
047: loop.addTestElement(sub_1);
048: loop.addIterationListener(sub_1);
049: loop.addTestElement(new TestSampler("three"));
050:
051: LoopController test = new LoopController();
052: test.setLoops(2);
053: test.addTestElement(loop);
054:
055: String[] order = new String[] { "zero", "one", "two", "three",
056: "zero", "one", "two", "three", "zero", "three", "zero",
057: "three", "zero", "three", "zero", "three", "zero",
058: "three", "zero", "three", "zero", "three", "zero",
059: "three", };
060: sub_1.testStarted();
061: test.setRunningVersion(true);
062: sub_1.setRunningVersion(true);
063: loop.setRunningVersion(true);
064: test.initialize();
065: for (int counter = 0; counter < order.length; counter++) {
066: TestElement sampler = test.next();
067: assertNotNull(sampler);
068: assertEquals("Counter: " + counter, order[counter], sampler
069: .getPropertyAsString(TestElement.NAME));
070: }
071: assertNull(test.next());
072: sub_1.testEnded();
073: }
074:
075: public void testByNumberZero() throws Exception {
076: ThroughputController sub_1 = new ThroughputController();
077: sub_1.setStyle(ThroughputController.BYNUMBER);
078: sub_1.setMaxThroughput(0);
079: sub_1.addTestElement(new TestSampler("one"));
080: sub_1.addTestElement(new TestSampler("two"));
081:
082: LoopController controller = new LoopController();
083: controller.setLoops(5);
084: controller.addTestElement(new TestSampler("zero"));
085: controller.addTestElement(sub_1);
086: controller.addIterationListener(sub_1);
087: controller.addTestElement(new TestSampler("three"));
088:
089: String[] order = new String[] { "zero", "three", "zero",
090: "three", "zero", "three", "zero", "three", "zero",
091: "three", };
092: int counter = 0;
093: controller.setRunningVersion(true);
094: sub_1.setRunningVersion(true);
095: sub_1.testStarted();
096: controller.initialize();
097: for (int i = 0; i < 3; i++) {
098: TestElement sampler = null;
099: while ((sampler = controller.next()) != null) {
100: assertEquals("Counter: " + counter + ", i: " + i,
101: order[counter], sampler
102: .getPropertyAsString(TestElement.NAME));
103: counter++;
104: }
105: assertEquals(counter, order.length);
106: counter = 0;
107: }
108: sub_1.testEnded();
109: }
110:
111: public void testByPercent33() throws Exception {
112: ThroughputController sub_1 = new ThroughputController();
113: sub_1.setStyle(ThroughputController.BYPERCENT);
114: sub_1.setPercentThroughput(33.33f);
115: sub_1.addTestElement(new TestSampler("one"));
116: sub_1.addTestElement(new TestSampler("two"));
117:
118: LoopController controller = new LoopController();
119: controller.setLoops(6);
120: controller.addTestElement(new TestSampler("zero"));
121: controller.addTestElement(sub_1);
122: controller.addIterationListener(sub_1);
123: controller.addTestElement(new TestSampler("three"));
124: // Expected results established using the DDA
125: // algorithm (see
126: // http://www.siggraph.org/education/materials/HyperGraph/scanline/outprims/drawline.htm):
127: String[] order = new String[] { "zero", // 0/1 vs. 1/1 -> 0 is
128: // closer to 33.33
129: "three", "zero", // 0/2 vs. 1/2 -> 50.0 is closer to
130: // 33.33
131: "one", "two", "three", "zero", // 1/3 vs. 2/3 -> 33.33 is
132: // closer to 33.33
133: "three", "zero", // 1/4 vs. 2/4 -> 25.0 is closer to
134: // 33.33
135: "three", "zero", // 1/5 vs. 2/5 -> 40.0 is closer to
136: // 33.33
137: "one", "two", "three", "zero", // 2/6 vs. 3/6 -> 33.33 is
138: // closer to 33.33
139: "three",
140: // etc...
141: };
142: int counter = 0;
143: controller.setRunningVersion(true);
144: sub_1.setRunningVersion(true);
145: sub_1.testStarted();
146: controller.initialize();
147: for (int i = 0; i < 3; i++) {
148: TestElement sampler = null;
149: while ((sampler = controller.next()) != null) {
150: assertEquals("Counter: " + counter + ", i: " + i,
151: order[counter], sampler
152: .getPropertyAsString(TestElement.NAME));
153: counter++;
154: }
155: assertEquals(counter, order.length);
156: counter = 0;
157: }
158: sub_1.testEnded();
159: }
160:
161: public void testByPercentZero() throws Exception {
162: ThroughputController sub_1 = new ThroughputController();
163: sub_1.setStyle(ThroughputController.BYPERCENT);
164: sub_1.setPercentThroughput(0.0f);
165: sub_1.addTestElement(new TestSampler("one"));
166: sub_1.addTestElement(new TestSampler("two"));
167:
168: LoopController controller = new LoopController();
169: controller.setLoops(150);
170: controller.addTestElement(new TestSampler("zero"));
171: controller.addTestElement(sub_1);
172: controller.addIterationListener(sub_1);
173: controller.addTestElement(new TestSampler("three"));
174:
175: String[] order = new String[] { "zero", "three", };
176: int counter = 0;
177: controller.setRunningVersion(true);
178: sub_1.setRunningVersion(true);
179: sub_1.testStarted();
180: controller.initialize();
181: for (int i = 0; i < 3; i++) {
182: TestElement sampler = null;
183: while ((sampler = controller.next()) != null) {
184: assertEquals("Counter: " + counter + ", i: " + i,
185: order[counter % order.length], sampler
186: .getPropertyAsString(TestElement.NAME));
187: counter++;
188: }
189: assertEquals(counter, 150 * order.length);
190: counter = 0;
191: }
192: sub_1.testEnded();
193: }
194:
195: public void testByPercent100() throws Exception {
196: ThroughputController sub_1 = new ThroughputController();
197: sub_1.setStyle(ThroughputController.BYPERCENT);
198: sub_1.setPercentThroughput(100.0f);
199: sub_1.addTestElement(new TestSampler("one"));
200: sub_1.addTestElement(new TestSampler("two"));
201:
202: LoopController controller = new LoopController();
203: controller.setLoops(150);
204: controller.addTestElement(new TestSampler("zero"));
205: controller.addTestElement(sub_1);
206: controller.addIterationListener(sub_1);
207: controller.addTestElement(new TestSampler("three"));
208:
209: String[] order = new String[] { "zero", "one", "two", "three", };
210: int counter = 0;
211: controller.setRunningVersion(true);
212: sub_1.setRunningVersion(true);
213: sub_1.testStarted();
214: controller.initialize();
215: for (int i = 0; i < 3; i++) {
216: TestElement sampler = null;
217: while ((sampler = controller.next()) != null) {
218: assertEquals("Counter: " + counter + ", i: " + i,
219: order[counter % order.length], sampler
220: .getPropertyAsString(TestElement.NAME));
221: counter++;
222: }
223: assertEquals(counter, 150 * order.length);
224: counter = 0;
225: }
226: sub_1.testEnded();
227: }
228: }
|