1
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
38 public class MakerStats {
39
40 public MakerStats(String name) {
41 }
43
44 public void add(String caller, int initSize, int finalSize) {
45 SizeSample stat = null;
46
47 synchronized (_map) {
48 stat = _map.get(caller);
49
50 if (stat == null) {
51 stat = new SizeSample(caller, initSize);
52
53 _map.put(caller, stat);
54 }
55
56 _count++;
57 }
58
59 synchronized (stat) {
60 stat.add(finalSize);
61 }
62 }
63
64 public void display(PrintStream printer) {
65 printer.println("caller,min,max,range,samples,average,initial");
66
67 List<SizeSample> list = new ArrayList<SizeSample>(_map.size());
68
69 list.addAll(_map.values());
70
71 list = ListUtil.sort(list);
72
73 int maxSize = 0;
74 int sampleSize = 0;
75 int totalSize = 0;
76
77 for (int i = 0; i < list.size(); i++) {
78 SizeSample stat = list.get(i);
79
80 printer.print(stat.getCaller());
81 printer.print(",");
82 printer.print(stat.getMinSize());
83 printer.print(",");
84 printer.print(stat.getMaxSize());
85 printer.print(",");
86 printer.print(stat.getMaxSize() - stat.getMinSize());
87 printer.print(",");
88 printer.print(stat.getSamplesSize());
89 printer.print(",");
90 printer.print(stat.getTotalSize() / stat.getSamplesSize());
91 printer.print(",");
92 printer.println(stat.getInitSize());
93
94 sampleSize += stat.getSamplesSize();
95 totalSize += stat.getTotalSize();
96
97 if (stat.getMaxSize() > maxSize) {
98 maxSize = stat.getMaxSize();
99 }
100 }
101
102 int avg = 0;
103
104 if (sampleSize > 0) {
105 avg = totalSize / sampleSize;
106 }
107
108 printer.print("SAMPLES=");
109 printer.print(sampleSize);
110 printer.print(", AVERAGE=");
111 printer.print(avg);
112 printer.print(", MAX=");
113 printer.println(maxSize);
114 }
115
116 private Map<String, SizeSample> _map = new HashMap<String, SizeSample>();
118 private int _count;
119
120 private class SizeSample implements Comparable<SizeSample> {
121
122 public SizeSample(String caller, int initSize) {
123 _caller = caller;
124 _initSize = initSize;
125 _minSize = Integer.MAX_VALUE;
126 _maxSize = Integer.MIN_VALUE;
127 }
128
129 public void add(int finalSize) {
130 if (finalSize < _minSize) {
131 _minSize = finalSize;
132 }
133
134 if (finalSize > _maxSize) {
135 _maxSize = finalSize;
136 }
137
138 _samplesSize++;
139 _totalSize += finalSize;
140 }
141
142 public String getCaller() {
143 return _caller;
144 }
145
146 public int getInitSize() {
147 return _initSize;
148 }
149
150 public int getMaxSize() {
151 return _maxSize;
152 }
153
154 public int getMinSize() {
155 return _minSize;
156 }
157
158 public int getSamplesSize() {
159 return _samplesSize;
160 }
161
162 public int getTotalSize() {
163 return _totalSize;
164 }
165
166 public int compareTo(SizeSample other) {
167 int thisAvg = 0;
168
169 if (_samplesSize > 0) {
170 thisAvg = _totalSize / _samplesSize;
171 }
172
173 int otherAvg = 0;
174
175 if (other.getSamplesSize() > 0) {
176 otherAvg = other.getTotalSize() / other.getSamplesSize();
177 }
178
179 return otherAvg - thisAvg;
180 }
181
182 private String _caller;
183 private int _initSize;
184 private int _maxSize;
185 private int _minSize;
186 private int _samplesSize;
187 private int _totalSize;
188
189 }
190
191 }