1 4 package com.tc.management.stats; 5 6 import java.io.Serializable ; 7 import java.text.MessageFormat ; 8 9 public final class AggregateInteger implements Serializable { 10 11 19 private static final String TO_STRING_FORMAT = "<{0}(integer): [samples/{1}], [sum/{2}], [minimum/{3}], [maximum/{4}], [average/{5}]>"; 20 21 private static final class Sample { 22 23 final int sample; 24 final long timestamp; 25 26 Sample(final int sample) { 27 this.sample = sample; 28 timestamp = System.currentTimeMillis(); 29 } 30 31 } 32 33 private final String name; 34 35 private volatile int n; 39 private volatile int sum; 40 private volatile int minimum; 41 private volatile int maximum; 42 43 private final Sample[] sampleHistory; 46 private int nextHistoryPosition; 47 private final Sample[] sampleHistorySnapshot; 48 49 54 public AggregateInteger(final String name) { 55 this(name, 0); 56 } 57 58 62 public AggregateInteger(final String name, final int historyLengthInSamples) { 63 this.name = name; 64 sampleHistory = historyLengthInSamples > 0 ? new Sample[historyLengthInSamples] : null; 65 sampleHistorySnapshot = historyLengthInSamples > 0 ? new Sample[historyLengthInSamples] : null; 66 nextHistoryPosition = 0; 67 reset(); 68 } 69 70 public synchronized void addSample(final int sample) { 71 if (sample < minimum) minimum = sample; 72 if (sample > maximum) maximum = sample; 73 ++n; 74 sum += sample; 75 if (sampleHistory != null) { 78 sampleHistory[nextHistoryPosition++] = new Sample(sample); 79 nextHistoryPosition %= sampleHistory.length; 80 } 81 } 82 83 86 public synchronized void reset() { 87 n = sum = 0; 88 minimum = Integer.MAX_VALUE; 89 maximum = Integer.MIN_VALUE; 90 if (sampleHistory != null) { 91 for (int pos = 0; pos < sampleHistory.length; ++pos) { 92 sampleHistory[pos] = null; 93 } 94 nextHistoryPosition = 0; 95 } 96 } 97 98 public String getName() { 99 return name; 100 } 101 102 105 public int getMaximum() { 106 return maximum; 107 } 108 109 112 public int getMinimum() { 113 return minimum; 114 } 115 116 119 public int getN() { 120 return n; 121 } 122 123 126 public int getSum() { 127 return sum; 128 } 129 130 133 public double getAverage() { 134 return n > 0 ? ((double) sum / (double) n) : 0.0; 135 } 136 137 146 public int getSampleRate(final long periodInMillis) { 147 final int sampleRate; 154 if (sampleHistorySnapshot != null) { 155 synchronized (sampleHistorySnapshot) { 158 final int snapshotPosition; 159 final int localN; 160 synchronized (this) { 161 for (int pos = 0; pos < sampleHistory.length; ++pos) { 162 sampleHistorySnapshot[pos] = sampleHistory[pos]; 163 } 164 snapshotPosition = nextHistoryPosition; 165 localN = n; 166 } 167 if (localN > 0) { 168 final Sample oldestSample; 170 final int existingSampleCount; 171 if (localN > sampleHistorySnapshot.length) { 172 oldestSample = sampleHistorySnapshot[snapshotPosition]; 173 existingSampleCount = sampleHistorySnapshot.length; 174 } else { 175 oldestSample = sampleHistorySnapshot[0]; 176 existingSampleCount = localN; 177 } 178 final double elapsedSampleTimeInMillis = System.currentTimeMillis() - oldestSample.timestamp; 179 if (elapsedSampleTimeInMillis > 0) { 180 sampleRate = (int) ((periodInMillis / elapsedSampleTimeInMillis) * existingSampleCount); 181 } else { 182 sampleRate = 0; 183 } 184 } else { 185 sampleRate = 0; 186 } 187 } 188 } else { 189 sampleRate = -1; 190 } 191 return sampleRate; 192 } 193 194 public String toString() { 195 return MessageFormat.format(TO_STRING_FORMAT, new Object [] { name, new Integer (n), new Integer (sum), 196 new Integer (minimum), new Integer (maximum), new Integer (sum / n) }); 197 } 198 199 } 200 | Popular Tags |