KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > common > util > Stats


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: sequoia@continuent.org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * Initial developer(s): Emmanuel Cecchet.
20  * Contributor(s): Julie Marguerite, Sara Bouchenak.
21  */

22
23 package org.continuent.sequoia.common.util;
24
25 /**
26  * This class provides thread-safe statistics. Each statistic entry is composed
27  * as follow:
28  * <ul>
29  * <li>count: statistic counter</li>
30  * <li>error: statistic error counter</li>
31  * <li>minTime: minimum time for this entry (automatically computed)</li>
32  * <li>maxTime: maximum time for this entry (automatically computed)</li>
33  * <li>totalTime: total time for this entry</li>
34  * </ul>
35  *
36  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
37  * @author <a HREF="mailto:julie.marguerite@inria.fr">Julie Marguerite </a>
38  * @version 1.0
39  */

40
41 public class Stats
42 {
43   /** Statistic counter */
44   private int count;
45
46   /** Statistic error counter */
47   private int error;
48
49   /** Cache hits counter */
50   private int cacheHit;
51
52   /** Minimum time for this entry (automatically computed) */
53   private long minTime;
54
55   /** Maximum time for this entry (automatically computed) */
56   private long maxTime;
57
58   /** Total time for this entry */
59   private long totalTime;
60
61   /** Name of the stats. */
62   private String JavaDoc name;
63
64   /**
65    * Creates a new <code>Stats</code> instance. The entries are reset to 0.
66    *
67    * @param statName The stat name
68    */

69   public Stats(String JavaDoc statName)
70   {
71     name = statName;
72     reset();
73   }
74
75   /**
76    * Resets all entries to 0.
77    */

78   public synchronized void reset()
79   {
80     count = 0;
81     error = 0;
82     minTime = Long.MAX_VALUE;
83     maxTime = Long.MIN_VALUE;
84     totalTime = 0;
85   }
86
87   /**
88    * Increments an entry count by one.
89    */

90   public synchronized void incrementCount()
91   {
92     count++;
93   }
94
95   /**
96    * Increments an entry error by one.
97    */

98   public synchronized void incrementError()
99   {
100     error++;
101   }
102
103   /**
104    * Increments an entry cache hit by one.
105    */

106   public synchronized void incrementCacheHit()
107   {
108     cacheHit++;
109   }
110
111   /**
112    * Adds a new time sample for this entry. <code>time</code> is added to
113    * total time and both minTime and maxTime are updated if needed.
114    *
115    * @param time time to add to this entry
116    */

117   public synchronized void updateTime(long time)
118   {
119     if (time < 0)
120     {
121       System.err.println("Negative time received in Stats.updateTime(" + time
122           + ")\n");
123       return;
124     }
125     totalTime += time;
126     if (time > maxTime)
127       maxTime = time;
128     if (time < minTime)
129       minTime = time;
130   }
131
132   /**
133    * Gets the name of the current stat.
134    *
135    * @return stat name
136    */

137   public String JavaDoc getName()
138   {
139     return name;
140   }
141
142   /**
143    * Gets current count of an entry.
144    *
145    * @return current entry count value
146    */

147   public synchronized int getCount()
148   {
149     return count;
150   }
151
152   /**
153    * Gets current error count of an entry
154    *
155    * @return current entry error value
156    */

157   public synchronized int getError()
158   {
159     return error;
160   }
161
162   /**
163    * Gets current cache hit count of an entry
164    *
165    * @return current entry cache hit value
166    */

167   public synchronized int getCacheHit()
168   {
169     return cacheHit;
170   }
171
172   /**
173    * Gets the minimum time of an entry
174    *
175    * @return entry minimum time
176    */

177   public synchronized long getMinTime()
178   {
179     return minTime;
180   }
181
182   /**
183    * Gets the maximum time of an entry
184    *
185    * @return entry maximum time
186    */

187   public synchronized long getMaxTime()
188   {
189     return maxTime;
190   }
191
192   /**
193    * Gets the total time of an entry
194    *
195    * @return entry total time
196    */

197   public synchronized long getTotalTime()
198   {
199     return totalTime;
200   }
201
202   /**
203    * Adds the entries of another <code>Stats</code> object to this one.
204    *
205    * @param anotherStat stat to merge with current stat
206    * @throws Exception if you try to merge a stat with itself
207    */

208   public synchronized void merge(Stats anotherStat) throws Exception JavaDoc
209   {
210     if (this == anotherStat)
211     {
212       throw new Exception JavaDoc("You cannot merge a stat with itself");
213     }
214
215     count += anotherStat.getCount();
216     error += anotherStat.getError();
217     cacheHit += anotherStat.getCacheHit();
218     if (minTime > anotherStat.getMinTime())
219       minTime = anotherStat.getMinTime();
220     if (maxTime < anotherStat.getMaxTime())
221       maxTime = anotherStat.getMaxTime();
222     totalTime += anotherStat.getTotalTime();
223   }
224
225   /**
226    * Displays the statistics on the standard output.
227    */

228   public void displayOnStdout()
229   {
230     System.out.println(multipleLineDisplay());
231   }
232
233   /**
234    * Displays the statistics information on multiple lines.
235    *
236    * @return a <code>String</code> containing the Stat output
237    */

238   public String JavaDoc multipleLineDisplay()
239   {
240     String JavaDoc output = name + " statistics:\n" + " Count: " + count + "\n"
241         + " Error: " + error + "\n";
242     if (totalTime != 0)
243     {
244       output += " Min time: " + minTime + " ms\n";
245       output += " Max time: " + maxTime + " ms\n";
246     }
247     else
248     {
249       output += " Min time: 0 ms\n";
250       output += " Max time: 0 ms\n";
251     }
252     if (count == 0)
253       output += " Avg time: 0 ms\n";
254     else
255       output += " Avg time: " + totalTime / count + " ms\n";
256     output += " Tot time: " + totalTime + " ms\n";
257
258     double timeSec = totalTime / 1000;
259     double timeMin = timeSec / 60, throup;
260     throup = (timeMin != 0) ? (count / timeMin) : (count / timeSec);
261     output += " Throughput: " + throup
262         + ((timeMin != 0) ? " requests/minute" : " requests/second");
263     return output;
264   }
265
266   /**
267    * Displays the statistics information on a single line in the format: name
268    * count error cacheHit %hit minTime maxTime avgTime totalTime
269    *
270    * @return a <code>String</code> containing the Stat output
271    */

272   public String JavaDoc singleLineDisplay()
273   {
274     String JavaDoc output = name + " " + count + " " + error + " " + cacheHit + " ";
275     if (count == 0)
276       output += "0 ";
277     else
278       output += ((double) cacheHit / (double) count * 100.0) + " ";
279     if (totalTime != 0)
280       output += minTime + " " + maxTime + " ";
281     else
282       output += " 0 0 ";
283     if (count == 0)
284       output += "0 ";
285     else
286       output += totalTime / count + " ";
287     output += totalTime;
288     double timeSec = totalTime / 1000;
289     double timeMin = timeSec / 60, throup;
290     throup = (timeMin != 0) ? (count / timeMin) : (count / timeSec);
291     output += throup
292         + ((timeMin != 0) ? " requests/minute" : " requests/second");
293     return output;
294   }
295
296   /**
297    * Get the stat information in the form of a String table. Format is: name
298    * count error cacheHit %hit minTime maxTime avgTime totalTime
299    *
300    * @return the String table corresponding to this stat
301    */

302   public String JavaDoc[] toStringTable()
303   {
304     String JavaDoc[] foo = {
305         name,
306         Integer.toString(count),
307         Integer.toString(error),
308         Integer.toString(cacheHit),
309         (count == 0) ? "0" : Float.toString((float) cacheHit / (float) count
310             * (float) 100.0), Long.toString(minTime), Long.toString(maxTime),
311         (count == 0) ? "0" : Float.toString((float) totalTime / (float) count),
312         Long.toString(totalTime)};
313     return foo;
314   }
315 }
Popular Tags