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