1 package edu.rice.rubbos.client; 2 3 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; private long sessionsTime; 28 29 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 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 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 86 public synchronized void addSession() 87 { 88 nbSessions++; 89 } 90 91 92 97 public synchronized void incrementCount(int index) 98 { 99 count[index]++; 100 } 101 102 103 108 public synchronized void incrementError(int index) 109 { 110 error[index]++; 111 } 112 113 114 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 143 public synchronized int getCount(int index) 144 { 145 return count[index]; 146 } 147 148 149 156 public synchronized int getError(int index) 157 { 158 return error[index]; 159 } 160 161 162 169 public synchronized long getMinTime(int index) 170 { 171 return minTime[index]; 172 } 173 174 175 182 public synchronized long getMaxTime(int index) 183 { 184 return maxTime[index]; 185 } 186 187 188 195 public synchronized long getTotalTime(int index) 196 { 197 return totalTime[index]; 198 } 199 200 201 206 public int getNbOfStats() 207 { 208 return nbOfStats; 209 } 210 211 212 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 252 public void display_stats(String 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 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 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 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 |