KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > timers > ConstantThroughputTimer


1 // $Header: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/timers/ConstantThroughputTimer.java,v 1.14.2.2 2004/10/13 00:38:39 sebb Exp $
2
/*
3  * Copyright 2002-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.timers;
20
21 import org.apache.jmeter.engine.event.LoopIterationEvent;
22 import org.apache.jmeter.testbeans.TestBean;
23 import org.apache.jmeter.testelement.TestListener;
24 import org.apache.jmeter.util.JMeterUtils;
25 import org.apache.jorphan.logging.LoggingManager;
26 import org.apache.log.Logger;
27
28 /**
29  * This class implements a constant throughput timer. A Constant Throughtput
30  * Timer paces the samplers under it's influence so that the total number of
31  * samples per unit of time approaches a given constant as much as possible.
32  *
33  * @version $Id: ConstantThroughputTimer.java,v 1.14.2.2 2004/10/13 00:38:39 sebb Exp $
34  */

35 public class ConstantThroughputTimer
36         extends TestBean
37         implements Timer, TestListener
38 {
39     private static final Logger log = LoggingManager.getLoggerForClass();
40
41     /**
42      * Target time for the start of the next request. The delay provided by
43      * the timer will be calculated so that the next request happens at this
44      * time.
45      */

46     private long targetTime= 0;
47
48     /**
49      * Desired throughput, in samples per minute.
50      */

51     private double throughput;
52
53     /**
54      * Constructor for a non-configured ConstantThroughputTimer.
55      */

56     public ConstantThroughputTimer()
57     {
58     }
59
60     /**
61      * Sets the desired throughput.
62      *
63      * @param throughput Desired sampling rate, in samples per minute.
64      */

65     public void setThroughput(double throughput)
66     {
67         this.throughput= throughput;
68     }
69
70     /**
71      * Gets the configured desired throughput.
72      *
73      * @return the rate at which samples should occur, in samples per minute.
74      */

75     public double getThroughput()
76     {
77         return throughput;
78     }
79     
80     /**
81      * Retrieve the delay to use during test execution.
82      *
83      * @see org.apache.jmeter.timers.Timer#delay()
84      */

85     public synchronized long delay()
86     {
87         long currentTime = System.currentTimeMillis();
88         long currentTarget = targetTime == 0 ? currentTime : targetTime;
89         targetTime = currentTarget + (long)( 60000.0 / getThroughput() );
90         if (currentTime > currentTarget)
91         {
92             // We're behind schedule -- try to catch up:
93
return 0;
94         }
95         return currentTarget - currentTime;
96     }
97
98     /**
99      * Provide a description of this timer class.
100      *
101      * TODO: Is this ever used? I can't remember where. Remove if it isn't --
102      * TODO: or obtain text from bean's displayName or shortDescription.
103      *
104      * @return the description of this timer class.
105      */

106     public String JavaDoc toString()
107     {
108         return JMeterUtils.getResString("constant_throughput_timer_memo");
109     }
110
111     /**
112      * Get the timer ready to compute delays for a new test.
113      *
114      * @see org.apache.jmeter.testelement.TestListener#testStarted()
115      */

116     public synchronized void testStarted()//synch to protect targetTime
117
{
118         log.debug("Test started - reset throughput calculation.");
119         targetTime= 0;
120     }
121
122     /* (non-Javadoc)
123      * @see org.apache.jmeter.testelement.TestListener#testEnded()
124      */

125     public void testEnded()
126     {
127     }
128
129     /* (non-Javadoc)
130      * @see org.apache.jmeter.testelement.TestListener#testStarted(java.lang.String)
131      */

132     public void testStarted(String JavaDoc host)
133     {
134     }
135
136     /* (non-Javadoc)
137      * @see org.apache.jmeter.testelement.TestListener#testEnded(java.lang.String)
138      */

139     public void testEnded(String JavaDoc host)
140     {
141     }
142
143     /* (non-Javadoc)
144      * @see org.apache.jmeter.testelement.TestListener#testIterationStart(org.apache.jmeter.engine.event.LoopIterationEvent)
145      */

146     public void testIterationStart(LoopIterationEvent event)
147     {
148     }
149 }
Popular Tags