1 8 9 package mx4j.tools.stats; 10 11 import java.util.Date ; 12 import java.util.SortedMap ; 13 import java.util.TreeMap ; 14 import javax.management.MBeanRegistration ; 15 import javax.management.MBeanServer ; 16 import javax.management.ObjectName ; 17 18 import mx4j.log.Log; 19 import mx4j.log.Logger; 20 21 28 public abstract class AbstractStatisticsRecorder implements StatisticsRecorderMBean, MBeanRegistration 29 { 30 31 protected boolean isActive = false; 32 33 34 protected MBeanServer server; 35 36 37 protected int maxEntries = 256; 38 39 40 protected SortedMap entries = new TreeMap (); 41 42 43 protected Date recordingStart; 44 45 46 protected boolean isDouble = false; 47 48 49 protected double minimumValue, maximumValue, averageValue; 50 51 52 protected long count = 0; 53 54 protected Logger getLogger() 55 { 56 return Log.getLogger(getClass().getName()); 57 } 58 59 public void start() 60 { 61 Logger logger = getLogger(); 62 if (!isActive) 63 { 64 if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Starting statistics recorder " + this); 65 this.isActive = true; 66 recordingStart = new Date (); 67 entries.clear(); 68 minimumValue = maximumValue = averageValue = 0; 69 count = 0; 70 isDouble = false; 71 try 72 { 73 doStart(); 74 } 75 catch (Exception e) 76 { 77 logger.error("Exception while starting recorder " + this, e); 78 } 79 } 80 } 81 82 public void stop() 83 { 84 Logger logger = getLogger(); 85 if (isActive) 86 { 87 if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Starting statistics recorder " + this); 88 this.isActive = false; 89 try 90 { 91 doStop(); 92 } 93 catch (Exception e) 94 { 95 logger.error("Exception starting recorder " + this, e); 96 } 97 } 98 } 99 100 public Number getAverage() 101 { 102 return createValue(averageValue); 103 } 104 105 public Number getMin() 106 { 107 return createValue(minimumValue); 108 } 109 110 public Number getMax() 111 { 112 return createValue(maximumValue); 113 } 114 115 public synchronized boolean isActive() 116 { 117 return isActive; 118 } 119 120 public int getMaxEntries() 121 { 122 return maxEntries; 123 } 124 125 public void setMaxEntries(int maxEntries) 126 { 127 if (maxEntries <= 0) 128 { 129 throw new IllegalArgumentException ("Max entries has to be bigger than 0"); 130 } 131 this.maxEntries = maxEntries; 132 } 133 134 public SortedMap getEntries() 135 { 136 return (SortedMap )((TreeMap )entries).clone(); 137 } 138 139 public Date getRecordingStart() 140 { 141 return recordingStart; 142 } 143 144 public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception 145 { 146 this.server = server; 147 return name; 148 } 149 150 public void postRegister(Boolean registrationDone) 151 { 152 } 153 154 public void preDeregister() throws Exception 155 { 156 this.stop(); 157 } 158 159 public void postDeregister() 160 { 161 } 162 163 166 protected void doStart() throws Exception 167 { 168 } 169 170 173 protected void doStop() throws Exception 174 { 175 } 176 177 181 protected synchronized void addEntry(Date key, Number value) 182 { 183 if (isActive) 184 { 185 entries.put(new PointTime(key, count++), value); 186 if (entries.size() > maxEntries) 187 { 188 while (entries.size() > maxEntries) 189 { 190 entries.remove(entries.firstKey()); 191 } 192 } 193 calculateStats(value); 194 } 195 } 196 197 200 private void calculateStats(Number value) 201 { 202 if (!isDouble && (value instanceof Double || value instanceof Float )) 203 { 204 isDouble = true; 205 } 206 double newValue = value.doubleValue(); 207 208 if (count == 1) 209 { 210 maximumValue = minimumValue = averageValue = newValue; 211 return; 212 } 213 if (newValue > maximumValue) 214 { 215 maximumValue = newValue; 216 } 217 if (newValue < minimumValue) 218 { 219 minimumValue = newValue; 220 } 221 averageValue = (1 - 1 / (double)count) * averageValue + 1 / (double)count * newValue; 222 } 223 224 private Number createValue(double targetValue) 225 { 226 if (isDouble) 227 { 228 return new Double (targetValue); 229 } 230 else 231 { 232 return new Long ((long)targetValue); 233 } 234 } 235 236 } 237 | Popular Tags |