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