View Javadoc

1   package org.apache.helix.monitoring;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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  }