1 24 package edu.rice.rubis.client; 25 26 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; private long sessionsTime; 51 52 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 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 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 109 public synchronized void addSession() 110 { 111 nbSessions++; 112 } 113 114 115 120 public synchronized void incrementCount(int index) 121 { 122 count[index]++; 123 } 124 125 126 131 public synchronized void incrementError(int index) 132 { 133 error[index]++; 134 } 135 136 137 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 166 public synchronized int getCount(int index) 167 { 168 return count[index]; 169 } 170 171 172 179 public synchronized int getError(int index) 180 { 181 return error[index]; 182 } 183 184 185 192 public synchronized long getMinTime(int index) 193 { 194 return minTime[index]; 195 } 196 197 198 205 public synchronized long getMaxTime(int index) 206 { 207 return maxTime[index]; 208 } 209 210 211 218 public synchronized long getTotalTime(int index) 219 { 220 return totalTime[index]; 221 } 222 223 224 229 public int getNbOfStats() 230 { 231 return nbOfStats; 232 } 233 234 235 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 275 public void display_stats(String 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 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 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 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 |