KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > control > ThroughputController


1 // $Header: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/control/ThroughputController.java,v 1.15.2.1 2004/10/24 01:03:46 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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 */

18
19 package org.apache.jmeter.control;
20
21 import java.io.IOException JavaDoc;
22 import java.io.Serializable JavaDoc;
23
24 import org.apache.jmeter.engine.event.LoopIterationEvent;
25 import org.apache.jmeter.engine.event.LoopIterationListener;
26 import org.apache.jmeter.junit.JMeterTestCase;
27 import org.apache.jmeter.junit.stubs.TestSampler;
28 import org.apache.jmeter.samplers.Sampler;
29 import org.apache.jmeter.testelement.TestElement;
30 import org.apache.jmeter.testelement.TestListener;
31 import org.apache.jmeter.testelement.property.BooleanProperty;
32 import org.apache.jmeter.testelement.property.FloatProperty;
33 import org.apache.jmeter.testelement.property.IntegerProperty;
34 import org.apache.jmeter.testelement.property.JMeterProperty;
35 import org.apache.jmeter.testelement.property.StringProperty;
36
37 /**
38  * This class represents a controller that can controll the
39  * number of times that it is executed, either by the total number
40  * of times the user wants the controller executed (BYNUMBER)
41  * or by the percentage of time it is called (BYPERCENT)
42  *
43  * @author Thad Smith
44  * @version $Revision: 1.15.2.1 $
45  */

46 public class ThroughputController
47     extends GenericController
48     implements Serializable JavaDoc, LoopIterationListener, TestListener
49 {
50
51     public static final int BYNUMBER = 0;
52     public static final int BYPERCENT = 1;
53
54     private static final String JavaDoc STYLE = "ThroughputController.style";
55     private static final String JavaDoc PERTHREAD = "ThroughputController.perThread";
56     private static final String JavaDoc MAXTHROUGHPUT =
57         "ThroughputController.maxThroughput";
58     private static final String JavaDoc PERCENTTHROUGHPUT =
59         "ThroughputController.percentThroughput";
60
61     private int globalNumExecutions;
62     private int globalIteration;
63     private transient Object JavaDoc counterLock;
64
65     /**
66      * Number of iterations on which we've chosen to deliver samplers.
67      */

68     private int numExecutions = 0;
69     
70     /**
71      * Index of the current iteration. 0-based.
72      */

73     private int iteration = -1;
74     
75     /**
76      * Whether to deliver samplers on this iteration.
77      */

78     private boolean runThisTime;
79
80     public ThroughputController()
81     {
82         globalNumExecutions = 0;
83         globalIteration = -1;
84         counterLock = new Object JavaDoc();
85         setStyle(BYNUMBER);
86         setPerThread(true);
87         setMaxThroughput(1);
88         setPercentThroughput(100);
89         runThisTime = false;
90     }
91
92     public void setStyle(int style)
93     {
94         setProperty(new IntegerProperty(STYLE, style));
95     }
96
97     public int getStyle()
98     {
99         return getPropertyAsInt(STYLE);
100     }
101
102     public void setPerThread(boolean perThread)
103     {
104         setProperty(new BooleanProperty(PERTHREAD, perThread));
105     }
106
107     public boolean isPerThread()
108     {
109         return getPropertyAsBoolean(PERTHREAD);
110     }
111
112     public void setMaxThroughput(int maxThroughput)
113     {
114         setProperty(new IntegerProperty(MAXTHROUGHPUT, maxThroughput));
115     }
116
117     public void setMaxThroughput(String JavaDoc maxThroughput)
118     {
119         setProperty(new StringProperty(MAXTHROUGHPUT, maxThroughput));
120     }
121
122     public String JavaDoc getMaxThroughput()
123     {
124         return getPropertyAsString(MAXTHROUGHPUT);
125     }
126
127     protected int getMaxThroughputAsInt()
128     {
129         JMeterProperty prop = getProperty(MAXTHROUGHPUT);
130         int retVal = 1;
131         if (prop instanceof IntegerProperty)
132         {
133             retVal = (((IntegerProperty) prop).getIntValue());
134         }
135         else
136         {
137             try
138             {
139                 retVal = Integer.parseInt(prop.getStringValue());
140             }
141             catch (NumberFormatException JavaDoc e)
142             {
143             }
144         }
145         return retVal;
146     }
147
148     public void setPercentThroughput(float percentThroughput)
149     {
150         setProperty(new FloatProperty(PERCENTTHROUGHPUT, percentThroughput));
151     }
152
153     public void setPercentThroughput(String JavaDoc percentThroughput)
154     {
155         setProperty(new StringProperty(PERCENTTHROUGHPUT, percentThroughput));
156     }
157
158     public String JavaDoc getPercentThroughput()
159     {
160         return getPropertyAsString(PERCENTTHROUGHPUT);
161     }
162
163     protected float getPercentThroughputAsFloat()
164     {
165         JMeterProperty prop = getProperty(PERCENTTHROUGHPUT);
166         float retVal = 100;
167         if (prop instanceof FloatProperty)
168         {
169             retVal = (((FloatProperty) prop).getFloatValue());
170         }
171         else
172         {
173             try
174             {
175                 retVal = Float.parseFloat(prop.getStringValue());
176             }
177             catch (NumberFormatException JavaDoc e)
178             {
179             }
180         }
181         return retVal;
182     }
183
184     protected void setExecutions(int executions)
185     {
186         if (!isPerThread())
187         {
188             globalNumExecutions=executions;
189         }
190         this.numExecutions = executions;
191     }
192
193     protected int getExecutions()
194     {
195         if (!isPerThread())
196         {
197             return globalNumExecutions;
198         }
199         else
200         {
201             return numExecutions;
202         }
203     }
204
205     private void increaseExecutions()
206     {
207         setExecutions(getExecutions() + 1);
208     }
209
210     protected void setIteration(int iteration)
211     {
212         if (!isPerThread())
213         {
214             globalIteration=iteration;
215         }
216         this.iteration = iteration;
217     }
218
219     protected int getIteration()
220     {
221         if (!isPerThread())
222         {
223             return globalIteration;
224         }
225         else
226         {
227             return iteration;
228         }
229     }
230
231     private void increaseIteration()
232     {
233         setIteration(getIteration() + 1);
234     }
235
236     /**
237      * @see org.apache.jmeter.control.Controller#next()
238      */

239     public Sampler next()
240     {
241         if (runThisTime)
242         {
243             return super.next();
244         }
245         return null;
246     }
247
248     /**
249      * Decide whether to return any samplers on this iteration.
250      */

251     private boolean decide()
252     {
253         int executions, iterations;
254
255         executions = getExecutions();
256         iterations = getIteration();
257         if (getStyle() == BYNUMBER)
258         {
259             return executions < getMaxThroughputAsInt();
260         }
261         else
262         {
263             return (100.0*executions+50.0) / (iterations+1)
264                     < getPercentThroughputAsFloat();
265         }
266     }
267
268     /**
269      * @see org.apache.jmeter.control.Controller#isDone()
270      */

271     public boolean isDone()
272     {
273         if (subControllersAndSamplers.size() == 0)
274         {
275             return true;
276         }
277         else if (
278             getStyle() == BYNUMBER
279                 && getExecutions() >= getMaxThroughputAsInt()
280                 && current >= getSubControllers().size())
281         {
282             return true;
283         }
284         else
285         {
286             return false;
287         }
288     }
289
290     public Object JavaDoc clone()
291     {
292         ThroughputController clone = (ThroughputController) super.clone();
293         clone.numExecutions = numExecutions;
294         clone.iteration = iteration;
295         clone.globalNumExecutions = globalNumExecutions;
296         clone.globalIteration = globalIteration;
297         clone.counterLock = counterLock;
298         clone.runThisTime = false;
299         return clone;
300     }
301
302     private void readObject(java.io.ObjectInputStream JavaDoc in)
303         throws IOException JavaDoc, ClassNotFoundException JavaDoc
304     {
305         in.defaultReadObject();
306         counterLock = new Object JavaDoc();
307     }
308
309     /* (non-Javadoc)
310      * @see LoopIterationListener#iterationStart(LoopIterationEvent)
311      */

312     public void iterationStart(LoopIterationEvent iterEvent)
313     {
314         if (!isPerThread())
315         {
316             synchronized (counterLock)
317             {
318                 increaseIteration();
319                 runThisTime= decide();
320                 if (runThisTime) increaseExecutions();
321             }
322         }
323         else
324         {
325             increaseIteration();
326             runThisTime= decide();
327             if (runThisTime) increaseExecutions();
328         }
329     }
330
331     /*
332      * (non-Javadoc)
333      * @see org.apache.jmeter.testelement.TestListener#testStarted()
334      */

335     public void testStarted()
336     {
337         setExecutions(0);
338         setIteration(-1);
339     }
340
341     /*
342      * (non-Javadoc)
343      * @see org.apache.jmeter.testelement.TestListener#testEnded()
344      */

345     public void testEnded()
346     {
347     }
348
349     /*
350      * (non-Javadoc)
351      * @see TestListener#testStarted(String)
352      */

353     public void testStarted(String JavaDoc host)
354     {
355     }
356
357     /*
358      * (non-Javadoc)
359      * @see TestListener#testEnded(String)
360      */

361     public void testEnded(String JavaDoc host)
362     {
363     }
364
365     /*
366      * (non-Javadoc)
367      * @see TestListener#testIterationStart(LoopIterationEvent)
368      */

369     public void testIterationStart(LoopIterationEvent event)
370     {
371     }
372
373     /////////////////////////// Start of Test Code ///////////////////////////
374

375     public static class Test extends JMeterTestCase
376     {
377         public Test(String JavaDoc name)
378         {
379             super(name);
380         }
381
382         public void testByNumber() throws Exception JavaDoc
383         {
384             ThroughputController sub_1 = new ThroughputController();
385             sub_1.setStyle(BYNUMBER);
386             sub_1.setMaxThroughput(2);
387             sub_1.addTestElement(new TestSampler("one"));
388             sub_1.addTestElement(new TestSampler("two"));
389
390             LoopController loop = new LoopController();
391             loop.setLoops(5);
392             loop.addTestElement(new TestSampler("zero"));
393             loop.addTestElement(sub_1);
394             loop.addIterationListener(sub_1);
395             loop.addTestElement(new TestSampler("three"));
396
397             LoopController test = new LoopController();
398             test.setLoops(2);
399             test.addTestElement(loop);
400
401             String JavaDoc[] order =
402                 new String JavaDoc[] {
403                     "zero",
404                     "one",
405                     "two",
406                     "three",
407                     "zero",
408                     "one",
409                     "two",
410                     "three",
411                     "zero",
412                     "three",
413                     "zero",
414                     "three",
415                     "zero",
416                     "three",
417                     "zero",
418                     "three",
419                     "zero",
420                     "three",
421                     "zero",
422                     "three",
423                     "zero",
424                     "three",
425                     "zero",
426                     "three",
427                 };
428             sub_1.testStarted();
429             test.initialize();
430             for (int counter= 0; counter < order.length; counter++)
431             {
432                 TestElement sampler = test.next();
433                 assertNotNull(sampler);
434                 assertEquals("Counter: "+counter
435                         +", executions: "+sub_1.getExecutions()
436                         +", iteration: "+sub_1.getIteration(),
437                     order[counter],
438                     sampler.getPropertyAsString(TestElement.NAME)
439                     );
440             }
441             assertNull(test.next());
442             sub_1.testEnded();
443         }
444
445         public void testByNumberZero() throws Exception JavaDoc
446         {
447             ThroughputController sub_1 = new ThroughputController();
448             sub_1.setStyle(BYNUMBER);
449             sub_1.setMaxThroughput(0);
450             sub_1.addTestElement(new TestSampler("one"));
451             sub_1.addTestElement(new TestSampler("two"));
452         
453             LoopController controller = new LoopController();
454             controller.setLoops(5);
455             controller.addTestElement(new TestSampler("zero"));
456             controller.addTestElement(sub_1);
457             controller.addIterationListener(sub_1);
458             controller.addTestElement(new TestSampler("three"));
459         
460             String JavaDoc[] order =
461                 new String JavaDoc[] {
462                     "zero",
463                     "three",
464                     "zero",
465                     "three",
466                     "zero",
467                     "three",
468                     "zero",
469                     "three",
470                     "zero",
471                     "three",
472                 };
473             int counter= 0;
474             sub_1.testStarted();
475             controller.initialize();
476             for (int i=0; i<3; i++)
477             {
478                 TestElement sampler = null;
479                 while ((sampler = controller.next()) != null)
480                 {
481                     assertEquals("Counter: "+counter+", i: "+i,
482                         order[counter],
483                         sampler.getPropertyAsString(TestElement.NAME)
484                         );
485                     counter++;
486                 }
487                 assertEquals(counter, order.length);
488                 counter= 0;
489             }
490             sub_1.testEnded();
491         }
492
493         public void testByPercent33() throws Exception JavaDoc
494         {
495             ThroughputController sub_1 = new ThroughputController();
496             sub_1.setStyle(BYPERCENT);
497             sub_1.setPercentThroughput(33.33f);
498             sub_1.addTestElement(new TestSampler("one"));
499             sub_1.addTestElement(new TestSampler("two"));
500
501             LoopController controller = new LoopController();
502             controller.setLoops(6);
503             controller.addTestElement(new TestSampler("zero"));
504             controller.addTestElement(sub_1);
505             controller.addIterationListener(sub_1);
506             controller.addTestElement(new TestSampler("three"));
507             // Expected results established using the DDA
508
// algorithm (see http://www.siggraph.org/education/materials/HyperGraph/scanline/outprims/drawline.htm):
509
String JavaDoc[] order =
510                 new String JavaDoc[] {
511                     "zero", // 0/1 vs. 1/1 -> 0 is closer to 33.33
512
"three",
513                     "zero", // 0/2 vs. 1/2 -> 50.0 is closer to 33.33
514
"one",
515                     "two",
516                     "three",
517                     "zero", // 1/3 vs. 2/3 -> 33.33 is closer to 33.33
518
"three",
519                     "zero", // 1/4 vs. 2/4 -> 25.0 is closer to 33.33
520
"three",
521                     "zero", // 1/5 vs. 2/5 -> 40.0 is closer to 33.33
522
"one",
523                     "two",
524                     "three",
525                     "zero", // 2/6 vs. 3/6 -> 33.33 is closer to 33.33
526
"three",
527                     // etc...
528
};
529             int counter= 0;
530             sub_1.testStarted();
531             controller.initialize();
532             for (int i=0; i<3; i++)
533             {
534                 TestElement sampler = null;
535                 while ((sampler = controller.next()) != null)
536                 {
537                     assertEquals("Counter: "+counter+", i: "+i,
538                         order[counter],
539                         sampler.getPropertyAsString(TestElement.NAME)
540                         );
541                     counter++;
542                 }
543                 assertEquals(counter, order.length);
544                 counter= 0;
545             }
546             sub_1.testEnded();
547         }
548
549         public void testByPercentZero() throws Exception JavaDoc
550         {
551             ThroughputController sub_1 = new ThroughputController();
552             sub_1.setStyle(BYPERCENT);
553             sub_1.setPercentThroughput(0.0f);
554             sub_1.addTestElement(new TestSampler("one"));
555             sub_1.addTestElement(new TestSampler("two"));
556         
557             LoopController controller = new LoopController();
558             controller.setLoops(150);
559             controller.addTestElement(new TestSampler("zero"));
560             controller.addTestElement(sub_1);
561             controller.addIterationListener(sub_1);
562             controller.addTestElement(new TestSampler("three"));
563         
564             String JavaDoc[] order =
565                 new String JavaDoc[] {
566                     "zero",
567                     "three",
568                 };
569             int counter= 0;
570             sub_1.testStarted();
571             controller.initialize();
572             for (int i=0; i<3; i++)
573             {
574                 TestElement sampler = null;
575                 while ((sampler = controller.next()) != null)
576                 {
577                     assertEquals("Counter: "+counter+", i: "+i,
578                         order[counter%order.length],
579                         sampler.getPropertyAsString(TestElement.NAME)
580                         );
581                     counter++;
582                 }
583                 assertEquals(counter, 150*order.length);
584                 counter= 0;
585             }
586             sub_1.testEnded();
587         }
588
589         public void testByPercent100() throws Exception JavaDoc
590         {
591             ThroughputController sub_1 = new ThroughputController();
592             sub_1.setStyle(BYPERCENT);
593             sub_1.setPercentThroughput(100.0f);
594             sub_1.addTestElement(new TestSampler("one"));
595             sub_1.addTestElement(new TestSampler("two"));
596         
597             LoopController controller = new LoopController();
598             controller.setLoops(150);
599             controller.addTestElement(new TestSampler("zero"));
600             controller.addTestElement(sub_1);
601             controller.addIterationListener(sub_1);
602             controller.addTestElement(new TestSampler("three"));
603         
604             String JavaDoc[] order =
605                 new String JavaDoc[] {
606                     "zero",
607                     "one",
608                     "two",
609                     "three",
610                 };
611             int counter= 0;
612             sub_1.testStarted();
613             controller.initialize();
614             for (int i=0; i<3; i++)
615             {
616                 TestElement sampler = null;
617                 while ((sampler = controller.next()) != null)
618                 {
619                     assertEquals("Counter: "+counter+", i: "+i,
620                         order[counter%order.length],
621                         sampler.getPropertyAsString(TestElement.NAME)
622                         );
623                     counter++;
624                 }
625                 assertEquals(counter, 150*order.length);
626                 counter= 0;
627             }
628             sub_1.testEnded();
629         }
630     }
631 }
632
Popular Tags