1 24 25 package org.objectweb.cjdbc.common.util; 26 27 42 43 public class Stats 44 { 45 46 private int count; 47 48 49 private int error; 50 51 52 private int cacheHit; 53 54 55 private long minTime; 56 57 58 private long maxTime; 59 60 61 private long totalTime; 62 63 64 private String name; 65 66 71 public Stats(String statName) 72 { 73 name = statName; 74 reset(); 75 } 76 77 80 public synchronized void reset() 81 { 82 count = 0; 83 error = 0; 84 minTime = Long.MAX_VALUE; 85 maxTime = Long.MIN_VALUE; 86 totalTime = 0; 87 } 88 89 92 public synchronized void incrementCount() 93 { 94 count++; 95 } 96 97 100 public synchronized void incrementError() 101 { 102 error++; 103 } 104 105 108 public synchronized void incrementCacheHit() 109 { 110 cacheHit++; 111 } 112 113 119 public synchronized void updateTime(long time) 120 { 121 if (time < 0) 122 { 123 System.err.println("Negative time received in Stats.updateTime(" + time 124 + ")\n"); 125 return; 126 } 127 totalTime += time; 128 if (time > maxTime) 129 maxTime = time; 130 if (time < minTime) 131 minTime = time; 132 } 133 134 139 public String getName() 140 { 141 return name; 142 } 143 144 149 public synchronized int getCount() 150 { 151 return count; 152 } 153 154 159 public synchronized int getError() 160 { 161 return error; 162 } 163 164 169 public synchronized int getCacheHit() 170 { 171 return cacheHit; 172 } 173 174 179 public synchronized long getMinTime() 180 { 181 return minTime; 182 } 183 184 189 public synchronized long getMaxTime() 190 { 191 return maxTime; 192 } 193 194 199 public synchronized long getTotalTime() 200 { 201 return totalTime; 202 } 203 204 210 public synchronized void merge(Stats anotherStat) throws Exception 211 { 212 if (this == anotherStat) 213 { 214 throw new Exception ("You cannot merge a stat with itself"); 215 } 216 217 count += anotherStat.getCount(); 218 error += anotherStat.getError(); 219 cacheHit += anotherStat.getCacheHit(); 220 if (minTime > anotherStat.getMinTime()) 221 minTime = anotherStat.getMinTime(); 222 if (maxTime < anotherStat.getMaxTime()) 223 maxTime = anotherStat.getMaxTime(); 224 totalTime += anotherStat.getTotalTime(); 225 } 226 227 230 public void displayOnStdout() 231 { 232 System.out.println(multipleLineDisplay()); 233 } 234 235 240 public String multipleLineDisplay() 241 { 242 String output = name + " statistics:\n" + " Count: " + count + "\n" 243 + " Error: " + error + "\n"; 244 if (totalTime != 0) 245 { 246 output += " Min time: " + minTime + " ms\n"; 247 output += " Max time: " + maxTime + " ms\n"; 248 } 249 else 250 { 251 output += " Min time: 0 ms\n"; 252 output += " Max time: 0 ms\n"; 253 } 254 if (count == 0) 255 output += " Avg time: 0 ms\n"; 256 else 257 output += " Avg time: " + totalTime / count + " ms\n"; 258 output += " Tot time: " + totalTime + " ms\n"; 259 260 double timeSec = totalTime / 1000; 261 double timeMin = timeSec / 60, throup; 262 throup = (timeMin != 0) ? (count / timeMin) : (count / timeSec); 263 output += " Throughput: " + throup 264 + ((timeMin != 0) ? " requests/minute" : " requests/second"); 265 return output; 266 } 267 268 274 public String singleLineDisplay() 275 { 276 String output = name + " " + count + " " + error + " " + cacheHit + " "; 277 if (count == 0) 278 output += "0 "; 279 else 280 output += ((double) cacheHit / (double) count * 100.0) + " "; 281 if (totalTime != 0) 282 output += minTime + " " + maxTime + " "; 283 else 284 output += " 0 0 "; 285 if (count == 0) 286 output += "0 "; 287 else 288 output += totalTime / count + " "; 289 output += totalTime; 290 double timeSec = totalTime / 1000; 291 double timeMin = timeSec / 60, throup; 292 throup = (timeMin != 0) ? (count / timeMin) : (count / timeSec); 293 output += throup 294 + ((timeMin != 0) ? " requests/minute" : " requests/second"); 295 return output; 296 } 297 298 304 public String [] toStringTable() 305 { 306 String [] foo = { 307 name, 308 Integer.toString(count), 309 Integer.toString(error), 310 Integer.toString(cacheHit), 311 (count == 0) ? "0" : Float.toString((float) cacheHit / (float) count 312 * (float) 100.0), Long.toString(minTime), Long.toString(maxTime), 313 (count == 0) ? "0" : Float.toString((float) totalTime / (float) count), 314 Long.toString(totalTime)}; 315 return foo; 316 } 317 } | Popular Tags |