Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

Stats.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2004 French National Institute For Research In Computer
00004  * Science And Control (INRIA).
00005  * Contact: c-jdbc@objectweb.org
00006  * 
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or any later
00010  * version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00015  * for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00020  *
00021  * Initial developer(s): Emmanuel Cecchet.
00022  * Contributor(s): Julie Marguerite, Sara Bouchenak.
00023  */
00024 
00025 package org.objectweb.cjdbc.common.util;
00026 
00027 /**
00028  * This class provides thread-safe statistics. Each statistic entry is composed
00029  * as follow:
00030  * <ul>
00031  * <li>count: statistic counter</li>
00032  * <li>error: statistic error counter</li>
00033  * <li>minTime: minimum time for this entry (automatically computed)</li>
00034  * <li>maxTime: maximum time for this entry (automatically computed)</li>
00035  * <li>totalTime: total time for this entry</li>
00036  * </ul>
00037  * 
00038  * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
00039  * @author <a href="mailto:julie.marguerite@inria.fr">Julie Marguerite </a>
00040  * @version 1.0
00041  */
00042 
00043 public class Stats
00044 {
00045   /** Statistic counter */
00046   private int    count;
00047 
00048   /** Statistic error counter */
00049   private int    error;
00050 
00051   /** Cache hits counter */
00052   private int    cacheHit;
00053 
00054   /** Minimum time for this entry (automatically computed) */
00055   private long   minTime;
00056 
00057   /** Maximum time for this entry (automatically computed) */
00058   private long   maxTime;
00059 
00060   /** Total time for this entry */
00061   private long   totalTime;
00062 
00063   /** Name of the stats. */
00064   private String name;
00065 
00066   /**
00067    * Creates a new <code>Stats</code> instance. The entries are reset to 0.
00068    * 
00069    * @param statName The stat name
00070    */
00071   public Stats(String statName)
00072   {
00073     name = statName;
00074     reset();
00075   }
00076 
00077   /**
00078    * Resets all entries to 0.
00079    */
00080   public synchronized void reset()
00081   {
00082     count = 0;
00083     error = 0;
00084     minTime = Long.MAX_VALUE;
00085     maxTime = Long.MIN_VALUE;
00086     totalTime = 0;
00087   }
00088 
00089   /**
00090    * Increments an entry count by one.
00091    */
00092   public synchronized void incrementCount()
00093   {
00094     count++;
00095   }
00096 
00097   /**
00098    * Increments an entry error by one.
00099    */
00100   public synchronized void incrementError()
00101   {
00102     error++;
00103   }
00104 
00105   /**
00106    * Increments an entry cache hit by one.
00107    */
00108   public synchronized void incrementCacheHit()
00109   {
00110     cacheHit++;
00111   }
00112 
00113   /**
00114    * Adds a new time sample for this entry. <code>time</code> is added to
00115    * total time and both minTime and maxTime are updated if needed.
00116    * 
00117    * @param time time to add to this entry
00118    */
00119   public synchronized void updateTime(long time)
00120   {
00121     if (time < 0)
00122     {
00123       System.err.println("Negative time received in Stats.updateTime(" + time
00124           + ")\n");
00125       return;
00126     }
00127     totalTime += time;
00128     if (time > maxTime)
00129       maxTime = time;
00130     if (time < minTime)
00131       minTime = time;
00132   }
00133 
00134   /**
00135    * Gets the name of the current stat.
00136    * 
00137    * @return stat name
00138    */
00139   public String getName()
00140   {
00141     return name;
00142   }
00143 
00144   /**
00145    * Gets current count of an entry.
00146    * 
00147    * @return current entry count value
00148    */
00149   public synchronized int getCount()
00150   {
00151     return count;
00152   }
00153 
00154   /**
00155    * Gets current error count of an entry
00156    * 
00157    * @return current entry error value
00158    */
00159   public synchronized int getError()
00160   {
00161     return error;
00162   }
00163 
00164   /**
00165    * Gets current cache hit count of an entry
00166    * 
00167    * @return current entry cache hit value
00168    */
00169   public synchronized int getCacheHit()
00170   {
00171     return cacheHit;
00172   }
00173 
00174   /**
00175    * Gets the minimum time of an entry
00176    * 
00177    * @return entry minimum time
00178    */
00179   public synchronized long getMinTime()
00180   {
00181     return minTime;
00182   }
00183 
00184   /**
00185    * Gets the maximum time of an entry
00186    * 
00187    * @return entry maximum time
00188    */
00189   public synchronized long getMaxTime()
00190   {
00191     return maxTime;
00192   }
00193 
00194   /**
00195    * Gets the total time of an entry
00196    * 
00197    * @return entry total time
00198    */
00199   public synchronized long getTotalTime()
00200   {
00201     return totalTime;
00202   }
00203 
00204   /**
00205    * Adds the entries of another <code>Stats</code> object to this one.
00206    * 
00207    * @param anotherStat stat to merge with current stat
00208    * @throws Exception if you try to merge a stat with itself
00209    */
00210   public synchronized void merge(Stats anotherStat) throws Exception
00211   {
00212     if (this == anotherStat)
00213     {
00214       throw new Exception("You cannot merge a stat with itself");
00215     }
00216 
00217     count += anotherStat.getCount();
00218     error += anotherStat.getError();
00219     cacheHit += anotherStat.getCacheHit();
00220     if (minTime > anotherStat.getMinTime())
00221       minTime = anotherStat.getMinTime();
00222     if (maxTime < anotherStat.getMaxTime())
00223       maxTime = anotherStat.getMaxTime();
00224     totalTime += anotherStat.getTotalTime();
00225   }
00226 
00227   /**
00228    * Displays the statistics on the standard output.
00229    */
00230   public void displayOnStdout()
00231   {
00232     System.out.println(multipleLineDisplay());
00233   }
00234 
00235   /**
00236    * Displays the statistics information on multiple lines.
00237    * 
00238    * @return a <code>String</code> containing the Stat output
00239    */
00240   public String multipleLineDisplay()
00241   {
00242     String output = name + " statistics:\n" + "  Count: " + count + "\n"
00243         + "  Error: " + error + "\n";
00244     if (totalTime != 0)
00245     {
00246       output += "  Min time: " + minTime + " ms\n";
00247       output += "  Max time: " + maxTime + " ms\n";
00248     }
00249     else
00250     {
00251       output += "  Min time: 0 ms\n";
00252       output += "  Max time: 0 ms\n";
00253     }
00254     if (count == 0)
00255       output += "  Avg time: 0 ms\n";
00256     else
00257       output += "  Avg time: " + totalTime / count + " ms\n";
00258     output += "  Tot time: " + totalTime + " ms\n";
00259 
00260     double timeSec = totalTime / 1000;
00261     double timeMin = timeSec / 60, throup;
00262     throup = (timeMin != 0) ? (count / timeMin) : (count / timeSec);
00263     output += "  Throughput: " + throup
00264         + ((timeMin != 0) ? " requests/minute" : " requests/second");
00265     return output;
00266   }
00267 
00268   /**
00269    * Displays the statistics information on a single line in the format: name
00270    * count error cacheHit %hit minTime maxTime avgTime totalTime
00271    * 
00272    * @return a <code>String</code> containing the Stat output
00273    */
00274   public String singleLineDisplay()
00275   {
00276     String output = name + " " + count + " " + error + " " + cacheHit + " ";
00277     if (count == 0)
00278       output += "0 ";
00279     else
00280       output += ((double) cacheHit / (double) count * 100.0) + " ";
00281     if (totalTime != 0)
00282       output += minTime + " " + maxTime + " ";
00283     else
00284       output += " 0 0 ";
00285     if (count == 0)
00286       output += "0 ";
00287     else
00288       output += totalTime / count + " ";
00289     output += totalTime;
00290     double timeSec = totalTime / 1000;
00291     double timeMin = timeSec / 60, throup;
00292     throup = (timeMin != 0) ? (count / timeMin) : (count / timeSec);
00293     output += throup
00294         + ((timeMin != 0) ? " requests/minute" : " requests/second");
00295     return output;
00296   }
00297 
00298   /**
00299    * Get the stat information in the form of a String table. Format is: name
00300    * count error cacheHit %hit minTime maxTime avgTime totalTime
00301    * 
00302    * @return the String table corresponding to this stat
00303    */
00304   public String[] toStringTable()
00305   {
00306     String[] foo = {
00307         name,
00308         Integer.toString(count),
00309         Integer.toString(error),
00310         Integer.toString(cacheHit),
00311         (count == 0) ? "0" : Float.toString((float) cacheHit / (float) count
00312             * (float) 100.0), Long.toString(minTime), Long.toString(maxTime),
00313         (count == 0) ? "0" : Float.toString((float) totalTime / (float) count),
00314         Long.toString(totalTime)};
00315     return foo;
00316   }
00317 }

Generated on Mon Apr 11 22:01:35 2005 for C-JDBC by  doxygen 1.3.9.1