KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > visualizers > RunningSample


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/visualizers/RunningSample.java,v 1.2.2.2 2004/06/12 20:26:05 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.visualizers;
20
21 import java.text.DecimalFormat JavaDoc;
22
23 import org.apache.jmeter.samplers.SampleResult;
24
25
26 /**
27  * Aggegate sample data container. Just instantiate a new instance of this
28  * class, and then call {@link #addSample(SampleResult)} a few times, and pull
29  * the stats out with whatever methods you prefer.
30  *
31  * @author James Boutcher
32  * @version $Revision: 1.2.2.2 $
33  */

34 public class RunningSample
35 {
36
37     private static DecimalFormat JavaDoc rateFormatter = new DecimalFormat JavaDoc("#.0");
38     private static DecimalFormat JavaDoc errorFormatter = new DecimalFormat JavaDoc("#0.00%");
39
40     private long counter;
41     private volatile long runningSum;
42     private volatile long max, min;
43     private volatile long errorCount;
44     private volatile long firstTime;
45     private volatile long lastTime;
46     private String JavaDoc label;
47     private int index;
48
49     private RunningSample(){// Don't (can't) use this...
50
}
51     
52     /**
53      * Use this constructor.
54      */

55     public RunningSample(String JavaDoc label, int index)
56     {
57         this.label = label;
58         this.index = index;
59         init();
60     }
61     
62     /**
63      * Copy constructor to a duplicate of existing instance
64      * (without the disadvantages of clone()0
65      * @param src RunningSample
66      */

67     public RunningSample(RunningSample src){
68         this.counter = src.counter;
69         this.errorCount = src.errorCount;
70         this.firstTime = src.firstTime;
71         this.index = src.index;
72         this.label = src.label;
73         this.lastTime = src.lastTime;
74         this.max = src.max;
75         this.min = src.min;
76         this.runningSum = src.runningSum;
77     }
78
79     private void init(){
80         counter = 0L;
81         runningSum = 0L;
82         max = Long.MIN_VALUE;
83         min = Long.MAX_VALUE;
84         errorCount = 0L;
85         firstTime = Long.MAX_VALUE;
86         lastTime = 0L;
87     }
88
89     /**
90      * Clear the counters (useful for differential stats)
91      *
92      */

93     public synchronized void clear(){
94         init();
95     }
96     
97     /**
98      * Get the elapsed time for the samples
99      * @return how long the samples took
100      */

101     public long getElapsed(){
102         if (lastTime == 0) return 0;// No samples collected ...
103
return lastTime - firstTime;
104     }
105     /**
106      * Returns the throughput associated to this sampler in requests per second.
107      * May be slightly skewed because it takes the timestamps of the first and
108      * last samples as the total time passed, and the test may actually have
109      * started before that start time and ended after that end time.
110      **/

111     public double getRate()
112     {
113         if (counter == 0) return 0.0; //Better behaviour when howLong=0 or lastTime=0
114

115         long howLongRunning = lastTime - firstTime;
116
117         if (howLongRunning == 0)
118         {
119             return Double.MAX_VALUE;
120         }
121              
122         return (double) counter / howLongRunning * 1000.0;
123     }
124
125     /**
126      * Returns the throughput associated to this sampler in requests per min.
127      * May be slightly skewed because it takes the timestamps of the first and
128      * last samples as the total time passed, and the test may actually have
129      * started before that start time and ended after that end time.
130      **/

131     public double getRatePerMin()
132     {
133         if (counter == 0) return 0.0; //Better behaviour when howLong=0 or lastTime=0
134

135         long howLongRunning = lastTime - firstTime;
136
137         if (howLongRunning == 0)
138         {
139             return Double.MAX_VALUE;
140         }
141         return (double) counter / howLongRunning * 60000.0;
142     }
143
144     /**
145      * Returns a String that represents the throughput associated for this
146      * sampler, in units appropriate to its dimension:
147      * <p>
148      * The number is represented in requests/second or requests/minute or
149      * requests/hour.
150      * <p>
151      * Examples:
152      * "34.2/sec"
153      * "0.1/sec"
154      * "43.0/hour"
155      * "15.9/min"
156      * @return a String representation of the rate the samples are being taken
157      * at.
158      */

159     public String JavaDoc getRateString()
160     {
161         double rate = getRate();
162
163         if (rate == Double.MAX_VALUE)
164         {
165             return "N/A";
166         }
167
168         String JavaDoc unit = "sec";
169
170         if (rate < 1.0)
171         {
172             rate *= 60.0;
173             unit = "min";
174         }
175         if (rate < 1.0)
176         {
177             rate *= 60.0;
178             unit = "hour";
179         }
180
181         String JavaDoc rval = rateFormatter.format(rate) + "/" + unit;
182
183         return (rval);
184     }
185
186     public String JavaDoc getLabel()
187     {
188         return label;
189     }
190
191     public int getIndex()
192     {
193         return index;
194     }
195
196     /**
197      * Records a sample.
198      *
199      */

200     public synchronized void addSample(SampleResult res)
201     {
202         long aTimeInMillis = res.getTime();
203         boolean aSuccessFlag = res.isSuccessful();
204
205         counter++;
206         long startTime = res.getTimeStamp() - aTimeInMillis;
207         if (firstTime > startTime)
208         {
209             // this is our first sample, set the start time to current timestamp
210
firstTime = startTime;
211         }
212         if(lastTime < res.getTimeStamp())
213         {
214             lastTime = res.getTimeStamp();
215         }
216         runningSum += aTimeInMillis;
217         
218         if (aTimeInMillis > max)
219         {
220             max = aTimeInMillis;
221         }
222          
223         if (aTimeInMillis < min)
224         {
225             min = aTimeInMillis;
226         }
227          
228         if (!aSuccessFlag)
229         {
230             errorCount++;
231         }
232     }
233
234
235     /**
236      * Adds another RunningSample to this one
237      * Does not check if it has the same label and index
238      */

239     public synchronized void addSample(RunningSample rs)
240     {
241         this.counter += rs.counter;
242         this.errorCount += rs.errorCount;
243         this.runningSum += rs.runningSum;
244         if (this.firstTime > rs.firstTime) this.firstTime = rs.firstTime;
245         if (this.lastTime < rs.lastTime) this.lastTime = rs.lastTime;
246         if (this.max < rs.max) this.max = rs.max;
247         if (this.min > rs.min) this.min = rs.min;
248     }
249
250
251     /**
252      * Returns the time in milliseconds of the quickest sample.
253      * @return the time in milliseconds of the quickest sample.
254      */

255     public long getMin()
256     {
257         long rval = 0;
258
259         if (min != Long.MAX_VALUE)
260         {
261             rval = min;
262         }
263         return (rval);
264     }
265
266     /**
267      * Returns the time in milliseconds of the slowest sample.
268      * @return the time in milliseconds of the slowest sample.
269      */

270     public long getMax()
271     {
272         long rval = 0;
273
274         if (max != Long.MIN_VALUE)
275         {
276             rval = max;
277         }
278         return (rval);
279     }
280
281     /**
282      * Returns the average time in milliseconds that samples ran in.
283      * @return the average time in milliseconds that samples ran in.
284      */

285     public long getAverage()
286     {
287         if (counter == 0)
288         {
289             return (0);
290         }
291         return (runningSum / counter);
292     }
293
294     /**
295      * Returns the number of samples that have been recorded by this instance
296      * of the RunningSample class.
297      * @return the number of samples that have been recorded by this instance
298      * of the RunningSample class.
299      */

300     public long getNumSamples()
301     {
302         return (counter);
303     }
304
305     /**
306      * Returns the raw double value of the percentage of samples with errors
307      * that were recorded. (Between 0.0 and 1.0)
308      * If you want a nicer return format, see
309      * {@link #getErrorPercentageString()}.
310      *
311      * @return the raw double value of the percentage of samples with errors
312      * that were recorded.
313      */

314     public double getErrorPercentage()
315     {
316         double rval = 0.0;
317
318         if (counter == 0)
319         {
320             return (rval);
321         }
322         rval = (double) errorCount / (double) counter;
323         return (rval);
324     }
325
326     /**
327      * Returns a String which represents the percentage of sample errors that
328      * have occurred. ("0.00%" through "100.00%")
329      *
330      * @return a String which represents the percentage of sample errors that
331      * have occurred.
332      */

333     public String JavaDoc getErrorPercentageString()
334     {
335         double myErrorPercentage = this.getErrorPercentage();
336
337         return (errorFormatter.format(myErrorPercentage));
338     }
339
340     /**
341      * For debugging purposes, mainly.
342      */

343     public String JavaDoc toString()
344     {
345         StringBuffer JavaDoc mySB = new StringBuffer JavaDoc();
346
347         mySB.append("Samples: " + this.getNumSamples() + " ");
348         mySB.append("Avg: " + this.getAverage() + " ");
349         mySB.append("Min: " + this.getMin() + " ");
350         mySB.append("Max: " + this.getMax() + " ");
351         mySB.append("Error Rate: " + this.getErrorPercentageString() + " ");
352         mySB.append("Sample Rate: " + this.getRateString());
353         return (mySB.toString());
354     }
355
356     /**
357      * @return errorCount
358      */

359     public long getErrorCount() {
360         return errorCount;
361     }
362
363 } // class RunningSample
364
Popular Tags