1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.performance;
18  
19  import java.util.logging.Logger;
20  import junit.framework.Test;
21  import junit.framework.TestCase;
22  import junit.framework.TestSuite;
23  
24  public class ClientThreadTest extends TestCase {
25      
26    protected ClientThread clientThread = null;
27    protected static Logger logger = Logger.getLogger(LoadGenerator.class.getName());
28    protected static Statistics stats = new Statistics();
29    
30    // Dummy ClientThread concrete class to instantiate in tests
31    class nullClientThread extends ClientThread {
32        public nullClientThread(long iterations, long minDelay, long maxDelay,
33                double sigma, String delayType, long rampPeriod,
34                long peakPeriod, long troughPeriod, String cycleType,
35                String rampType, Logger logger,
36                Statistics stats) {    
37            super(iterations, minDelay, maxDelay, sigma, delayType, rampPeriod,
38                    peakPeriod, troughPeriod, cycleType, rampType, logger,
39                    stats);
40        }
41        public void execute() {}
42    }
43    
44    public ClientThreadTest(String name) {
45      super(name);
46    }
47  
48  
49    public static Test suite() {
50      return new TestSuite(ClientThreadTest.class);
51    }
52    
53    public void setUp() throws Exception {
54      clientThread = new nullClientThread(
55              1000, // iterations
56              100,  // minDelay
57              1000, // maxDelay
58              100,  // sigma
59              "constant", // delayType
60              1000, // ramp period
61              2000, // peak period
62              3000, // trough period
63              "oscillating", // cycle type
64              "linear", // ramp type
65              logger, stats);
66    }
67  
68    // ======================================================
69    //   computCyclicDelay tests
70    // ======================================================
71    
72    public void testComputeCyclicDelayRamp() throws Exception {
73        clientThread.setCycleState(ClientThread.RAMPING_UP);
74        clientThread.setPeriodStart(1000);
75        assertEquals(150, clientThread.computeCyclicDelay(1500, 100, 200), 10E-12);
76        assertEquals(110, clientThread.computeCyclicDelay(1900, 100, 200), 10E-12);
77        clientThread.setCycleState(ClientThread.RAMPING_DOWN);
78        assertEquals(150, clientThread.computeCyclicDelay(1500, 100, 200), 10E-12);
79        assertEquals(190, clientThread.computeCyclicDelay(1900, 100, 200), 10E-12);
80    }
81    
82    public void testComputeCyclicDelayConst() throws Exception {
83        clientThread.setCycleState(ClientThread.PEAK_LOAD);
84        clientThread.setPeriodStart(1000);
85        assertEquals(100, clientThread.computeCyclicDelay(1500, 100, 200), 10E-12);
86        assertEquals(100, clientThread.computeCyclicDelay(1900, 100, 200), 10E-12);
87        clientThread.setCycleState(ClientThread.TROUGH_LOAD);
88        assertEquals(200, clientThread.computeCyclicDelay(1500, 100, 200), 10E-12);
89        assertEquals(200, clientThread.computeCyclicDelay(1900, 100, 200), 10E-12);
90    }
91    
92    public void testCyclicDelayRandom() throws Exception {
93        clientThread.setRampType("random");
94        clientThread.setCycleState(ClientThread.RAMPING_UP);
95        clientThread.setPeriodStart(1000);
96        clientThread.setLastMean(200);
97        for (int i = 1; i < 10; i++) {
98            double nextMean = clientThread.computeCyclicDelay(1500, 100, 200);
99            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 }