Bug 483621: Move segment store statistics into timing analysis plug-ins
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.timing.core.tests / src / org / eclipse / tracecompass / analysis / timing / core / tests / segmentstore / statistics / OfflineStatisticsCalculator.java
diff --git a/analysis/org.eclipse.tracecompass.analysis.timing.core.tests/src/org/eclipse/tracecompass/analysis/timing/core/tests/segmentstore/statistics/OfflineStatisticsCalculator.java b/analysis/org.eclipse.tracecompass.analysis.timing.core.tests/src/org/eclipse/tracecompass/analysis/timing/core/tests/segmentstore/statistics/OfflineStatisticsCalculator.java
new file mode 100644 (file)
index 0000000..fe64a6e
--- /dev/null
@@ -0,0 +1,95 @@
+/*******************************************************************************
+ * Copyright (c) 2016 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+
+package org.eclipse.tracecompass.analysis.timing.core.tests.segmentstore.statistics;
+
+import java.util.Collection;
+
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.tracecompass.analysis.os.linux.core.latency.SystemCall;
+import org.eclipse.tracecompass.segmentstore.core.ISegment;
+
+/**
+ * This calculates the statistics of a segment store in an offline manner to
+ * validate online calculations.
+ *
+ * @author Matthew Khouzam
+ *
+ */
+public class OfflineStatisticsCalculator {
+    private final Collection<@NonNull SystemCall> fSs;
+
+    /**
+     * Constructor
+     *
+     * @param ss
+     *            segment store, fully build
+     */
+    public OfflineStatisticsCalculator(Collection<@NonNull SystemCall> ss) {
+        fSs = ss;
+    }
+
+    /**
+     * Get the max value
+     *
+     * @return the max value
+     */
+    public long getMax() {
+        long max = Long.MIN_VALUE;
+        for (ISegment interval : fSs) {
+            max = Math.max(max, interval.getLength());
+        }
+        return max;
+    }
+
+    /**
+     * Get the min value
+     *
+     * @return the min value
+     */
+    public long getMin() {
+        long min = Long.MAX_VALUE;
+        for (ISegment interval : fSs) {
+            min = Math.min(min, interval.getLength());
+        }
+        return min;
+    }
+
+    /**
+     * Get the average value
+     *
+     * @return the average value
+     */
+    public double getAvg() {
+        double total = 0;
+        for (ISegment interval : fSs) {
+            total += (double) interval.getLength() / (double) fSs.size();
+        }
+        return total;
+    }
+
+    /**
+     * Get the standard deviation for a window.
+     *
+     * @return the standard deviation
+     */
+    public double getStdDev() {
+        if (fSs.size() < 3) {
+            return Double.NaN;
+        }
+        double mean = getAvg();
+
+        double totalVariance = 0;
+        for (ISegment interval : fSs) {
+            double result = interval.getLength() - mean;
+            totalVariance += result * result / (fSs.size() - 1);
+        }
+        return Math.sqrt(totalVariance);
+    }
+}
This page took 0.025731 seconds and 5 git commands to generate.