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.samplers.Sampler;
   24   import org.apache.jmeter.testelement.property.LongProperty;
   25   import org.apache.jmeter.testelement.property.StringProperty;
   26   
   27   public class RunTime extends GenericController implements Serializable {
   28   
   29       private final static String SECONDS = "RunTime.seconds"; //$NON-NLS-1$
   30   
   31       private volatile long startTime = 0;
   32   
   33       private int loopCount = 0; // for getIterCount
   34   
   35       public RunTime() {
   36       }
   37   
   38       public void setRuntime(long seconds) {
   39           setProperty(new LongProperty(SECONDS, seconds));
   40       }
   41   
   42       public void setRuntime(String seconds) {
   43           setProperty(new StringProperty(SECONDS, seconds));
   44       }
   45   
   46       public long getRuntime() {
   47           try {
   48               return Long.parseLong(getPropertyAsString(SECONDS));
   49           } catch (NumberFormatException e) {
   50               return 0L;
   51           }
   52       }
   53   
   54       public String getRuntimeString() {
   55           return getPropertyAsString(SECONDS);
   56       }
   57   
   58       /*
   59        * (non-Javadoc)
   60        *
   61        * @see org.apache.jmeter.control.Controller#isDone()
   62        */
   63       public boolean isDone() {
   64           if (getRuntime() > 0 && getSubControllers().size() > 0) {
   65               return super.isDone();
   66           }
   67           return true; // Runtime is zero - no point staying around
   68       }
   69   
   70       private boolean endOfLoop() {
   71           return System.currentTimeMillis() - startTime >= 1000 * getRuntime();
   72       }
   73   
   74       public Sampler next() {
   75           if (startTime == 0) {
   76               startTime = System.currentTimeMillis();
   77           }
   78           if (endOfLoop()) {
   79               reInitialize();// ??
   80               resetLoopCount();
   81               return null;
   82           }
   83           return super.next();
   84       }
   85   
   86       /*
   87        * (non-Javadoc)
   88        *
   89        * @see org.apache.jmeter.control.GenericController#nextIsNull()
   90        */
   91       protected Sampler nextIsNull() throws NextIsNullException {
   92           reInitialize();
   93           if (endOfLoop()) {
   94               resetLoopCount();
   95               return null;
   96           }
   97           return next();
   98       }
   99   
  100       protected void incrementLoopCount() {
  101           loopCount++;
  102       }
  103   
  104       protected void resetLoopCount() {
  105           loopCount = 0;
  106           startTime = 0;
  107       }
  108   
  109       /*
  110        * This is needed for OnceOnly to work like other Loop Controllers
  111        */
  112       protected int getIterCount() {
  113           return loopCount + 1;
  114       }
  115   
  116       protected void reInitialize() {
  117           setFirst(true);
  118           resetCurrent();
  119           incrementLoopCount();
  120           recoverRunningVersion();
  121       }
  122   }

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