analysis: Add totals to latency statistics view
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.timing.core / src / org / eclipse / tracecompass / analysis / timing / core / segmentstore / statistics / SegmentStoreStatistics.java
1 /*******************************************************************************
2 * Copyright (c) 2015, 2016 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are made
5 * available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
12 package org.eclipse.tracecompass.analysis.timing.core.segmentstore.statistics;
13
14 import org.eclipse.tracecompass.segmentstore.core.BasicSegment;
15 import org.eclipse.tracecompass.segmentstore.core.ISegment;
16
17 /**
18 * Class to calculate simple segment store statistics (min, max, average)
19 *
20 * @author Bernd Hufmann
21 */
22 public class SegmentStoreStatistics {
23 private ISegment fMin;
24 private ISegment fMax;
25 private long fNbSegments;
26 private double fAverage;
27 private double fVariance;
28 private double fTotal;
29
30 /**
31 * Constructor
32 */
33 public SegmentStoreStatistics() {
34 fMin = new BasicSegment(0, Long.MAX_VALUE);
35 fMax = new BasicSegment(Long.MIN_VALUE, 0);
36 fNbSegments = 0;
37 fAverage = 0.0;
38 fVariance = 0.0;
39 fTotal = 0.0;
40 }
41
42 /**
43 * Get minimum value
44 *
45 * @return minimum value
46 */
47 public long getMin() {
48 return fMin.getLength();
49 }
50
51 /**
52 * Get maximum value
53 *
54 * @return maximum value
55 */
56 public long getMax() {
57 return fMax.getLength();
58 }
59
60 /**
61 * Get segment with minimum length
62 *
63 * @return segment with minimum length
64 */
65 public ISegment getMinSegment() {
66 return fMin;
67 }
68
69 /**
70 * Get segment with maximum length
71 *
72 * @return segment with maximum length
73 */
74 public ISegment getMaxSegment() {
75 return fMax;
76 }
77
78 /**
79 * Get number of segments analyzed
80 *
81 * @return number of segments analyzed
82 */
83 public long getNbSegments() {
84 return fNbSegments;
85 }
86
87 /**
88 * Gets the arithmetic average
89 *
90 * @return arithmetic average
91 */
92 public double getAverage() {
93 return fAverage;
94 }
95
96 /**
97 * Gets the standard deviation of the segments, uses the online algorithm
98 * shown here <a href=
99 * "https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Online_algorithm">
100 * Wikipedia article of dec 3 2015 </a>
101 *
102 * @return the standard deviation of the segment store, will return NaN if
103 * there are less than 3 elements
104 */
105 public double getStdDev() {
106 return fNbSegments > 2 ? Math.sqrt(fVariance / (fNbSegments - 1)) : Double.NaN;
107 }
108
109 /**
110 * Update the statistics based on a given segment
111 * <p>
112 * This is an online algorithm and must retain a complexity of O(1)
113 *
114 * @param segment
115 * the segment used for the update
116 */
117 public void update(ISegment segment) {
118 long value = segment.getLength();
119 /*
120 * Min and max are trivial, as well as number of segments
121 */
122 long min = fMin.getLength();
123 long max = fMax.getLength();
124 fMin = min <= value ? fMin : segment;
125 fMax = max >= value ? fMax : segment;
126
127 fNbSegments++;
128 /*
129 * The running mean is not trivial, see proof in javadoc.
130 */
131 double delta = value - fAverage;
132 fAverage += delta / fNbSegments;
133 fVariance += delta * (value - fAverage);
134 fTotal += value;
135 }
136
137 /**
138 * Get total value
139 *
140 * @return total value
141 * @since 1.1
142 */
143 public double getTotal() {
144 return fTotal;
145 }
146 }
This page took 0.049808 seconds and 6 git commands to generate.