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