Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.control » [javadoc | source]
    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    */
   18   
   19   package org.apache.jmeter.control;
   20   
   21   import java.io.Serializable;
   22   
   23   import org.apache.jmeter.engine.event.LoopIterationEvent;
   24   import org.apache.jmeter.engine.event.LoopIterationListener;
   25   import org.apache.jmeter.samplers.Sampler;
   26   import org.apache.jmeter.testelement.TestListener;
   27   import org.apache.jmeter.testelement.property.BooleanProperty;
   28   import org.apache.jmeter.testelement.property.FloatProperty;
   29   import org.apache.jmeter.testelement.property.IntegerProperty;
   30   import org.apache.jmeter.testelement.property.JMeterProperty;
   31   import org.apache.jmeter.testelement.property.StringProperty;
   32   
   33   /**
   34    * This class represents a controller that can control the number of times that
   35    * it is executed, either by the total number of times the user wants the
   36    * controller executed (BYNUMBER) or by the percentage of time it is called
   37    * (BYPERCENT)
   38    *
   39    * The current implementation executes the first N samples (BYNUMBER)
   40    * or the last N% of samples (BYPERCENT).
   41    */
   42   public class ThroughputController extends GenericController implements Serializable, LoopIterationListener,
   43           TestListener {
   44   
   45       private static final long serialVersionUID = 233L;
   46   
   47       public static final int BYNUMBER = 0;
   48   
   49       public static final int BYPERCENT = 1;
   50   
   51       private static final String STYLE = "ThroughputController.style";// $NON-NLS-1$
   52   
   53       private static final String PERTHREAD = "ThroughputController.perThread";// $NON-NLS-1$
   54   
   55       private static final String MAXTHROUGHPUT = "ThroughputController.maxThroughput";// $NON-NLS-1$
   56   
   57       private static final String PERCENTTHROUGHPUT = "ThroughputController.percentThroughput";// $NON-NLS-1$
   58   
   59       private static class MutableInteger{
   60           private int integer;
   61           MutableInteger(int value){
   62               integer=value;
   63           }
   64           int incr(){
   65               return ++integer;
   66           }
   67           public int intValue() {
   68               return integer;
   69           }
   70       }
   71       
   72       // These items are shared between threads in a group by the clone() method
   73       // They are initialised by testStarted() so don't need to be serialised
   74       private transient MutableInteger globalNumExecutions;
   75   
   76       private transient MutableInteger globalIteration;
   77   
   78       private String counterLock = ""; // ensure counts are updated correctly
   79       // Need to use something that is serializable, so Object is no use
   80   
   81       /**
   82        * Number of iterations on which we've chosen to deliver samplers.
   83        */
   84       private int numExecutions = 0;
   85   
   86       /**
   87        * Index of the current iteration. 0-based.
   88        */
   89       private int iteration = -1;
   90   
   91       /**
   92        * Whether to deliver samplers on this iteration.
   93        */
   94       private boolean runThisTime;
   95   
   96       public ThroughputController() {
   97           setStyle(BYNUMBER);
   98           setPerThread(true);
   99           setMaxThroughput(1);
  100           setPercentThroughput(100);
  101           runThisTime = false;
  102       }
  103   
  104       public void setStyle(int style) {
  105           setProperty(new IntegerProperty(STYLE, style));
  106       }
  107   
  108       public int getStyle() {
  109           return getPropertyAsInt(STYLE);
  110       }
  111   
  112       public void setPerThread(boolean perThread) {
  113           setProperty(new BooleanProperty(PERTHREAD, perThread));
  114       }
  115   
  116       public boolean isPerThread() {
  117           return getPropertyAsBoolean(PERTHREAD);
  118       }
  119   
  120       public void setMaxThroughput(int maxThroughput) {
  121           setProperty(new IntegerProperty(MAXTHROUGHPUT, maxThroughput));
  122       }
  123   
  124       public void setMaxThroughput(String maxThroughput) {
  125           setProperty(new StringProperty(MAXTHROUGHPUT, maxThroughput));
  126       }
  127   
  128       public String getMaxThroughput() {
  129           return getPropertyAsString(MAXTHROUGHPUT);
  130       }
  131   
  132       protected int getMaxThroughputAsInt() {
  133           JMeterProperty prop = getProperty(MAXTHROUGHPUT);
  134           int retVal = 1;
  135           if (prop instanceof IntegerProperty) {
  136               retVal = (((IntegerProperty) prop).getIntValue());
  137           } else {
  138               try {
  139                   retVal = Integer.parseInt(prop.getStringValue());
  140               } catch (NumberFormatException e) {
  141               }
  142           }
  143           return retVal;
  144       }
  145   
  146       public void setPercentThroughput(float percentThroughput) {
  147           setProperty(new FloatProperty(PERCENTTHROUGHPUT, percentThroughput));
  148       }
  149   
  150       public void setPercentThroughput(String percentThroughput) {
  151           setProperty(new StringProperty(PERCENTTHROUGHPUT, percentThroughput));
  152       }
  153   
  154       public String getPercentThroughput() {
  155           return getPropertyAsString(PERCENTTHROUGHPUT);
  156       }
  157   
  158       protected float getPercentThroughputAsFloat() {
  159           JMeterProperty prop = getProperty(PERCENTTHROUGHPUT);
  160           float retVal = 100;
  161           if (prop instanceof FloatProperty) {
  162               retVal = (((FloatProperty) prop).getFloatValue());
  163           } else {
  164               try {
  165                   retVal = Float.parseFloat(prop.getStringValue());
  166               } catch (NumberFormatException e) {
  167               }
  168           }
  169           return retVal;
  170       }
  171   
  172       private int getExecutions() {
  173           if (!isPerThread()) {
  174               synchronized (counterLock) {
  175                   return globalNumExecutions.intValue();
  176               }
  177           }
  178           return numExecutions;
  179       }
  180   
  181       /**
  182        * @see org.apache.jmeter.control.Controller#next()
  183        */
  184       public Sampler next() {
  185           if (runThisTime) {
  186               return super.next();
  187           }
  188           return null;
  189       }
  190   
  191       /**
  192        * Decide whether to return any samplers on this iteration.
  193        */
  194       private boolean decide(int executions, int iterations) {
  195           if (getStyle() == BYNUMBER) {
  196               return executions < getMaxThroughputAsInt();
  197           }
  198           return (100.0 * executions + 50.0) / (iterations + 1) < getPercentThroughputAsFloat();
  199       }
  200   
  201       /**
  202        * @see org.apache.jmeter.control.Controller#isDone()
  203        */
  204       public boolean isDone() {
  205           if (subControllersAndSamplers.size() == 0) {
  206               return true;
  207           } else if (getStyle() == BYNUMBER && getExecutions() >= getMaxThroughputAsInt()
  208                   && current >= getSubControllers().size()) {
  209               return true;
  210           } else {
  211               return false;
  212           }
  213       }
  214   
  215       public Object clone() {
  216           ThroughputController clone = (ThroughputController) super.clone();
  217           clone.numExecutions = numExecutions;
  218           clone.iteration = iteration;
  219           clone.runThisTime = false;
  220           // Ensure global counters and lock are shared across threads in the group
  221           clone.globalIteration = globalIteration;
  222           clone.globalNumExecutions = globalNumExecutions;
  223           clone.counterLock = counterLock;
  224           return clone;
  225       }
  226   
  227       public void iterationStart(LoopIterationEvent iterEvent) {
  228           if (!isPerThread()) {
  229               synchronized (counterLock) {
  230                   globalIteration.incr();
  231                   runThisTime = decide(globalNumExecutions.intValue(), globalIteration.intValue());
  232                   if (runThisTime) {
  233                       globalNumExecutions.incr();
  234                   }
  235               }
  236           } else {
  237               iteration++;
  238               runThisTime = decide(numExecutions, iteration);
  239               if (runThisTime) {
  240                   numExecutions++;
  241               }
  242           }
  243       }
  244   
  245       public void testStarted() {
  246           synchronized (counterLock) {
  247               globalNumExecutions = new MutableInteger(0);
  248               globalIteration = new MutableInteger(-1);
  249           }
  250       }
  251   
  252       public void testStarted(String host) {
  253           testStarted();
  254       }
  255   
  256       public void testEnded() {
  257       }
  258   
  259       public void testEnded(String host) {
  260       }
  261   
  262       public void testIterationStart(LoopIterationEvent event) {
  263       }
  264   }

Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.control » [javadoc | source]