001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.kernel.util;
022:
023: import java.io.PrintStream;
024:
025: import java.util.ArrayList;
026: import java.util.Collections;
027: import java.util.HashMap;
028: import java.util.List;
029: import java.util.Map;
030:
031: /**
032: * <a href="MakerStats.java.html"><b><i>View Source</i></b></a>
033: *
034: * @author Harry Mark
035: *
036: */
037: public class MakerStats {
038:
039: public MakerStats(String name) {
040: _name = name;
041: }
042:
043: public void add(String caller, int initSize, int finalSize) {
044: SizeSample stat = null;
045:
046: synchronized (_map) {
047: stat = (SizeSample) _map.get(caller);
048:
049: if (stat == null) {
050: stat = new SizeSample(caller, initSize);
051:
052: _map.put(caller, stat);
053: }
054:
055: _count++;
056: }
057:
058: synchronized (stat) {
059: stat.add(finalSize);
060: }
061: }
062:
063: public void display(PrintStream printer) {
064: printer.println("caller,min,max,range,samples,average,initial");
065:
066: List list = new ArrayList(_map.size());
067:
068: list.addAll(_map.values());
069:
070: Collections.sort(list);
071:
072: int maxSize = 0;
073: int sampleSize = 0;
074: int totalSize = 0;
075:
076: for (int i = 0; i < list.size(); i++) {
077: SizeSample stat = (SizeSample) list.get(i);
078:
079: printer.print(stat.getCaller());
080: printer.print(",");
081: printer.print(stat.getMinSize());
082: printer.print(",");
083: printer.print(stat.getMaxSize());
084: printer.print(",");
085: printer.print(stat.getMaxSize() - stat.getMinSize());
086: printer.print(",");
087: printer.print(stat.getSamplesSize());
088: printer.print(",");
089: printer.print(stat.getTotalSize() / stat.getSamplesSize());
090: printer.print(",");
091: printer.println(stat.getInitSize());
092:
093: sampleSize += stat.getSamplesSize();
094: totalSize += stat.getTotalSize();
095:
096: if (stat.getMaxSize() > maxSize) {
097: maxSize = stat.getMaxSize();
098: }
099: }
100:
101: int avg = 0;
102:
103: if (sampleSize > 0) {
104: avg = totalSize / sampleSize;
105: }
106:
107: printer.print("SAMPLES=");
108: printer.print(sampleSize);
109: printer.print(", AVERAGE=");
110: printer.print(avg);
111: printer.print(", MAX=");
112: printer.println(maxSize);
113: }
114:
115: private String _name;
116: private Map _map = new HashMap();
117: private int _count;
118:
119: private class SizeSample implements Comparable {
120:
121: public SizeSample(String caller, int initSize) {
122: _caller = caller;
123: _initSize = initSize;
124: _minSize = Integer.MAX_VALUE;
125: _maxSize = Integer.MIN_VALUE;
126: }
127:
128: public void add(int finalSize) {
129: if (finalSize < _minSize) {
130: _minSize = finalSize;
131: }
132:
133: if (finalSize > _maxSize) {
134: _maxSize = finalSize;
135: }
136:
137: _samplesSize++;
138: _totalSize += finalSize;
139: }
140:
141: public String getCaller() {
142: return _caller;
143: }
144:
145: public int getInitSize() {
146: return _initSize;
147: }
148:
149: public int getMaxSize() {
150: return _maxSize;
151: }
152:
153: public int getMinSize() {
154: return _minSize;
155: }
156:
157: public int getSamplesSize() {
158: return _samplesSize;
159: }
160:
161: public int getTotalSize() {
162: return _totalSize;
163: }
164:
165: public int compareTo(Object obj) {
166: SizeSample other = (SizeSample) obj;
167:
168: int this Avg = 0;
169:
170: if (_samplesSize > 0) {
171: this Avg = _totalSize / _samplesSize;
172: }
173:
174: int otherAvg = 0;
175:
176: if (other.getSamplesSize() > 0) {
177: otherAvg = other.getTotalSize()
178: / other.getSamplesSize();
179: }
180:
181: return otherAvg - this Avg;
182: }
183:
184: private String _caller;
185: private int _initSize;
186: private int _maxSize;
187: private int _minSize;
188: private int _samplesSize;
189: private int _totalSize;
190:
191: }
192:
193: }
|