Skip to contentPackage: Statistics
Statistics
Coverage
1: /*
2: * *********************************************************************************************************************
3: *
4: * Mistral: open source imaging engine
5: * http://tidalwave.it/projects/mistral
6: *
7: * Copyright (C) 2003 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/mistral-src
23: * git clone https://github.com/tidalwave-it/mistral-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.image.processor;
28:
29: import java.util.Iterator;
30: import java.util.SortedMap;
31: import java.util.TreeMap;
32: import java.io.PrintWriter;
33: import java.io.Serializable;
34: import it.tidalwave.image.processor.Statistics.Item;
35: import lombok.extern.slf4j.Slf4j;
36:
37: /***********************************************************************************************************************
38: *
39: * This class represents a set of statistics, that can be updated incrementally
40: * sample by sample. Each item has a name; min/max/avg values are recorded.
41: * It is mostly used for measuring execution time, but can be used for any value
42: * of type <code>long</code>.
43: *
44: * @author Fabrizio Giudici
45: *
46: **********************************************************************************************************************/
47: @Slf4j
48: public class Statistics implements Serializable, Iterable<Item>
49: {
50: private static final long serialVersionUID = 6751339155167897162L;
51:
52: /**
53: * The map
54: */
55: private SortedMap<String, Item> map = new TreeMap<>();
56:
57: /*******************************************************************************************************************
58: *
59: * This class represents a single statistics item with name, min/max/avg
60: * value.
61: *
62: ******************************************************************************************************************/
63: public static class Item implements Serializable, Comparable<Item>
64: {
65: // private static final long serialVersionUID = 49503335739587897162L;
66:
67: private String name;
68:
69: private long minimum = Long.MAX_VALUE;
70:
71: private long maximum = Long.MIN_VALUE;
72:
73: private long accumulator;
74:
75: private int sampleCount;
76:
77: /***************************************************************************************************************
78: *
79: * Create a new sample.
80: *
81: * @param name the item name
82: *
83: **************************************************************************************************************/
84: public Item (final String name)
85: {
86: this.name = name;
87: }
88:
89: /***************************************************************************************************************
90: *
91: * Returns the sample name.
92: *
93: * @return the name
94: *
95: **************************************************************************************************************/
96: public String getName()
97: {
98: return name;
99: }
100:
101: /***************************************************************************************************************
102: *
103: * Returns the minimum value.
104: *
105: * @return the minimum value
106: *
107: **************************************************************************************************************/
108: public long getMinimum()
109: {
110: return minimum;
111: }
112:
113: /***************************************************************************************************************
114: *
115: * Returns the maximum value.
116: *
117: * @return the maximum value
118: *
119: **************************************************************************************************************/
120: public long getMaximum()
121: {
122: return maximum;
123: }
124:
125: /***************************************************************************************************************
126: *
127: * Returns the average value.
128: *
129: * @return the average value
130: *
131: **************************************************************************************************************/
132: public long getAverage()
133: {
134: return accumulator / sampleCount;
135: }
136:
137: public long getAccumulator()
138: {
139: return accumulator;
140: }
141:
142: public int getSampleCount()
143: {
144: return sampleCount;
145: }
146:
147: /***************************************************************************************************************
148: *
149: * Adds a new sample to this item.
150: *
151: * @param value the value
152: *
153: **************************************************************************************************************/
154: public void addSample (final long value)
155: {
156: minimum = Math.min(minimum, value);
157: maximum = Math.max(maximum, value);
158: accumulator += value;
159: sampleCount++;
160: }
161:
162: /***************************************************************************************************************
163: *
164: *
165: *
166: **************************************************************************************************************/
167: @Override
168: public int compareTo (final Statistics.Item o)
169: {
170: return this.name.compareTo(o.name);
171: }
172:
173: /***************************************************************************************************************
174: *
175: * Merges this item with another.
176: *
177: * @param otherItem the other item
178: *
179: **************************************************************************************************************/
180: protected void merge (final Item otherItem)
181: {
182: minimum = Math.min(minimum, otherItem.minimum);
183: maximum = Math.max(maximum, otherItem.maximum);
184: accumulator += otherItem.accumulator;
185: sampleCount += otherItem.sampleCount;
186: }
187:
188: /***************************************************************************************************************
189: *
190: *
191: *
192: **************************************************************************************************************/
193: @Override
194: public String toString()
195: {
196: return "Statistics.Item[" + name + " min:" + minimum + " max:" + maximum + " avg:" + getAverage() + "]";
197: }
198: }
199:
200: /*******************************************************************************************************************
201: *
202: * Adds a new sample to these statistics.
203: *
204: * @param name the name
205: * @param value the value
206: *
207: ******************************************************************************************************************/
208: public synchronized void addSample (final String name, final long value)
209: {
210: var statItem = map.get(name);
211:
212:• if (statItem == null)
213: {
214: statItem = new Item(name);
215: map.put(name, statItem);
216: }
217:
218: statItem.addSample(value);
219: }
220:
221: /*******************************************************************************************************************
222: *
223: * Merges these statistics with others.
224: *
225: * @param statistics the other statistics
226: *
227: ******************************************************************************************************************/
228: public synchronized void merge (final Statistics statistics)
229: {
230:• for (final var statItem : statistics)
231: {
232: final var here = map.get(statItem.getName());
233:
234:• if (here == null)
235: {
236: map.put(statItem.getName(), statItem);
237: }
238:
239: else
240: {
241: here.merge(statItem);
242: }
243: }
244: }
245:
246: /*******************************************************************************************************************
247: *
248: *
249: *
250: ******************************************************************************************************************/
251: public void dump()
252: {
253:• for (final var item : this)
254: {
255: log.info("STATS: >>>> " +
256: item.getName() + ": min/max/avg " +
257: item.getMinimum() + "/" +
258: item.getMaximum() + "/" +
259: item.getAverage() + " samples: " +
260: item.getSampleCount() + " acc: " +
261: item.getAccumulator());
262: }
263: }
264:
265: /*******************************************************************************************************************
266: *
267: *
268: *
269: ******************************************************************************************************************/
270: public void dump (final PrintWriter pw)
271: {
272:• for (final var item : this)
273: {
274: pw.println(item.getName() + ": min/max/avg " +
275: item.getMinimum() + "/" +
276: item.getMaximum() + "/" +
277: item.getAverage() + " samples: " +
278: item.getSampleCount() + " acc: " +
279: item.getAccumulator());
280: }
281: }
282:
283: /*******************************************************************************************************************
284: *
285: * {@inheritDoc}
286: *
287: ******************************************************************************************************************/
288: @Override
289: public Iterator<Statistics.Item> iterator()
290: {
291: return map.values().iterator();
292: }
293: }