| 1 | /* __copyright_begin__ |
| 2 | Copyright 2011 Dan Hagberg |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | __copyright_end__ */ |
| 16 | package net.digitaltsunami.tmeter; |
| 17 | |
| 18 | import java.util.concurrent.TimeUnit; |
| 19 | |
| 20 | /** |
| 21 | * Maintains basic statistics for a given task. The maintained statistics are |
| 22 | * basic so that it does not have to maintain a list of the elapsed values. |
| 23 | * <p> |
| 24 | * Currently tracked statistics are: |
| 25 | * <ul> |
| 26 | * <li>Count</li> |
| 27 | * <li>Min Elapsed Time</li> |
| 28 | * <li>Max Elapsed Time</li> |
| 29 | * <li>Total Elapsed Time</li> |
| 30 | * <li>Average Elapsed Time</li> |
| 31 | * </ul> |
| 32 | * |
| 33 | * @author dhagberg |
| 34 | * |
| 35 | */ |
| 36 | public class TimerBasicStatistics implements Comparable<TimerBasicStatistics> { |
| 37 | |
| 38 | private final String taskName; |
| 39 | private volatile int count; |
| 40 | private volatile long totalElapsedNanos; |
| 41 | private volatile long minElapsedNanos = Long.MAX_VALUE; |
| 42 | private volatile long maxElapsedNanos = Long.MIN_VALUE; |
| 43 | private volatile double meanElapsedNanos; |
| 44 | // Used to calculate variance and standard deviation |
| 45 | private volatile double sumOfDeltasElapsedNanos; |
| 46 | private TimeUnit reportingUnit = TimeUnit.MILLISECONDS; |
| 47 | |
| 48 | /** |
| 49 | * Create an empty statistics instance for the task. |
| 50 | * |
| 51 | * @param taskName |
| 52 | */ |
| 53 | public TimerBasicStatistics(String taskName) { |
| 54 | this.taskName = taskName; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Create a statistics instance and initialize with the values from the |
| 59 | * {@link Timer} provided. |
| 60 | * |
| 61 | * @param timer |
| 62 | */ |
| 63 | public TimerBasicStatistics(Timer timer) { |
| 64 | this.taskName = timer.getTaskName(); |
| 65 | addTimer(timer); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Add the results of a timer to the currently tracked statistics for this |
| 70 | * timer. |
| 71 | * |
| 72 | * @param timer |
| 73 | * @throws IllegalArgumentException |
| 74 | * if the {@link Timer#getTaskName()} does not match |
| 75 | * {@link #taskName} |
| 76 | */ |
| 77 | public void addTimer(Timer timer) { |
| 78 | if (!taskName.equals(timer.getTaskName())) { |
| 79 | throw new IllegalArgumentException( |
| 80 | "Task Statistics belong to a different task than provided timer"); |
| 81 | } |
| 82 | synchronized (this) { |
| 83 | count++; |
| 84 | long elapsedNanos = timer.getElapsedNanos(); |
| 85 | totalElapsedNanos += elapsedNanos; |
| 86 | minElapsedNanos = Math.min(minElapsedNanos, elapsedNanos); |
| 87 | maxElapsedNanos = Math.max(maxElapsedNanos, elapsedNanos); |
| 88 | |
| 89 | double previousMean = meanElapsedNanos; |
| 90 | meanElapsedNanos += (elapsedNanos - previousMean) / count; |
| 91 | sumOfDeltasElapsedNanos += ((double)elapsedNanos - previousMean) * ((double)elapsedNanos - meanElapsedNanos); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Return the task name for which the statistics are being tracked. |
| 97 | * |
| 98 | * @return |
| 99 | */ |
| 100 | public String getTaskName() { |
| 101 | return taskName; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Return the number of times this instance has been updated with timer |
| 106 | * values. |
| 107 | * |
| 108 | * @return |
| 109 | */ |
| 110 | public int getCount() { |
| 111 | return count; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Return the accumulated number of elapsed nanoseconds recorded for this |
| 116 | * task. |
| 117 | * |
| 118 | * @return |
| 119 | */ |
| 120 | public long getTotalElapsedNanos() { |
| 121 | return totalElapsedNanos; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Return the accumulated time recorded for this task. The time will be |
| 126 | * returned in the {@link TimeUnit} provided. |
| 127 | * |
| 128 | * @param timeUnit |
| 129 | * Desired time unit for returned value. |
| 130 | * @return |
| 131 | */ |
| 132 | public long getTotalElapsed(TimeUnit timeUnit) { |
| 133 | return timeUnit.convert(totalElapsedNanos, TimeUnit.NANOSECONDS); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Return the minimum number of elapsed nanoseconds recorded for this task. |
| 138 | * |
| 139 | * @return Minimum elapsed time in nanoseconds for this this task or |
| 140 | * {@link Long#MAX_VALUE} if no timers have yet been recorded. |
| 141 | */ |
| 142 | public long getMinElapsedNanos() { |
| 143 | return minElapsedNanos; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Return the minimum time recorded for this task. The time will be returned |
| 148 | * in the {@link TimeUnit} provided. |
| 149 | * |
| 150 | * @param timeUnit |
| 151 | * Desired time unit for returned value. |
| 152 | * @return |
| 153 | */ |
| 154 | public long getMinElapsed(TimeUnit timeUnit) { |
| 155 | return timeUnit.convert(minElapsedNanos, TimeUnit.NANOSECONDS); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Return the maximum number of elapsed nanoseconds recorded for this task. |
| 160 | * |
| 161 | * @return Maximum elapsed time in nanoseconds for this this task or |
| 162 | * {@link Long#MIN_VALUE} if no timers have yet been recorded. |
| 163 | */ |
| 164 | public long getMaxElapsedNanos() { |
| 165 | return maxElapsedNanos; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Return the maximum time recorded for this task. The time will be returned |
| 170 | * in the {@link TimeUnit} provided. |
| 171 | * |
| 172 | * @param timeUnit |
| 173 | * Desired time unit for returned value. |
| 174 | * @return |
| 175 | */ |
| 176 | public long getMaxElapsed(TimeUnit timeUnit) { |
| 177 | return timeUnit.convert(maxElapsedNanos, TimeUnit.NANOSECONDS); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Return the average elapsed time recorded for this task in nanoseconds. |
| 182 | * |
| 183 | * @return average elapsed time in nanoseconds. |
| 184 | */ |
| 185 | public double getAverageElapsedNanos() { |
| 186 | return meanElapsedNanos; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Return the average elapsed time recorded for this task. The time will be |
| 191 | * returned in the {@link TimeUnit} provided. |
| 192 | * <p> |
| 193 | * May lose precision as fractional portion of mean will be truncated in conversion method. |
| 194 | * |
| 195 | * @param timeUnit |
| 196 | * Desired time unit for returned value. |
| 197 | * @return average elapsed time in {@link TimeUnit} provided. |
| 198 | */ |
| 199 | public double getAverageElapsed(TimeUnit timeUnit) { |
| 200 | return timeUnit.convert((long)meanElapsedNanos, TimeUnit.NANOSECONDS); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Return the variance of elapsed times recorded for this task in nanoseconds. |
| 205 | * |
| 206 | * @return variance of elapsed time in nanoseconds. |
| 207 | */ |
| 208 | public double getVarianceElapsedNanos() { |
| 209 | return count > 1 ? sumOfDeltasElapsedNanos / (count - 1) : 0.0; |
| 210 | } |
| 211 | /** |
| 212 | * Return the variance of elapsed times recorded for this task. |
| 213 | * The time will be returned in the {@link TimeUnit} provided. |
| 214 | * <p> |
| 215 | * May lose precision as fractional portion of variance will be truncated in conversion method. |
| 216 | * |
| 217 | * @param timeUnit |
| 218 | * Desired time unit for returned value. |
| 219 | * @return standard deviation of elapsed times in {@link TimeUnit} provided. |
| 220 | */ |
| 221 | public double getVarianceElapsed(TimeUnit timeUnit) { |
| 222 | return timeUnit.convert((long)getVarianceElapsedNanos(), TimeUnit.NANOSECONDS); |
| 223 | } |
| 224 | |
| 225 | |
| 226 | /** |
| 227 | * Return the standard deviation of elapsed times recorded for this task in nanoseconds. |
| 228 | * |
| 229 | * @return standard deviation of elapsed time in nanoseconds. |
| 230 | */ |
| 231 | public double getStdDevElapsedNanos() { |
| 232 | return Math.sqrt(getVarianceElapsedNanos()); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Return the standard deviation of elapsed times recorded for this task. |
| 237 | * The time will be returned in the {@link TimeUnit} provided. |
| 238 | * <p> |
| 239 | * May lose precision as fractional portion of variance will be truncated in conversion method. |
| 240 | * |
| 241 | * @param timeUnit |
| 242 | * Desired time unit for returned value. |
| 243 | * @return standard deviation of elapsed times in {@link TimeUnit} provided. |
| 244 | */ |
| 245 | public double getStdDevElapsed(TimeUnit timeUnit) { |
| 246 | return timeUnit.convert((long)getStdDevElapsedNanos(), TimeUnit.NANOSECONDS); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Creates a snapshot of the instance and returns as a new instance. These |
| 251 | * values will not be updated as more timers are processed. |
| 252 | * |
| 253 | * @return new instance of {@link TimerBasicStatistics} that is a snapshot |
| 254 | * at the time returned. |
| 255 | */ |
| 256 | public TimerBasicStatistics getSnapshot() { |
| 257 | TimerBasicStatistics copy = new TimerBasicStatistics(taskName); |
| 258 | synchronized (this) { |
| 259 | copy.count = count; |
| 260 | copy.minElapsedNanos = minElapsedNanos; |
| 261 | copy.maxElapsedNanos = maxElapsedNanos; |
| 262 | copy.totalElapsedNanos = totalElapsedNanos; |
| 263 | copy.meanElapsedNanos = meanElapsedNanos; |
| 264 | copy.sumOfDeltasElapsedNanos = sumOfDeltasElapsedNanos; |
| 265 | } |
| 266 | return copy; |
| 267 | } |
| 268 | |
| 269 | @Override |
| 270 | public int compareTo(TimerBasicStatistics o) { |
| 271 | return taskName.compareTo(o.taskName); |
| 272 | } |
| 273 | |
| 274 | /* |
| 275 | * (non-Javadoc) |
| 276 | * |
| 277 | * @see java.lang.Object#hashCode() Eclipse generated |
| 278 | */ |
| 279 | @Override |
| 280 | public int hashCode() { |
| 281 | final int prime = 31; |
| 282 | int result = 1; |
| 283 | result = prime * result + ((taskName == null) ? 0 : taskName.hashCode()); |
| 284 | return result; |
| 285 | } |
| 286 | |
| 287 | /* |
| 288 | * (non-Javadoc) |
| 289 | * |
| 290 | * @see java.lang.Object#equals(java.lang.Object) Eclipse generated |
| 291 | */ |
| 292 | @Override |
| 293 | public boolean equals(Object obj) { |
| 294 | if (this == obj) { |
| 295 | return true; |
| 296 | } |
| 297 | if (obj == null) { |
| 298 | return false; |
| 299 | } |
| 300 | if (!(obj instanceof TimerBasicStatistics)) { |
| 301 | return false; |
| 302 | } |
| 303 | TimerBasicStatistics other = (TimerBasicStatistics) obj; |
| 304 | if (taskName == null) { |
| 305 | if (other.taskName != null) { |
| 306 | return false; |
| 307 | } |
| 308 | } else if (!taskName.equals(other.taskName)) { |
| 309 | return false; |
| 310 | } |
| 311 | return true; |
| 312 | } |
| 313 | |
| 314 | /* |
| 315 | * (non-Javadoc) |
| 316 | * |
| 317 | * @see java.lang.Object#toString() |
| 318 | */ |
| 319 | @Override |
| 320 | public String toString() { |
| 321 | StringBuilder sb = new StringBuilder(200); |
| 322 | sb.append("TimerBasicStatistics [taskName=").append(taskName); |
| 323 | sb.append(", count=").append(getCount()); |
| 324 | sb.append(", total=").append(getTotalElapsed(reportingUnit)); |
| 325 | sb.append(", min=").append(getMinElapsed(reportingUnit)); |
| 326 | sb.append(", max=").append(getMaxElapsed(reportingUnit)); |
| 327 | sb.append(", mean=").append(getAverageElapsed(reportingUnit)); |
| 328 | sb.append(", std_dev=").append(getStdDevElapsed(reportingUnit)); |
| 329 | sb.append("]"); |
| 330 | return sb.toString(); |
| 331 | } |
| 332 | } |