1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.util;
24  
25  import java.io.PrintStream;
26  
27  import java.util.ArrayList;
28  import java.util.HashMap;
29  import java.util.List;
30  import java.util.Map;
31  
32  /**
33   * <a href="MakerStats.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Harry Mark
36   */
37  public class MakerStats {
38  
39      public MakerStats(String name) {
40          //_name = name;
41      }
42  
43      public void add(String caller, int initSize, int finalSize) {
44          SizeSample stat = null;
45  
46          synchronized (_map) {
47              stat = _map.get(caller);
48  
49              if (stat == null) {
50                  stat = new SizeSample(caller, initSize);
51  
52                  _map.put(caller, stat);
53              }
54  
55              _count++;
56          }
57  
58          synchronized (stat) {
59              stat.add(finalSize);
60          }
61      }
62  
63      public void display(PrintStream printer) {
64          printer.println("caller,min,max,range,samples,average,initial");
65  
66          List<SizeSample> list = new ArrayList<SizeSample>(_map.size());
67  
68          list.addAll(_map.values());
69  
70          list = ListUtil.sort(list);
71  
72          int maxSize = 0;
73          int sampleSize = 0;
74          int totalSize = 0;
75  
76          for (int i = 0; i < list.size(); i++) {
77              SizeSample stat = list.get(i);
78  
79              printer.print(stat.getCaller());
80              printer.print(",");
81              printer.print(stat.getMinSize());
82              printer.print(",");
83              printer.print(stat.getMaxSize());
84              printer.print(",");
85              printer.print(stat.getMaxSize() - stat.getMinSize());
86              printer.print(",");
87              printer.print(stat.getSamplesSize());
88              printer.print(",");
89              printer.print(stat.getTotalSize() / stat.getSamplesSize());
90              printer.print(",");
91              printer.println(stat.getInitSize());
92  
93              sampleSize += stat.getSamplesSize();
94              totalSize += stat.getTotalSize();
95  
96              if (stat.getMaxSize() > maxSize) {
97                  maxSize = stat.getMaxSize();
98              }
99          }
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<String, SizeSample> _map = new HashMap<String, SizeSample>();
117     private int _count;
118 
119     private class SizeSample implements Comparable<SizeSample> {
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(SizeSample other) {
166             int thisAvg = 0;
167 
168             if (_samplesSize > 0) {
169                 thisAvg = _totalSize / _samplesSize;
170             }
171 
172             int otherAvg = 0;
173 
174             if (other.getSamplesSize() > 0) {
175                 otherAvg = other.getTotalSize() / other.getSamplesSize();
176             }
177 
178             return otherAvg - thisAvg;
179         }
180 
181         private String _caller;
182         private int _initSize;
183         private int _maxSize;
184         private int _minSize;
185         private int _samplesSize;
186         private int _totalSize;
187 
188     }
189 
190 }