KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > rubis > client > Stats


1 /*
2  * RUBiS
3  * Copyright (C) 2002, 2003, 2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: jmob@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, Julie Marguerite
22  * Contributor(s):
23  */

24  package edu.rice.rubis.client;
25  
26 /**
27  * This class provides thread-safe statistics. Each statistic entry is composed as follow:
28  * <pre>
29  * count : statistic counter
30  * error : statistic error counter
31  * minTime : minimum time for this entry (automatically computed)
32  * maxTime : maximum time for this entry (automatically computed)
33  * totalTime : total time for this entry
34  * </pre>
35  *
36  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
37  * @version 1.0
38  */

39
40 public class Stats
41 {
42   private int nbOfStats;
43   private int count[];
44   private int error[];
45   private long minTime[];
46   private long maxTime[];
47   private long totalTime[];
48   private int nbSessions; // Number of sessions succesfully ended
49
private long sessionsTime; // Sessions total duration
50

51
52   /**
53    * Creates a new <code>Stats</code> instance.
54    * The entries are reset to 0.
55    *
56    * @param NbOfStats number of entries to create
57    */

58   public Stats(int NbOfStats)
59   {
60     nbOfStats = NbOfStats;
61     count = new int[nbOfStats];
62     error = new int[nbOfStats];
63     minTime = new long[nbOfStats];
64     maxTime = new long[nbOfStats];
65     totalTime = new long[nbOfStats];
66     reset();
67   }
68
69
70   /**
71    * Resets all entries to 0
72    */

73   public synchronized void reset()
74   {
75     int i;
76
77     for (i = 0 ; i < nbOfStats ; i++)
78     {
79       count[i] = 0;
80       error[i] = 0;
81       minTime[i] = Long.MAX_VALUE;
82       maxTime[i] = 0;
83       totalTime[i] = 0;
84     }
85     nbSessions = 0;
86     sessionsTime = 0;
87   }
88
89   /**
90    * Add a session duration to the total sessions duration and
91    * increase the number of succesfully ended sessions.
92    *
93    * @param time duration of the session
94    */

95   public synchronized void addSessionTime(long time)
96   {
97     nbSessions++;
98     if (time < 0)
99     {
100       System.err.println("Negative time received in Stats.addSessionTime("+time+")<br>\n");
101       return ;
102     }
103     sessionsTime = sessionsTime + time;
104   }
105
106  /**
107    * Increment the number of succesfully ended sessions.
108    */

109   public synchronized void addSession()
110   {
111     nbSessions++;
112   }
113
114
115   /**
116    * Increment an entry count by one.
117    *
118    * @param index index of the entry
119    */

120   public synchronized void incrementCount(int index)
121   {
122     count[index]++;
123   }
124
125
126   /**
127    * Increment an entry error by one.
128    *
129    * @param index index of the entry
130    */

131   public synchronized void incrementError(int index)
132   {
133     error[index]++;
134   }
135
136
137   /**
138    * Add a new time sample for this entry. <code>time</code> is added to total time
139    * and both minTime and maxTime are updated if needed.
140    *
141    * @param index index of the entry
142    * @param time time to add to this entry
143    */

144   public synchronized void updateTime(int index, long time)
145   {
146     if (time < 0)
147     {
148       System.err.println("Negative time received in Stats.updateTime("+time+")<br>\n");
149       return ;
150     }
151     totalTime[index] += time;
152     if (time > maxTime[index])
153       maxTime[index] = time;
154     if (time < minTime[index])
155       minTime[index] = time;
156   }
157
158
159   /**
160    * Get current count of an entry
161    *
162    * @param index index of the entry
163    *
164    * @return entry count value
165    */

166   public synchronized int getCount(int index)
167   {
168     return count[index];
169   }
170
171
172   /**
173    * Get current error count of an entry
174    *
175    * @param index index of the entry
176    *
177    * @return entry error value
178    */

179   public synchronized int getError(int index)
180   {
181     return error[index];
182   }
183
184
185   /**
186    * Get the minimum time of an entry
187    *
188    * @param index index of the entry
189    *
190    * @return entry minimum time
191    */

192   public synchronized long getMinTime(int index)
193   {
194     return minTime[index];
195   }
196
197
198   /**
199    * Get the maximum time of an entry
200    *
201    * @param index index of the entry
202    *
203    * @return entry maximum time
204    */

205   public synchronized long getMaxTime(int index)
206   {
207     return maxTime[index];
208   }
209
210
211   /**
212    * Get the total time of an entry
213    *
214    * @param index index of the entry
215    *
216    * @return entry total time
217    */

218   public synchronized long getTotalTime(int index)
219   {
220     return totalTime[index];
221   }
222
223
224   /**
225    * Get the total number of entries that are collected
226    *
227    * @return total number of entries
228    */

229   public int getNbOfStats()
230   {
231     return nbOfStats;
232   }
233
234
235   /**
236    * Adds the entries of another Stats object to this one.
237    *
238    * @param anotherStat stat to merge with current stat
239    */

240   public synchronized void merge(Stats anotherStat)
241   {
242     if (this == anotherStat)
243     {
244       System.out.println("You cannot merge a stats with itself");
245       return;
246     }
247     if (nbOfStats != anotherStat.getNbOfStats())
248     {
249       System.out.println("Cannot merge stats of differents sizes.");
250       return;
251     }
252     for (int i = 0 ; i < nbOfStats ; i++)
253     {
254       count[i] += anotherStat.getCount(i);
255       error[i] += anotherStat.getError(i);
256       if (minTime[i] > anotherStat.getMinTime(i))
257         minTime[i] = anotherStat.getMinTime(i);
258       if (maxTime[i] < anotherStat.getMaxTime(i))
259         maxTime[i] = anotherStat.getMaxTime(i);
260       totalTime[i] += anotherStat.getTotalTime(i);
261     }
262     nbSessions += anotherStat.nbSessions;
263     sessionsTime += anotherStat.sessionsTime;
264   }
265
266
267   /**
268    * Display an HTML table containing the stats for each state.
269    * Also compute the totals and average throughput
270    *
271    * @param title table title
272    * @param sessionTime total time for this session
273    * @param exclude0Stat true if you want to exclude the stat with a 0 value from the output
274    */

275   public void display_stats(String JavaDoc title, long sessionTime, boolean exclude0Stat)
276   {
277     int counts = 0;
278     int errors = 0;
279     long time = 0;
280
281     System.out.println("<br><h3>"+title+" statistics</h3><p>");
282     System.out.println("<TABLE BORDER=1>");
283     System.out.println("<THEAD><TR><TH>State name<TH>% of total<TH>Count<TH>Errors<TH>Minimum Time<TH>Maximum Time<TH>Average Time<TBODY>");
284     // Display stat for each state
285
for (int i = 0 ; i < getNbOfStats() ; i++)
286     {
287       counts += count[i];
288       errors += error[i];
289       time += totalTime[i];
290     }
291
292     for (int i = 0 ; i < getNbOfStats() ; i++)
293     {
294       if ((exclude0Stat && count[i] != 0) || (!exclude0Stat))
295       {
296         System.out.print("<TR><TD><div align=left>"+TransitionTable.getStateName(i)+"</div><TD><div align=right>");
297         if ((counts > 0) && (count[i] > 0))
298           System.out.print(100*count[i]/counts+" %");
299         else
300           System.out.print("0 %");
301         System.out.print("</div><TD><div align=right>"+count[i]+"</div><TD><div align=right>");
302         if (error[i] > 0)
303           System.out.print("<B>"+error[i]+"</B>");
304         else
305           System.out.print(error[i]);
306         System.out.print("</div><TD><div align=right>");
307         if (minTime[i] != Long.MAX_VALUE)
308           System.out.print(minTime[i]);
309         else
310           System.out.print("0");
311         System.out.print(" ms</div><TD><div align=right>"+maxTime[i]+" ms</div><TD><div align=right>");
312         if (count[i] != 0)
313           System.out.println(totalTime[i]/count[i]+" ms</div>");
314         else
315            System.out.println("0 ms</div>");
316       }
317     }
318
319     // Display total
320
if (counts > 0)
321     {
322       System.out.print("<TR><TD><div align=left><B>Total</B></div><TD><div align=right><B>100 %</B></div><TD><div align=right><B>"+counts+
323                        "</B></div><TD><div align=right><B>"+errors+ "</B></div><TD><div align=center>-</div><TD><div align=center>-</div><TD><div align=right><B>");
324       counts += errors;
325       System.out.println(time/counts+" ms</B></div>");
326       // Display stats about sessions
327
System.out.println("<TR><TD><div align=left><B>Average throughput</div></B><TD colspan=6><div align=center><B>"+1000*counts/sessionTime+" req/s</B></div>");
328       System.out.println("<TR><TD><div align=left>Completed sessions</div><TD colspan=6><div align=left>"+nbSessions+"</div>");
329       System.out.println("<TR><TD><div align=left>Total time</div><TD colspan=6><div align=left>"+sessionsTime/1000L+" seconds</div>");
330       System.out.print("<TR><TD><div align=left><B>Average session time</div></B><TD colspan=6><div align=left><B>");
331       if (nbSessions > 0)
332         System.out.print(sessionsTime/(long)nbSessions/1000L+" seconds");
333       else
334         System.out.print("0 second");
335       System.out.println("</B></div>");
336     }
337     System.out.println("</TABLE><p>");
338   }
339
340
341 }
342
Popular Tags