| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2016 Ericsson |
| 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 | |
| 10 | package org.eclipse.tracecompass.analysis.timing.core.tests.segmentstore.statistics; |
| 11 | |
| 12 | import java.util.Collection; |
| 13 | |
| 14 | import org.eclipse.jdt.annotation.NonNull; |
| 15 | import org.eclipse.tracecompass.analysis.os.linux.core.latency.SystemCall; |
| 16 | import org.eclipse.tracecompass.segmentstore.core.ISegment; |
| 17 | |
| 18 | /** |
| 19 | * This calculates the statistics of a segment store in an offline manner to |
| 20 | * validate online calculations. |
| 21 | * |
| 22 | * @author Matthew Khouzam |
| 23 | * |
| 24 | */ |
| 25 | public class OfflineStatisticsCalculator { |
| 26 | private final Collection<@NonNull SystemCall> fSs; |
| 27 | |
| 28 | /** |
| 29 | * Constructor |
| 30 | * |
| 31 | * @param ss |
| 32 | * segment store, fully build |
| 33 | */ |
| 34 | public OfflineStatisticsCalculator(Collection<@NonNull SystemCall> ss) { |
| 35 | fSs = ss; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Get the max value |
| 40 | * |
| 41 | * @return the max value |
| 42 | */ |
| 43 | public long getMax() { |
| 44 | long max = Long.MIN_VALUE; |
| 45 | for (ISegment interval : fSs) { |
| 46 | max = Math.max(max, interval.getLength()); |
| 47 | } |
| 48 | return max; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Get the min value |
| 53 | * |
| 54 | * @return the min value |
| 55 | */ |
| 56 | public long getMin() { |
| 57 | long min = Long.MAX_VALUE; |
| 58 | for (ISegment interval : fSs) { |
| 59 | min = Math.min(min, interval.getLength()); |
| 60 | } |
| 61 | return min; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Get the average value |
| 66 | * |
| 67 | * @return the average value |
| 68 | */ |
| 69 | public double getAvg() { |
| 70 | double total = 0; |
| 71 | for (ISegment interval : fSs) { |
| 72 | total += (double) interval.getLength() / (double) fSs.size(); |
| 73 | } |
| 74 | return total; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Get the standard deviation for a window. |
| 79 | * |
| 80 | * @return the standard deviation |
| 81 | */ |
| 82 | public double getStdDev() { |
| 83 | if (fSs.size() < 3) { |
| 84 | return Double.NaN; |
| 85 | } |
| 86 | double mean = getAvg(); |
| 87 | |
| 88 | double totalVariance = 0; |
| 89 | for (ISegment interval : fSs) { |
| 90 | double result = interval.getLength() - mean; |
| 91 | totalVariance += result * result / (fSs.size() - 1); |
| 92 | } |
| 93 | return Math.sqrt(totalVariance); |
| 94 | } |
| 95 | } |