blob: abc1ed37e66ca5ac3d4b62065b87fcb33e6708e1 [file] [log] [blame]
Jake Slack03928ae2014-05-13 18:41:56 -07001//
2// ========================================================================
3// Copyright (c) 1995-2014 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
19package org.eclipse.jetty.util.statistic;
20
21import java.util.concurrent.atomic.AtomicLong;
22
23import org.eclipse.jetty.util.Atomics;
24
25
26/* ------------------------------------------------------------ */
27/**
28 * SampledStatistics
29 * <p>
30 * Provides max, total, mean, count, variance, and standard
31 * deviation of continuous sequence of samples.
32 * <p>
33 * Calculates estimates of mean, variance, and standard deviation
34 * characteristics of a sample using a non synchronized
35 * approximation of the on-line algorithm presented
36 * in Donald Knuth's Art of Computer Programming, Volume 2,
37 * Seminumerical Algorithms, 3rd edition, page 232,
38 * Boston: Addison-Wesley. that cites a 1962 paper by B.P. Welford
39 * that can be found by following the link http://www.jstor.org/pss/1266577
40 * <p>
41 * This algorithm is also described in Wikipedia at
42 * http://en.wikipedia.org/w/index.php?title=Algorithms_for_calculating_variance&section=4#On-line_algorithm
43 */
44public class SampleStatistic
45{
46 protected final AtomicLong _max = new AtomicLong();
47 protected final AtomicLong _total = new AtomicLong();
48 protected final AtomicLong _count = new AtomicLong();
49 protected final AtomicLong _totalVariance100 = new AtomicLong();
50
51 public void reset()
52 {
53 _max.set(0);
54 _total.set(0);
55 _count.set(0);
56 _totalVariance100.set(0);
57 }
58
59 public void set(final long sample)
60 {
61 long total = _total.addAndGet(sample);
62 long count = _count.incrementAndGet();
63
64 if (count>1)
65 {
66 long mean10 = total*10/count;
67 long delta10 = sample*10 - mean10;
68 _totalVariance100.addAndGet(delta10*delta10);
69 }
70
71 Atomics.updateMax(_max, sample);
72 }
73
74 /**
75 * @return the max value
76 */
77 public long getMax()
78 {
79 return _max.get();
80 }
81
82 public long getTotal()
83 {
84 return _total.get();
85 }
86
87 public long getCount()
88 {
89 return _count.get();
90 }
91
92 public double getMean()
93 {
94 return (double)_total.get()/_count.get();
95 }
96
97 public double getVariance()
98 {
99 final long variance100 = _totalVariance100.get();
100 final long count = _count.get();
101
102 return count>1?((double)variance100)/100.0/(count-1):0.0;
103 }
104
105 public double getStdDev()
106 {
107 return Math.sqrt(getVariance());
108 }
109}