Commit | Line | Data |
---|---|---|
e06c9955 | 1 | /******************************************************************************* |
658401c8 | 2 | * Copyright (c) 2016 Ericsson |
e06c9955 MK |
3 | * |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made 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 | ||
658401c8 | 10 | package org.eclipse.tracecompass.analysis.timing.core.tests.segmentstore.statistics; |
e06c9955 MK |
11 | |
12 | import java.util.Collection; | |
13 | ||
14 | import org.eclipse.jdt.annotation.NonNull; | |
e06c9955 MK |
15 | import org.eclipse.tracecompass.segmentstore.core.ISegment; |
16 | ||
17 | /** | |
18 | * This calculates the statistics of a segment store in an offline manner to | |
19 | * validate online calculations. | |
20 | * | |
21 | * @author Matthew Khouzam | |
22 | * | |
23 | */ | |
24 | public class OfflineStatisticsCalculator { | |
381e1541 | 25 | private final Collection<@NonNull ISegment> fSs; |
e06c9955 MK |
26 | |
27 | /** | |
28 | * Constructor | |
29 | * | |
30 | * @param ss | |
31 | * segment store, fully build | |
32 | */ | |
381e1541 | 33 | public OfflineStatisticsCalculator(Collection<@NonNull ISegment> ss) { |
e06c9955 MK |
34 | fSs = ss; |
35 | } | |
36 | ||
37 | /** | |
38 | * Get the max value | |
39 | * | |
40 | * @return the max value | |
41 | */ | |
42 | public long getMax() { | |
43 | long max = Long.MIN_VALUE; | |
44 | for (ISegment interval : fSs) { | |
45 | max = Math.max(max, interval.getLength()); | |
46 | } | |
47 | return max; | |
48 | } | |
49 | ||
50 | /** | |
51 | * Get the min value | |
52 | * | |
53 | * @return the min value | |
54 | */ | |
55 | public long getMin() { | |
56 | long min = Long.MAX_VALUE; | |
57 | for (ISegment interval : fSs) { | |
58 | min = Math.min(min, interval.getLength()); | |
59 | } | |
60 | return min; | |
61 | } | |
62 | ||
63 | /** | |
64 | * Get the average value | |
65 | * | |
66 | * @return the average value | |
67 | */ | |
68 | public double getAvg() { | |
69 | double total = 0; | |
70 | for (ISegment interval : fSs) { | |
71 | total += (double) interval.getLength() / (double) fSs.size(); | |
72 | } | |
73 | return total; | |
74 | } | |
75 | ||
76 | /** | |
381e1541 | 77 | * Get the standard deviation. |
e06c9955 MK |
78 | * |
79 | * @return the standard deviation | |
80 | */ | |
81 | public double getStdDev() { | |
82 | if (fSs.size() < 3) { | |
83 | return Double.NaN; | |
84 | } | |
85 | double mean = getAvg(); | |
86 | ||
87 | double totalVariance = 0; | |
88 | for (ISegment interval : fSs) { | |
89 | double result = interval.getLength() - mean; | |
90 | totalVariance += result * result / (fSs.size() - 1); | |
91 | } | |
92 | return Math.sqrt(totalVariance); | |
93 | } | |
381e1541 MK |
94 | |
95 | /** | |
96 | * Get the total | |
97 | * | |
98 | * @return the total | |
99 | */ | |
100 | public long getTotal() { | |
101 | long total = 0; | |
102 | for (ISegment interval : fSs) { | |
103 | total += interval.getLength(); | |
104 | } | |
105 | return total; | |
106 | } | |
107 | ||
108 | /** | |
109 | * Get the # of intervals | |
110 | * @return the # of intervals | |
111 | */ | |
112 | public int count() { | |
113 | return fSs.size(); | |
114 | } | |
e06c9955 | 115 | } |