001package org.avaje.datasource.pool;
002
003import org.avaje.datasource.PoolStatistics;
004
005/**
006 * Represents aggregated statistics collected from the DataSourcePool.
007 * <p>
008 * The goal is to present insight into the overload load of the DataSourcePool.
009 * These statistics can be collected and reported regularly to show load over
010 * time.
011 * </p>
012 * <p>
013 * Each pooled connection collects statistics. When a pooled connection is fully
014 * closed it can report it's statistics to the pool to be included as part of
015 * the collected statistics.
016 * </p>
017 */
018public class DataSourcePoolStatistics implements PoolStatistics {
019
020  private final long collectionStart;
021
022  private final long count;
023
024  private final long errorCount;
025
026  private final long hwmMicros;
027
028  private final long totalMicros;
029
030  /**
031   * Construct with statistics collected.
032   */
033  DataSourcePoolStatistics(long collectionStart, long count, long errorCount, long hwmMicros, long totalMicros) {
034    this.collectionStart = collectionStart;
035    this.count = count;
036    this.errorCount = errorCount;
037    this.hwmMicros = hwmMicros;
038    this.totalMicros = totalMicros;
039  }
040
041  public String toString() {
042    return "count[" + count + "] errors[" + errorCount + "] totalMicros[" + totalMicros + "] hwmMicros[" + hwmMicros
043        + "] avgMicros[" + getAvgMicros() + "]";
044  }
045
046  /**
047   * Return the start time this set of statistics was collected from.
048   */
049  @Override
050  public long getCollectionStart() {
051    return collectionStart;
052  }
053
054  /**
055   * Return the total number of 'get connection' requests.
056   */
057  @Override
058  public long getCount() {
059    return count;
060  }
061
062  /**
063   * Return the number of SQLExceptions reported.
064   */
065  @Override
066  public long getErrorCount() {
067    return errorCount;
068  }
069
070  /**
071   * Return the high water mark for the duration a connection was busy/used.
072   */
073  @Override
074  public long getHwmMicros() {
075    return hwmMicros;
076  }
077
078  /**
079   * Return the aggregate time connections were busy/used.
080   */
081  @Override
082  public long getTotalMicros() {
083    return totalMicros;
084  }
085
086  /**
087   * Return the average time connections were busy/used.
088   */
089  @Override
090  public long getAvgMicros() {
091    return (totalMicros == 0) ? 0 : totalMicros / count;
092  }
093
094}