Skip to contentPackage: Stats
Stats
Coverage
1: /*
2: * #%L
3: * *********************************************************************************************************************
4: *
5: * NorthernWind - lightweight CMS
6: * http://northernwind.tidalwave.it - git clone https://bitbucket.org/tidalwave/northernwind-src.git
7: * %%
8: * Copyright (C) 2011 - 2023 Tidalwave s.a.s. (http://tidalwave.it)
9: * %%
10: * *********************************************************************************************************************
11: *
12: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
13: * the License. You may obtain a copy of the License at
14: *
15: * http://www.apache.org/licenses/LICENSE-2.0
16: *
17: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
18: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
19: * specific language governing permissions and limitations under the License.
20: *
21: * *********************************************************************************************************************
22: *
23: *
24: * *********************************************************************************************************************
25: * #L%
26: */
27: package it.tidalwave.northernwind.profiling.impl;
28:
29: import javax.annotation.Nonnegative;
30: import javax.annotation.Nonnull;
31: import org.apache.commons.math3.stat.descriptive.SummaryStatistics;
32: import lombok.RequiredArgsConstructor;
33:
34: /***********************************************************************************************************************
35: *
36: * @author Fabrizio Giudici
37: *
38: **********************************************************************************************************************/
39: @RequiredArgsConstructor
40: public class Stats
41: {
42: private static final String PATTERN = "%s %s | count: %6d | min: %7.2f | avg: %7.2f | max: %9.2f | dev: %7.2f";
43:
44: @Nonnull
45: private final String name;
46:
47: @Nonnegative
48: private final double scale;
49:
50: private final SummaryStatistics globalStats = new SummaryStatistics();
51:
52: private final SummaryStatistics recentStats = new SummaryStatistics();
53:
54: public synchronized void addValue (final long elapsedTime)
55: {
56: globalStats.addValue(elapsedTime);
57: recentStats.addValue(elapsedTime);
58: }
59:
60: public synchronized void clearRecent()
61: {
62: recentStats.clear();
63: }
64:
65: @Nonnull
66: public synchronized String globalAsString()
67: {
68: return String.format(PATTERN,
69: "TOTAL ",
70: name,
71: globalStats.getN(),
72: globalStats.getMin() * scale,
73: globalStats.getMean() * scale,
74: globalStats.getMax() * scale,
75: globalStats.getStandardDeviation() * scale);
76: }
77:
78: @Nonnull
79: public synchronized String recentAsString()
80: {
81: return String.format(PATTERN,
82: "RECENT",
83: name,
84: recentStats.getN(),
85: recentStats.getMin() * scale,
86: recentStats.getMean() * scale,
87: recentStats.getMax() * scale,
88: recentStats.getStandardDeviation() * scale);
89: }
90: }