001: /*
002: * hgcommons 7
003: * Hammurapi Group Common Library
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023:
024: package biz.hammurapi.metrics;
025:
026: import java.io.Serializable;
027: import java.util.ArrayList;
028: import java.util.Collection;
029: import java.util.Iterator;
030: import java.util.List;
031:
032: /**
033: *
034: * @author Pavel Vlasov
035: * @version $Revision: 1.8 $
036: */
037: public class SimpleMetric implements Metric, Comparable, Serializable {
038: /**
039: * Comment for <code>serialVersionUID</code>
040: */
041: private static final long serialVersionUID = -3636560361707914006L;
042: private String name;
043: private boolean keepMeasurements;
044:
045: public SimpleMetric(String name) {
046: this .name = name;
047: }
048:
049: public SimpleMetric(String name, boolean keepMeasurements) {
050: this .name = name;
051: this .keepMeasurements = keepMeasurements;
052: }
053:
054: private int measurements;
055: private double total;
056: private double min;
057: private double max;
058: private double deviation;
059:
060: public int getNumber() {
061: return measurements;
062: }
063:
064: public double getMin() {
065: return min;
066: }
067:
068: public double getMax() {
069: return max;
070: }
071:
072: public double getAvg() {
073: return total / measurements;
074: }
075:
076: public double getTotal() {
077: return total;
078: }
079:
080: public void add(final double value, final long time) {
081: addMeasurement(new Measurement() {
082: public double getValue() {
083: return value;
084: }
085:
086: public long getTime() {
087: return time;
088: }
089: });
090: }
091:
092: /**
093: * @param value
094: * @param measurement
095: */
096: protected void addMeasurement(Measurement measurement) {
097: if (measurements == 0 || measurement.getValue() < min) {
098: min = measurement.getValue();
099: }
100:
101: if (measurements == 0 || measurement.getValue() > max) {
102: max = measurement.getValue();
103: }
104:
105: measurements++;
106: total += measurement.getValue();
107: deviation += Math.abs(measurement.getValue() - total
108: / measurements);
109:
110: if (keepMeasurements) {
111: measurementsList.add(measurement);
112: }
113: }
114:
115: /**
116: * @param value
117: * @param time
118: * @return
119: */
120: protected Measurement newMeasurement(final double value,
121: final long time) {
122: return new Measurement() {
123: public double getValue() {
124: return value;
125: }
126:
127: public long getTime() {
128: return time;
129: }
130: };
131: }
132:
133: public void add(Metric metric) {
134: if (measurements == 0 || metric.getMin() < min) {
135: min = metric.getMin();
136: }
137:
138: if (measurements == 0 || metric.getMax() > max) {
139: max = metric.getMax();
140: }
141:
142: measurements += metric.getNumber();
143: total += metric.getTotal();
144: deviation += metric.getDeviation() * metric.getNumber();
145:
146: if (keepMeasurements) {
147: measurementsList.addAll(metric.getMeasurements());
148: }
149: }
150:
151: private List measurementsList = new ArrayList();
152:
153: public Collection getMeasurements() {
154: return measurementsList;
155: }
156:
157: public String getName() {
158: return name;
159: }
160:
161: public String toString() {
162: StringBuffer ret = new StringBuffer();
163: ret.append(measurements).append("/").append(total).append(
164: " <= ").append(name).append("\n");
165: Iterator it = measurementsList.iterator();
166: while (it.hasNext()) {
167: Measurement m = (Measurement) it.next();
168: ret.append("\t");
169: ret.append(m.getValue());
170: ret.append(" <- ");
171: ret.append("\n");
172: }
173: return ret.toString();
174: }
175:
176: public int compareTo(Object o) {
177: if (o instanceof Metric) {
178: return Double.compare(((Metric) o).getTotal(), total);
179: }
180:
181: return 1;
182: }
183:
184: public double getDeviation() {
185: return deviation / measurements;
186: }
187:
188: }
|