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 package org.apache.commons.performance;
018
019 import java.util.logging.Logger;
020 import junit.framework.Test;
021 import junit.framework.TestCase;
022 import junit.framework.TestSuite;
023
024 public class ClientThreadTest extends TestCase {
025
026 protected ClientThread clientThread = null;
027 protected static Logger logger = Logger.getLogger(LoadGenerator.class.getName());
028 protected static Statistics stats = new Statistics();
029
030 // Dummy ClientThread concrete class to instantiate in tests
031 class nullClientThread extends ClientThread {
032 public nullClientThread(long iterations, long minDelay, long maxDelay,
033 double sigma, String delayType, long rampPeriod,
034 long peakPeriod, long troughPeriod, String cycleType,
035 String rampType, Logger logger,
036 Statistics stats) {
037 super(iterations, minDelay, maxDelay, sigma, delayType, rampPeriod,
038 peakPeriod, troughPeriod, cycleType, rampType, logger,
039 stats);
040 }
041 public void execute() {}
042 }
043
044 public ClientThreadTest(String name) {
045 super(name);
046 }
047
048
049 public static Test suite() {
050 return new TestSuite(ClientThreadTest.class);
051 }
052
053 public void setUp() throws Exception {
054 clientThread = new nullClientThread(
055 1000, // iterations
056 100, // minDelay
057 1000, // maxDelay
058 100, // sigma
059 "constant", // delayType
060 1000, // ramp period
061 2000, // peak period
062 3000, // trough period
063 "oscillating", // cycle type
064 "linear", // ramp type
065 logger, stats);
066 }
067
068 // ======================================================
069 // computCyclicDelay tests
070 // ======================================================
071
072 public void testComputeCyclicDelayRamp() throws Exception {
073 clientThread.setCycleState(ClientThread.RAMPING_UP);
074 clientThread.setPeriodStart(1000);
075 assertEquals(150, clientThread.computeCyclicDelay(1500, 100, 200), 10E-12);
076 assertEquals(110, clientThread.computeCyclicDelay(1900, 100, 200), 10E-12);
077 clientThread.setCycleState(ClientThread.RAMPING_DOWN);
078 assertEquals(150, clientThread.computeCyclicDelay(1500, 100, 200), 10E-12);
079 assertEquals(190, clientThread.computeCyclicDelay(1900, 100, 200), 10E-12);
080 }
081
082 public void testComputeCyclicDelayConst() throws Exception {
083 clientThread.setCycleState(ClientThread.PEAK_LOAD);
084 clientThread.setPeriodStart(1000);
085 assertEquals(100, clientThread.computeCyclicDelay(1500, 100, 200), 10E-12);
086 assertEquals(100, clientThread.computeCyclicDelay(1900, 100, 200), 10E-12);
087 clientThread.setCycleState(ClientThread.TROUGH_LOAD);
088 assertEquals(200, clientThread.computeCyclicDelay(1500, 100, 200), 10E-12);
089 assertEquals(200, clientThread.computeCyclicDelay(1900, 100, 200), 10E-12);
090 }
091
092 public void testCyclicDelayRandom() throws Exception {
093 clientThread.setRampType("random");
094 clientThread.setCycleState(ClientThread.RAMPING_UP);
095 clientThread.setPeriodStart(1000);
096 clientThread.setLastMean(200);
097 for (int i = 1; i < 10; i++) {
098 double nextMean = clientThread.computeCyclicDelay(1500, 100, 200);
099 assertTrue(nextMean <= 200 && nextMean >= 100 &&
100 nextMean <= clientThread.getLastMean());
101 clientThread.setLastMean(nextMean);
102 }
103 clientThread.setCycleState(ClientThread.RAMPING_DOWN);
104 clientThread.setPeriodStart(1000);
105 clientThread.setLastMean(100);
106 for (int i = 1; i < 10; i++) {
107 double nextMean = clientThread.computeCyclicDelay(1500, 100, 200);
108 assertTrue(nextMean <= 200 && nextMean >= 100 &&
109 nextMean >= clientThread.getLastMean());
110 clientThread.setLastMean(nextMean);
111 }
112 }
113
114 // ======================================================
115 // adjustState tests
116 // ======================================================
117 public void testAdjustStateNoChange() throws Exception {
118 clientThread.setPeriodStart(1000);
119 clientThread.setCycleState(ClientThread.RAMPING_UP);
120 clientThread.setRampPeriod(1000);
121 clientThread.adjustState(1100);
122 assertEquals(ClientThread.RAMPING_UP, clientThread.getCycleState());
123 clientThread.setCycleState(ClientThread.RAMPING_DOWN);
124 clientThread.adjustState(1100);
125 assertEquals(ClientThread.RAMPING_DOWN, clientThread.getCycleState());
126 clientThread.setCycleState(ClientThread.PEAK_LOAD);
127 clientThread.setPeakPeriod(1000);
128 clientThread.adjustState(1100);
129 assertEquals(ClientThread.PEAK_LOAD, clientThread.getCycleState());
130 clientThread.setCycleState(ClientThread.TROUGH_LOAD);
131 clientThread.setPeakPeriod(1000);
132 clientThread.adjustState(1100);
133 assertEquals(ClientThread.TROUGH_LOAD, clientThread.getCycleState());
134 }
135
136 public void testStateTransitions() throws Exception {
137 clientThread.setPeakPeriod(1500);
138 clientThread.setRampPeriod(1000);
139 clientThread.setTroughPeriod(2000);
140
141 // Ramping up to peak
142 clientThread.setPeriodStart(1000);
143 clientThread.setCycleState(ClientThread.RAMPING_UP);
144 clientThread.adjustState(2100);
145 assertEquals(ClientThread.PEAK_LOAD, clientThread.getCycleState());
146 assertEquals(2100, clientThread.getPeriodStart());
147 assertEquals((double) clientThread.getMinDelay(), clientThread.getLastMean());
148
149 // Peak to ramping down
150 clientThread.adjustState(3700);
151 assertEquals(ClientThread.RAMPING_DOWN, clientThread.getCycleState());
152 assertEquals(3700, clientThread.getPeriodStart());
153 assertEquals((double) clientThread.getMinDelay(), clientThread.getLastMean());
154
155 // Ramping down to trough
156 clientThread.adjustState(4800);
157 assertEquals(ClientThread.TROUGH_LOAD, clientThread.getCycleState());
158 assertEquals(4800, clientThread.getPeriodStart());
159 assertEquals((double) clientThread.getMaxDelay(), clientThread.getLastMean());
160
161 // Trough to ramping up
162 clientThread.adjustState(6900);
163 assertEquals(ClientThread.RAMPING_UP, clientThread.getCycleState());
164 assertEquals(6900, clientThread.getPeriodStart());
165 assertEquals((double) clientThread.getMaxDelay(), clientThread.getLastMean());
166 }
167
168 //=======================================================
169 // Lifecycle tests
170 //=======================================================
171
172 public void testLifeCycle() {
173 TesterClientThread testerThread = new TesterClientThread(
174 10, // iterations
175 100, // minDelay
176 1000, // maxDelay
177 100, // sigma
178 "constant", // delayType
179 1000, // ramp period
180 2000, // peak period
181 3000, // trough period
182 "oscillating", // cycle type
183 "linear", // ramp type
184 logger,
185 stats,
186 0, // min service delay
187 100, // max service delay
188 50, // mean service delay
189 10, // std dev of service delay
190 "uniform"); // service delay distribution
191 assertFalse(testerThread.isInitialized());
192 testerThread.run();
193 assertEquals(10, testerThread.getSetups());
194 assertEquals(10, testerThread.getTearDowns());
195 assertTrue(testerThread.isFinalized());
196 assertTrue(testerThread.isInitialized());
197 }
198
199 public void testLifeCycleThrowing() {
200 TesterClientThread testerThread = new TesterClientThread(
201 10, // iterations
202 100, // minDelay
203 1000, // maxDelay
204 100, // sigma
205 "constant", // delayType
206 1000, // ramp period
207 2000, // peak period
208 3000, // trough period
209 "oscillating", // cycle type
210 "linear", // ramp type
211 logger,
212 stats,
213 0, // min service delay
214 100, // max service delay
215 50, // mean service delay
216 10, // std dev of service delay
217 "uniform"); // service delay distribution
218 assertFalse(testerThread.isInitialized());
219 testerThread.setHurling(true);
220 testerThread.run();
221 assertEquals(10, testerThread.getSetups());
222 assertEquals(10, testerThread.getTearDowns());
223 assertTrue(testerThread.isFinalized());
224 assertTrue(testerThread.isInitialized());
225 }
226
227 }