KickJava   Java API By Example, From Geeks To Geeks.

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


1 package edu.rice.rubbos.client;
2  
3 /**
4  * This class provides thread-safe statistics. Each statistic entry is composed as follow:
5  * <pre>
6  * count : statistic counter
7  * error : statistic error counter
8  * minTime : minimum time for this entry (automatically computed)
9  * maxTime : maximum time for this entry (automatically computed)
10  * totalTime : total time for this entry
11  * </pre>
12  *
13  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
14  * @version 1.0
15  */

16
17 public class Stats
18 {
19   private int nbOfStats;
20   private int count[];
21   private int error[];
22   private long minTime[];
23   private long maxTime[];
24   private long totalTime[];
25   private int nbSessions; // Number of sessions succesfully ended
26
private long sessionsTime; // Sessions total duration
27

28
29   /**
30    * Creates a new <code>Stats</code> instance.
31    * The entries are reset to 0.
32    *
33    * @param NbOfStats number of entries to create
34    */

35   public Stats(int NbOfStats)
36   {
37     nbOfStats = NbOfStats;
38     count = new int[nbOfStats];
39     error = new int[nbOfStats];
40     minTime = new long[nbOfStats];
41     maxTime = new long[nbOfStats];
42     totalTime = new long[nbOfStats];
43     reset();
44   }
45
46
47   /**
48    * Resets all entries to 0
49    */

50   public synchronized void reset()
51   {
52     int i;
53
54     for (i = 0 ; i < nbOfStats ; i++)
55     {
56       count[i] = 0;
57       error[i] = 0;
58       minTime[i] = Long.MAX_VALUE;
59       maxTime[i] = 0;
60       totalTime[i] = 0;
61     }
62     nbSessions = 0;
63     sessionsTime = 0;
64   }
65
66   /**
67    * Add a session duration to the total sessions duration and
68    * increase the number of succesfully ended sessions.
69    *
70    * @param time duration of the session
71    */

72   public synchronized void addSessionTime(long time)
73   {
74     nbSessions++;
75     if (time < 0)
76     {
77       System.err.println("Negative time received in Stats.addSessionTime("+time+")<br>\n");
78       return ;
79     }
80     sessionsTime = sessionsTime + time;
81   }
82
83  /**
84    * Increment the number of succesfully ended sessions.
85    */

86   public synchronized void addSession()
87   {
88     nbSessions++;
89   }
90
91
92   /**
93    * Increment an entry count by one.
94    *
95    * @param index index of the entry
96    */

97   public synchronized void incrementCount(int index)
98   {
99     count[index]++;
100   }
101
102
103   /**
104    * Increment an entry error by one.
105    *
106    * @param index index of the entry
107    */

108   public synchronized void incrementError(int index)
109   {
110     error[index]++;
111   }
112
113
114   /**
115    * Add a new time sample for this entry. <code>time</code> is added to total time
116    * and both minTime and maxTime are updated if needed.
117    *
118    * @param index index of the entry
119    * @param time time to add to this entry
120    */

121   public synchronized void updateTime(int index, long time)
122   {
123     if (time < 0)
124     {
125       System.err.println("Negative time received in Stats.updateTime("+time+")<br>\n");
126       return ;
127     }
128     totalTime[index] += time;
129     if (time > maxTime[index])
130       maxTime[index] = time;
131     if (time < minTime[index])
132       minTime[index] = time;
133   }
134
135
136   /**
137    * Get current count of an entry
138    *
139    * @param index index of the entry
140    *
141    * @return entry count value
142    */

143   public synchronized int getCount(int index)
144   {
145     return count[index];
146   }
147
148
149   /**
150    * Get current error count of an entry
151    *
152    * @param index index of the entry
153    *
154    * @return entry error value
155    */

156   public synchronized int getError(int index)
157   {
158     return error[index];
159   }
160
161
162   /**
163    * Get the minimum time of an entry
164    *
165    * @param index index of the entry
166    *
167    * @return entry minimum time
168    */

169   public synchronized long getMinTime(int index)
170   {
171     return minTime[index];
172   }
173
174
175   /**
176    * Get the maximum time of an entry
177    *
178    * @param index index of the entry
179    *
180    * @return entry maximum time
181    */

182   public synchronized long getMaxTime(int index)
183   {
184     return maxTime[index];
185   }
186
187
188   /**
189    * Get the total time of an entry
190    *
191    * @param index index of the entry
192    *
193    * @return entry total time
194    */

195   public synchronized long getTotalTime(int index)
196   {
197     return totalTime[index];
198   }
199
200
201   /**
202    * Get the total number of entries that are collected
203    *
204    * @return total number of entries
205    */

206   public int getNbOfStats()
207   {
208     return nbOfStats;
209   }
210
211
212   /**
213    * Adds the entries of another Stats object to this one.
214    *
215    * @param anotherStat stat to merge with current stat
216    */

217   public synchronized void merge(Stats anotherStat)
218   {
219     if (this == anotherStat)
220     {
221       System.out.println("You cannot merge a stats with itself");
222       return;
223     }
224     if (nbOfStats != anotherStat.getNbOfStats())
225     {
226       System.out.println("Cannot merge stats of differents sizes.");
227       return;
228     }
229     for (int i = 0 ; i < nbOfStats ; i++)
230     {
231       count[i] += anotherStat.getCount(i);
232       error[i] += anotherStat.getError(i);
233       if (minTime[i] > anotherStat.getMinTime(i))
234         minTime[i] = anotherStat.getMinTime(i);
235       if (maxTime[i] < anotherStat.getMaxTime(i))
236         maxTime[i] = anotherStat.getMaxTime(i);
237       totalTime[i] += anotherStat.getTotalTime(i);
238     }
239     nbSessions += anotherStat.nbSessions;
240     sessionsTime += anotherStat.sessionsTime;
241   }
242
243
244   /**
245    * Display an HTML table containing the stats for each state.
246    * Also compute the totals and average throughput
247    *
248    * @param title table title
249    * @param sessionTime total time for this session
250    * @param exclude0Stat true if you want to exclude the stat with a 0 value from the output
251    */

252   public void display_stats(String JavaDoc title, long sessionTime, boolean exclude0Stat)
253   {
254     int counts = 0;
255     int errors = 0;
256     long time = 0;
257
258     System.out.println("<br><h3>"+title+" statistics</h3><p>");
259     System.out.println("<TABLE BORDER=1>");
260     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>");
261     // Display stat for each state
262
for (int i = 0 ; i < getNbOfStats() ; i++)
263     {
264       counts += count[i];
265       errors += error[i];
266       time += totalTime[i];
267     }
268
269     for (int i = 0 ; i < getNbOfStats() ; i++)
270     {
271       if ((exclude0Stat && count[i] != 0) || (!exclude0Stat))
272       {
273         System.out.print("<TR><TD><div align=left>"+TransitionTable.getStateName(i)+"</div><TD><div align=right>");
274         if ((counts > 0) && (count[i] > 0))
275           System.out.print(100*count[i]/counts+" %");
276         else
277           System.out.print("0 %");
278         System.out.print("</div><TD><div align=right>"+count[i]+"</div><TD><div align=right>");
279         if (error[i] > 0)
280           System.out.print("<B>"+error[i]+"</B>");
281         else
282           System.out.print(error[i]);
283         System.out.print("</div><TD><div align=right>");
284         if (minTime[i] != Long.MAX_VALUE)
285           System.out.print(minTime[i]);
286         else
287           System.out.print("0");
288         System.out.print(" ms</div><TD><div align=right>"+maxTime[i]+" ms</div><TD><div align=right>");
289         int success = count[i] - error[i];
290         if (success > 0)
291           System.out.println(totalTime[i]/success+" ms</div>");
292         else
293            System.out.println("0 ms</div>");
294       }
295     }
296
297     // Display total
298
if (counts > 0)
299     {
300       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+
301                        "</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>");
302       counts += errors;
303       System.out.println(time/counts+" ms</B></div>");
304       // Display stats about sessions
305
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>");
306       System.out.println("<TR><TD><div align=left>Completed sessions</div><TD colspan=6><div align=left>"+nbSessions+"</div>");
307       System.out.println("<TR><TD><div align=left>Total time</div><TD colspan=6><div align=left>"+sessionsTime/1000L+" seconds</div>");
308       System.out.print("<TR><TD><div align=left><B>Average session time</div></B><TD colspan=6><div align=left><B>");
309       if (nbSessions > 0)
310         System.out.print(sessionsTime/(long)nbSessions/1000L+" seconds");
311       else
312         System.out.print("0 second");
313       System.out.println("</B></div>");
314     }
315     System.out.println("</TABLE><p>");
316   }
317
318
319 }
320
Popular Tags