KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > common > util > Stats


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): Julie Marguerite, Sara Bouchenak.
23  */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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