1 22 23 package org.continuent.sequoia.common.util; 24 25 40 41 public class Stats 42 { 43 44 private int count; 45 46 47 private int error; 48 49 50 private int cacheHit; 51 52 53 private long minTime; 54 55 56 private long maxTime; 57 58 59 private long totalTime; 60 61 62 private String name; 63 64 69 public Stats(String statName) 70 { 71 name = statName; 72 reset(); 73 } 74 75 78 public synchronized void reset() 79 { 80 count = 0; 81 error = 0; 82 minTime = Long.MAX_VALUE; 83 maxTime = Long.MIN_VALUE; 84 totalTime = 0; 85 } 86 87 90 public synchronized void incrementCount() 91 { 92 count++; 93 } 94 95 98 public synchronized void incrementError() 99 { 100 error++; 101 } 102 103 106 public synchronized void incrementCacheHit() 107 { 108 cacheHit++; 109 } 110 111 117 public synchronized void updateTime(long time) 118 { 119 if (time < 0) 120 { 121 System.err.println("Negative time received in Stats.updateTime(" + time 122 + ")\n"); 123 return; 124 } 125 totalTime += time; 126 if (time > maxTime) 127 maxTime = time; 128 if (time < minTime) 129 minTime = time; 130 } 131 132 137 public String getName() 138 { 139 return name; 140 } 141 142 147 public synchronized int getCount() 148 { 149 return count; 150 } 151 152 157 public synchronized int getError() 158 { 159 return error; 160 } 161 162 167 public synchronized int getCacheHit() 168 { 169 return cacheHit; 170 } 171 172 177 public synchronized long getMinTime() 178 { 179 return minTime; 180 } 181 182 187 public synchronized long getMaxTime() 188 { 189 return maxTime; 190 } 191 192 197 public synchronized long getTotalTime() 198 { 199 return totalTime; 200 } 201 202 208 public synchronized void merge(Stats anotherStat) throws Exception 209 { 210 if (this == anotherStat) 211 { 212 throw new Exception ("You cannot merge a stat with itself"); 213 } 214 215 count += anotherStat.getCount(); 216 error += anotherStat.getError(); 217 cacheHit += anotherStat.getCacheHit(); 218 if (minTime > anotherStat.getMinTime()) 219 minTime = anotherStat.getMinTime(); 220 if (maxTime < anotherStat.getMaxTime()) 221 maxTime = anotherStat.getMaxTime(); 222 totalTime += anotherStat.getTotalTime(); 223 } 224 225 228 public void displayOnStdout() 229 { 230 System.out.println(multipleLineDisplay()); 231 } 232 233 238 public String multipleLineDisplay() 239 { 240 String output = name + " statistics:\n" + " Count: " + count + "\n" 241 + " Error: " + error + "\n"; 242 if (totalTime != 0) 243 { 244 output += " Min time: " + minTime + " ms\n"; 245 output += " Max time: " + maxTime + " ms\n"; 246 } 247 else 248 { 249 output += " Min time: 0 ms\n"; 250 output += " Max time: 0 ms\n"; 251 } 252 if (count == 0) 253 output += " Avg time: 0 ms\n"; 254 else 255 output += " Avg time: " + totalTime / count + " ms\n"; 256 output += " Tot time: " + totalTime + " ms\n"; 257 258 double timeSec = totalTime / 1000; 259 double timeMin = timeSec / 60, throup; 260 throup = (timeMin != 0) ? (count / timeMin) : (count / timeSec); 261 output += " Throughput: " + throup 262 + ((timeMin != 0) ? " requests/minute" : " requests/second"); 263 return output; 264 } 265 266 272 public String singleLineDisplay() 273 { 274 String output = name + " " + count + " " + error + " " + cacheHit + " "; 275 if (count == 0) 276 output += "0 "; 277 else 278 output += ((double) cacheHit / (double) count * 100.0) + " "; 279 if (totalTime != 0) 280 output += minTime + " " + maxTime + " "; 281 else 282 output += " 0 0 "; 283 if (count == 0) 284 output += "0 "; 285 else 286 output += totalTime / count + " "; 287 output += totalTime; 288 double timeSec = totalTime / 1000; 289 double timeMin = timeSec / 60, throup; 290 throup = (timeMin != 0) ? (count / timeMin) : (count / timeSec); 291 output += throup 292 + ((timeMin != 0) ? " requests/minute" : " requests/second"); 293 return output; 294 } 295 296 302 public String [] toStringTable() 303 { 304 String [] foo = { 305 name, 306 Integer.toString(count), 307 Integer.toString(error), 308 Integer.toString(cacheHit), 309 (count == 0) ? "0" : Float.toString((float) cacheHit / (float) count 310 * (float) 100.0), Long.toString(minTime), Long.toString(maxTime), 311 (count == 0) ? "0" : Float.toString((float) totalTime / (float) count), 312 Long.toString(totalTime)}; 313 return foo; 314 } 315 } | Popular Tags |