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: */
018:
019: package org.apache.jmeter.control;
020:
021: import java.io.Serializable;
022:
023: import org.apache.jmeter.samplers.Sampler;
024: import org.apache.jmeter.testelement.property.LongProperty;
025: import org.apache.jmeter.testelement.property.StringProperty;
026:
027: public class RunTime extends GenericController implements Serializable {
028:
029: private final static String SECONDS = "RunTime.seconds"; //$NON-NLS-1$
030:
031: private volatile long startTime = 0;
032:
033: private int loopCount = 0; // for getIterCount
034:
035: public RunTime() {
036: }
037:
038: public void setRuntime(long seconds) {
039: setProperty(new LongProperty(SECONDS, seconds));
040: }
041:
042: public void setRuntime(String seconds) {
043: setProperty(new StringProperty(SECONDS, seconds));
044: }
045:
046: public long getRuntime() {
047: try {
048: return Long.parseLong(getPropertyAsString(SECONDS));
049: } catch (NumberFormatException e) {
050: return 0L;
051: }
052: }
053:
054: public String getRuntimeString() {
055: return getPropertyAsString(SECONDS);
056: }
057:
058: /*
059: * (non-Javadoc)
060: *
061: * @see org.apache.jmeter.control.Controller#isDone()
062: */
063: public boolean isDone() {
064: if (getRuntime() > 0 && getSubControllers().size() > 0) {
065: return super .isDone();
066: }
067: return true; // Runtime is zero - no point staying around
068: }
069:
070: private boolean endOfLoop() {
071: return System.currentTimeMillis() - startTime >= 1000 * getRuntime();
072: }
073:
074: public Sampler next() {
075: if (startTime == 0)
076: startTime = System.currentTimeMillis();
077: if (endOfLoop()) {
078: reInitialize();// ??
079: resetLoopCount();
080: return null;
081: }
082: return super .next();
083: }
084:
085: /*
086: * (non-Javadoc)
087: *
088: * @see org.apache.jmeter.control.GenericController#nextIsNull()
089: */
090: protected Sampler nextIsNull() throws NextIsNullException {
091: reInitialize();
092: if (endOfLoop()) {
093: resetLoopCount();
094: return null;
095: }
096: return next();
097: }
098:
099: protected void incrementLoopCount() {
100: loopCount++;
101: }
102:
103: protected void resetLoopCount() {
104: loopCount = 0;
105: startTime = 0;
106: }
107:
108: /*
109: * This is needed for OnceOnly to work like other Loop Controllers
110: */
111: protected int getIterCount() {
112: return loopCount + 1;
113: }
114:
115: protected void reInitialize() {
116: setFirst(true);
117: resetCurrent();
118: incrementLoopCount();
119: recoverRunningVersion();
120: }
121: }
|