1 package org.apache.helix.monitoring;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;
23 import org.apache.commons.math.stat.descriptive.SynchronizedDescriptiveStatistics;
24
25 public class StatCollector
26 {
27 private static final int DEFAULT_WINDOW_SIZE = 100;
28 private final DescriptiveStatistics _stats;
29 private long _numDataPoints;
30 private long _totalSum;
31
32 public StatCollector()
33 {
34 _stats = new SynchronizedDescriptiveStatistics();
35 _stats.setWindowSize(DEFAULT_WINDOW_SIZE);
36 }
37
38 public void addData(double data)
39 {
40 _numDataPoints++;
41 _totalSum += data;
42 _stats.addValue(data);
43 }
44
45 public long getTotalSum()
46 {
47 return _totalSum;
48 }
49
50 public DescriptiveStatistics getStatistics()
51 {
52 return _stats;
53 }
54
55 public long getNumDataPoints()
56 {
57 return _numDataPoints;
58 }
59
60 public void reset()
61 {
62 _numDataPoints = 0;
63 _totalSum = 0;
64 _stats.clear();
65 }
66
67 public double getMean()
68 {
69 if(_stats.getN() == 0)
70 {
71 return 0;
72 }
73 return _stats.getMean();
74 }
75
76 public double getMax()
77 {
78 return _stats.getMax();
79 }
80
81 public double getMin()
82 {
83 return _stats.getMin();
84 }
85
86 public double getPercentile(int percentage)
87 {
88 if(_stats.getN() == 0)
89 {
90 return 0;
91 }
92 return _stats.getPercentile(percentage*1.0);
93 }
94 }