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 
31 
33  @Override
35  return new StdDevMetricAggregator();
36  }
37 
38  private static class StdDevMetricAggregator implements MetricAggregator<Number> {
39 
40  List<Number> points = null;
41 
42 
43  private double getMean() {
44  double sum = 0;
45 
46  for (Number d : points) {
47  sum += d.doubleValue();
48  }
49  return sum / points.size();
50 
51  }
52 
53  @Override
54  public void append(Number calculated) {
55  if (points == null)
56  points = Lists.newLinkedList();
57 
58  points.add(calculated);
59  }
60 
61  @Override
62  public Number getAggregated() {
63  if (points == null)
64  return null;
65 
66  double mean = getMean();
67  double sum = 0;
68 
69  for (Number d : points) {
70  sum += (d.doubleValue() - mean) * (d.doubleValue() - mean);
71  }
72 
73  return Math.sqrt(sum / points.size());
74  }
75 
76  @Override
77  public void reset() {
78  points = null;
79  }
80 
81  @Override
82  public String getName() {
83  return "std_dev";
84  }
85 
86  @Override
87  public String toString() {
88  return "StdDevMetricAggregator{" +
89  "points=" + points +
90  '}';
91  }
92  }
93 }