KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/control/RunTime.java,v 1.1.2.4 2005/03/17 00:34:43 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.Serializable JavaDoc;
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 //NOTUSED import org.apache.jorphan.logging.LoggingManager;
27
//NOTUSED import org.apache.log.Logger;
28

29 /**
30  * @version $Revision: 1.1.2.4 $
31  */

32 public class RunTime extends GenericController implements Serializable JavaDoc
33 {
34     //NOTUSED private static Logger log = LoggingManager.getLoggerForClass();
35

36     private final static String JavaDoc SECONDS = "RunTime.seconds";
37     private volatile long startTime = 0;
38
39     private int loopCount = 0; // for getIterCount
40

41     public RunTime()
42     {
43     }
44
45     public void setRuntime(long seconds)
46     {
47         setProperty(new LongProperty(SECONDS, seconds));
48     }
49
50     public void setRuntime(String JavaDoc seconds)
51     {
52         setProperty(new StringProperty(SECONDS, seconds));
53     }
54
55     public long getRuntime()
56     {
57         try
58         {
59             return Long.parseLong(getPropertyAsString(SECONDS));
60         }
61         catch (NumberFormatException JavaDoc e)
62         {
63             return 0L;
64         }
65     }
66
67     public String JavaDoc getRuntimeString()
68     {
69         return getPropertyAsString(SECONDS);
70     }
71
72     /* (non-Javadoc)
73      * @see org.apache.jmeter.control.Controller#isDone()
74      */

75     public boolean isDone()
76     {
77         if (getRuntime() > 0 && getSubControllers().size() > 0)
78         {
79             return super.isDone();
80         }
81         else
82         {
83             return true; // Runtime is zero - no point staying around
84
}
85     }
86
87     private boolean endOfLoop()
88     {
89         return System.currentTimeMillis()-startTime >= 1000*getRuntime();
90     }
91
92     public Sampler next()
93     {
94         if (startTime == 0) startTime=System.currentTimeMillis();
95         return super.next();
96     }
97     /* (non-Javadoc)
98      * @see org.apache.jmeter.control.GenericController#nextIsNull()
99      */

100     protected Sampler nextIsNull() throws NextIsNullException
101     {
102         reInitialize();
103         if (endOfLoop())
104         {
105             resetLoopCount();
106             return null;
107         }
108         else
109         {
110             return next();
111         }
112     }
113
114     protected void incrementLoopCount()
115     {
116         loopCount++;
117     }
118     protected void resetLoopCount()
119     {
120         loopCount=0;
121         startTime=0;
122     }
123     /*
124      * This is needed for OnceOnly to work like other Loop Controllers
125      */

126     protected int getIterCount()
127     {
128         return loopCount + 1;
129     }
130     protected void reInitialize()
131     {
132         setFirst(true);
133         resetCurrent();
134         incrementLoopCount();
135     }
136 }
Popular Tags