1 //
2 // ========================================================================
3 // Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
8 //
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
11 //
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
14 //
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
17 //
18
19 package org.eclipse.jetty.util.statistic;
20
21 import java.util.concurrent.atomic.AtomicLong;
22
23 import org.eclipse.jetty.util.Atomics;
24
25
26 /**
27 * SampledStatistics
28 * <p>
29 * Provides max, total, mean, count, variance, and standard deviation of continuous sequence of samples.
30 * <p>
31 * Calculates estimates of mean, variance, and standard deviation characteristics of a sample using a non synchronized
32 * approximation of the on-line algorithm presented in <cite>Donald Knuth's Art of Computer Programming, Volume 2,
33 * Seminumerical Algorithms, 3rd edition, page 232, Boston: Addison-Wesley</cite>. that cites a 1962 paper by B.P. Welford that
34 * can be found by following <a href="http://www.jstor.org/pss/1266577">Note on a Method for Calculating Corrected Sums
35 * of Squares and Products</a>
36 * <p>
37 * This algorithm is also described in Wikipedia at <a href=
38 * "http://en.wikipedia.org/w/index.php?title=Algorithms_for_calculating_variance&section=4#On-line_algorithm">
39 * Algorithms for calculating variance </a>
40 */
41 public class SampleStatistic
42 {
43 protected final AtomicLong _max = new AtomicLong();
44 protected final AtomicLong _total = new AtomicLong();
45 protected final AtomicLong _count = new AtomicLong();
46 protected final AtomicLong _totalVariance100 = new AtomicLong();
47
48 public void reset()
49 {
50 _max.set(0);
51 _total.set(0);
52 _count.set(0);
53 _totalVariance100.set(0);
54 }
55
56 public void set(final long sample)
57 {
58 long total = _total.addAndGet(sample);
59 long count = _count.incrementAndGet();
60
61 if (count>1)
62 {
63 long mean10 = total*10/count;
64 long delta10 = sample*10 - mean10;
65 _totalVariance100.addAndGet(delta10*delta10);
66 }
67
68 Atomics.updateMax(_max, sample);
69 }
70
71 /**
72 * @return the max value
73 */
74 public long getMax()
75 {
76 return _max.get();
77 }
78
79 public long getTotal()
80 {
81 return _total.get();
82 }
83
84 public long getCount()
85 {
86 return _count.get();
87 }
88
89 public double getMean()
90 {
91 return (double)_total.get()/_count.get();
92 }
93
94 public double getVariance()
95 {
96 final long variance100 = _totalVariance100.get();
97 final long count = _count.get();
98
99 return count>1?((double)variance100)/100.0/(count-1):0.0;
100 }
101
102 public double getStdDev()
103 {
104 return Math.sqrt(getVariance());
105 }
106
107 /* ------------------------------------------------------------ */
108 @Override
109 public String toString()
110 {
111 return String.format("%s@%x{c=%d,m=%d,t=%d,v100=%d}",this.getClass().getSimpleName(),hashCode(),_count.get(),_max.get(),_total.get(),_totalVariance100.get());
112 }
113 }