Jagger
 All Classes Namespaces Files Functions Variables Enumerator Groups Pages
StdDevMetricAggregatorProvider.java
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2012 Grid Dynamics Consulting Services, Inc, All Rights Reserved
3  * http://www.griddynamics.com
4  *
5  * This library is free software; you can redistribute it and/or modify it under the terms of
6  * the Apache License; either
7  * version 2.0 of the License, or any later version.
8  *
9  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
10  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
11  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
12  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
13  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
14  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
15  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
16  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
17  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
18  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19  */
20 package com.griddynamics.jagger.engine.e1.collector;
21 
22 import com.google.common.collect.Lists;
23 
24 import java.util.List;
25 
26 import static com.griddynamics.jagger.util.StandardMetricsNamesUtil.STANDARD_DEVIATION_AGGREGATOR_ID;
27 
32 public class StdDevMetricAggregatorProvider implements MetricAggregatorProvider {
33 
35  @Override
37  return new StdDevMetricAggregator();
38  }
39 
40  private static class StdDevMetricAggregator implements MetricAggregator<Number> {
41 
42  List<Number> points = null;
43 
44 
45  private double getMean() {
46  double sum = 0;
47 
48  for (Number d : points) {
49  sum += d.doubleValue();
50  }
51  return sum / points.size();
52 
53  }
54 
55  @Override
56  public void append(Number calculated) {
57  if (points == null)
58  points = Lists.newLinkedList();
59 
60  points.add(calculated);
61  }
62 
63  @Override
64  public Number getAggregated() {
65  if (points == null)
66  return null;
67 
68  double mean = getMean();
69  double sum = 0;
70 
71  for (Number d : points) {
72  sum += (d.doubleValue() - mean) * (d.doubleValue() - mean);
73  }
74 
75  return Math.sqrt(sum / points.size());
76  }
77 
78  @Override
79  public void reset() {
80  points = null;
81  }
82 
83  @Override
84  public String getName() {
85  return STANDARD_DEVIATION_AGGREGATOR_ID;
86  }
87 
88  @Override
89  public String toString() {
90  return "StdDevMetricAggregator{" +
91  "points=" + points +
92  '}';
93  }
94  }
95 }