1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.Collections;
29  import java.util.HashMap;
30  import java.util.List;
31  import java.util.Map;
32  
33  /**
34   * <a href="MakerStats.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Harry Mark
37   *
38   */
39  public class MakerStats {
40  
41      public MakerStats(String name) {
42          //_name = name;
43      }
44  
45      public void add(String caller, int initSize, int finalSize) {
46          SizeSample stat = null;
47  
48          synchronized (_map) {
49              stat = _map.get(caller);
50  
51              if (stat == null) {
52                  stat = new SizeSample(caller, initSize);
53  
54                  _map.put(caller, stat);
55              }
56  
57              _count++;
58          }
59  
60          synchronized (stat) {
61              stat.add(finalSize);
62          }
63      }
64  
65      public void display(PrintStream printer) {
66          printer.println("caller,min,max,range,samples,average,initial");
67  
68          List<SizeSample> list = new ArrayList<SizeSample>(_map.size());
69  
70          list.addAll(_map.values());
71  
72          Collections.sort(list);
73  
74          int maxSize = 0;
75          int sampleSize = 0;
76          int totalSize = 0;
77  
78          for (int i = 0; i < list.size(); i++) {
79              SizeSample stat = list.get(i);
80  
81              printer.print(stat.getCaller());
82              printer.print(",");
83              printer.print(stat.getMinSize());
84              printer.print(",");
85              printer.print(stat.getMaxSize());
86              printer.print(",");
87              printer.print(stat.getMaxSize() - stat.getMinSize());
88              printer.print(",");
89              printer.print(stat.getSamplesSize());
90              printer.print(",");
91              printer.print(stat.getTotalSize() / stat.getSamplesSize());
92              printer.print(",");
93              printer.println(stat.getInitSize());
94  
95              sampleSize += stat.getSamplesSize();
96              totalSize += stat.getTotalSize();
97  
98              if (stat.getMaxSize() > maxSize) {
99                  maxSize = stat.getMaxSize();
100             }
101         }
102 
103         int avg = 0;
104 
105         if (sampleSize > 0) {
106             avg = totalSize / sampleSize;
107         }
108 
109         printer.print("SAMPLES=");
110         printer.print(sampleSize);
111         printer.print(", AVERAGE=");
112         printer.print(avg);
113         printer.print(", MAX=");
114         printer.println(maxSize);
115     }
116 
117     //private String _name;
118     private Map<String, SizeSample> _map = new HashMap<String, SizeSample>();
119     private int _count;
120 
121     private class SizeSample implements Comparable<SizeSample> {
122 
123         public SizeSample(String caller, int initSize) {
124             _caller = caller;
125             _initSize = initSize;
126             _minSize = Integer.MAX_VALUE;
127             _maxSize = Integer.MIN_VALUE;
128         }
129 
130         public void add(int finalSize) {
131             if (finalSize < _minSize) {
132                 _minSize = finalSize;
133             }
134 
135             if (finalSize > _maxSize) {
136                 _maxSize = finalSize;
137             }
138 
139             _samplesSize++;
140             _totalSize += finalSize;
141         }
142 
143         public String getCaller() {
144             return _caller;
145         }
146 
147         public int getInitSize() {
148             return _initSize;
149         }
150 
151         public int getMaxSize() {
152             return _maxSize;
153         }
154 
155         public int getMinSize() {
156             return _minSize;
157         }
158 
159         public int getSamplesSize() {
160             return _samplesSize;
161         }
162 
163         public int getTotalSize() {
164             return _totalSize;
165         }
166 
167         public int compareTo(SizeSample other) {
168             int thisAvg = 0;
169 
170             if (_samplesSize > 0) {
171                 thisAvg = _totalSize / _samplesSize;
172             }
173 
174             int otherAvg = 0;
175 
176             if (other.getSamplesSize() > 0) {
177                 otherAvg = other.getTotalSize() / other.getSamplesSize();
178             }
179 
180             return otherAvg - thisAvg;
181         }
182 
183         private String _caller;
184         private int _initSize;
185         private int _maxSize;
186         private int _minSize;
187         private int _samplesSize;
188         private int _totalSize;
189 
190     }
191 
192 }