Commit | Line | Data |
---|---|---|
ce8319b6 BH |
1 | /******************************************************************************* |
2 | * Copyright (c) 2015 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.internal.analysis.os.linux.core.latency.statistics; | |
13 | ||
14 | import org.eclipse.tracecompass.segmentstore.core.ISegment; | |
15 | ||
16 | /** | |
53f46dc0 | 17 | * Class to calculate simple segment store statistics (min, max, average) |
ce8319b6 BH |
18 | * |
19 | * @author Bernd Hufmann | |
20 | */ | |
53f46dc0 | 21 | public class SegmentStoreStatistics { |
ce8319b6 BH |
22 | private long fMin; |
23 | private long fMax; | |
ce8319b6 | 24 | private long fNbSegments; |
2d626d38 MK |
25 | private double fAverage; |
26 | private double fVariance; | |
ce8319b6 BH |
27 | |
28 | /** | |
29 | * Constructor | |
30 | */ | |
53f46dc0 | 31 | public SegmentStoreStatistics() { |
2d626d38 MK |
32 | fMin = Long.MAX_VALUE; |
33 | fMax = Long.MIN_VALUE; | |
34 | fNbSegments = 0; | |
35 | fAverage = 0.0; | |
36 | fVariance = 0.0; | |
ce8319b6 BH |
37 | } |
38 | ||
39 | /** | |
40 | * Get minimum value | |
41 | * | |
42 | * @return minimum value | |
43 | */ | |
44 | public long getMin() { | |
45 | return fMin; | |
46 | } | |
47 | ||
48 | /** | |
49 | * Get maximum value | |
50 | * | |
51 | * @return maximum value | |
52 | */ | |
53 | public long getMax() { | |
54 | return fMax; | |
55 | } | |
56 | ||
57 | /** | |
58 | * Get number of segments analyzed | |
59 | * | |
60 | * @return number of segments analyzed | |
61 | */ | |
62 | public long getNbSegments() { | |
63 | return fNbSegments; | |
64 | } | |
65 | ||
66 | /** | |
67 | * Gets the arithmetic average | |
68 | * | |
69 | * @return arithmetic average | |
70 | */ | |
71 | public double getAverage() { | |
2d626d38 MK |
72 | return fAverage; |
73 | } | |
74 | ||
75 | /** | |
76 | * Gets the standard deviation of the segments, uses the online algorithm | |
77 | * shown here <a href= | |
78 | * "https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Online_algorithm"> | |
79 | * Wikipedia article of dec 3 2015 </a> | |
80 | * | |
81 | * @return the standard deviation of the segment store, will return NaN if | |
82 | * there are less than 3 elements | |
83 | */ | |
84 | public double getStdDev() { | |
85 | return fNbSegments > 2 ? Math.sqrt(fVariance / (fNbSegments - 1)) : Double.NaN; | |
ce8319b6 BH |
86 | } |
87 | ||
88 | /** | |
89 | * Update the statistics based on a given segment | |
2d626d38 MK |
90 | * <p> |
91 | * This is an online algorithm and must retain a complexity of O(1) | |
ce8319b6 BH |
92 | * |
93 | * @param segment | |
94 | * the segment used for the update | |
95 | */ | |
2d626d38 | 96 | public void update(ISegment segment) { |
ce8319b6 | 97 | long value = segment.getLength(); |
2d626d38 MK |
98 | /* |
99 | * Min and max are trivial, as well as number of segments | |
100 | */ | |
ce8319b6 BH |
101 | fMin = Math.min(fMin, value); |
102 | fMax = Math.max(fMax, value); | |
2d626d38 | 103 | |
ce8319b6 | 104 | fNbSegments++; |
2d626d38 MK |
105 | /* |
106 | * The running mean is not trivial, see proof in javadoc. | |
107 | */ | |
108 | double delta = value - fAverage; | |
109 | fAverage += delta / fNbSegments; | |
110 | fVariance += delta * (value - fAverage); | |
ce8319b6 BH |
111 | } |
112 | } |