00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 package org.objectweb.cjdbc.common.util;
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 public class Stats
00044 {
00045
00046 private int count;
00047
00048
00049 private int error;
00050
00051
00052 private int cacheHit;
00053
00054
00055 private long minTime;
00056
00057
00058 private long maxTime;
00059
00060
00061 private long totalTime;
00062
00063
00064 private String name;
00065
00066
00067
00068
00069
00070
00071 public Stats(String statName)
00072 {
00073 name = statName;
00074 reset();
00075 }
00076
00077
00078
00079
00080 public synchronized void reset()
00081 {
00082 count = 0;
00083 error = 0;
00084 minTime = Long.MAX_VALUE;
00085 maxTime = Long.MIN_VALUE;
00086 totalTime = 0;
00087 }
00088
00089
00090
00091
00092 public synchronized void incrementCount()
00093 {
00094 count++;
00095 }
00096
00097
00098
00099
00100 public synchronized void incrementError()
00101 {
00102 error++;
00103 }
00104
00105
00106
00107
00108 public synchronized void incrementCacheHit()
00109 {
00110 cacheHit++;
00111 }
00112
00113
00114
00115
00116
00117
00118
00119 public synchronized void updateTime(long time)
00120 {
00121 if (time < 0)
00122 {
00123 System.err.println("Negative time received in Stats.updateTime(" + time
00124 + ")\n");
00125 return;
00126 }
00127 totalTime += time;
00128 if (time > maxTime)
00129 maxTime = time;
00130 if (time < minTime)
00131 minTime = time;
00132 }
00133
00134
00135
00136
00137
00138
00139 public String getName()
00140 {
00141 return name;
00142 }
00143
00144
00145
00146
00147
00148
00149 public synchronized int getCount()
00150 {
00151 return count;
00152 }
00153
00154
00155
00156
00157
00158
00159 public synchronized int getError()
00160 {
00161 return error;
00162 }
00163
00164
00165
00166
00167
00168
00169 public synchronized int getCacheHit()
00170 {
00171 return cacheHit;
00172 }
00173
00174
00175
00176
00177
00178
00179 public synchronized long getMinTime()
00180 {
00181 return minTime;
00182 }
00183
00184
00185
00186
00187
00188
00189 public synchronized long getMaxTime()
00190 {
00191 return maxTime;
00192 }
00193
00194
00195
00196
00197
00198
00199 public synchronized long getTotalTime()
00200 {
00201 return totalTime;
00202 }
00203
00204
00205
00206
00207
00208
00209
00210 public synchronized void merge(Stats anotherStat) throws Exception
00211 {
00212 if (this == anotherStat)
00213 {
00214 throw new Exception("You cannot merge a stat with itself");
00215 }
00216
00217 count += anotherStat.getCount();
00218 error += anotherStat.getError();
00219 cacheHit += anotherStat.getCacheHit();
00220 if (minTime > anotherStat.getMinTime())
00221 minTime = anotherStat.getMinTime();
00222 if (maxTime < anotherStat.getMaxTime())
00223 maxTime = anotherStat.getMaxTime();
00224 totalTime += anotherStat.getTotalTime();
00225 }
00226
00227
00228
00229
00230 public void displayOnStdout()
00231 {
00232 System.out.println(multipleLineDisplay());
00233 }
00234
00235
00236
00237
00238
00239
00240 public String multipleLineDisplay()
00241 {
00242 String output = name + " statistics:\n" + " Count: " + count + "\n"
00243 + " Error: " + error + "\n";
00244 if (totalTime != 0)
00245 {
00246 output += " Min time: " + minTime + " ms\n";
00247 output += " Max time: " + maxTime + " ms\n";
00248 }
00249 else
00250 {
00251 output += " Min time: 0 ms\n";
00252 output += " Max time: 0 ms\n";
00253 }
00254 if (count == 0)
00255 output += " Avg time: 0 ms\n";
00256 else
00257 output += " Avg time: " + totalTime / count + " ms\n";
00258 output += " Tot time: " + totalTime + " ms\n";
00259
00260 double timeSec = totalTime / 1000;
00261 double timeMin = timeSec / 60, throup;
00262 throup = (timeMin != 0) ? (count / timeMin) : (count / timeSec);
00263 output += " Throughput: " + throup
00264 + ((timeMin != 0) ? " requests/minute" : " requests/second");
00265 return output;
00266 }
00267
00268
00269
00270
00271
00272
00273
00274 public String singleLineDisplay()
00275 {
00276 String output = name + " " + count + " " + error + " " + cacheHit + " ";
00277 if (count == 0)
00278 output += "0 ";
00279 else
00280 output += ((double) cacheHit / (double) count * 100.0) + " ";
00281 if (totalTime != 0)
00282 output += minTime + " " + maxTime + " ";
00283 else
00284 output += " 0 0 ";
00285 if (count == 0)
00286 output += "0 ";
00287 else
00288 output += totalTime / count + " ";
00289 output += totalTime;
00290 double timeSec = totalTime / 1000;
00291 double timeMin = timeSec / 60, throup;
00292 throup = (timeMin != 0) ? (count / timeMin) : (count / timeSec);
00293 output += throup
00294 + ((timeMin != 0) ? " requests/minute" : " requests/second");
00295 return output;
00296 }
00297
00298
00299
00300
00301
00302
00303
00304 public String[] toStringTable()
00305 {
00306 String[] foo = {
00307 name,
00308 Integer.toString(count),
00309 Integer.toString(error),
00310 Integer.toString(cacheHit),
00311 (count == 0) ? "0" : Float.toString((float) cacheHit / (float) count
00312 * (float) 100.0), Long.toString(minTime), Long.toString(maxTime),
00313 (count == 0) ? "0" : Float.toString((float) totalTime / (float) count),
00314 Long.toString(totalTime)};
00315 return foo;
00316 }
00317 }