From: Matthew Khouzam Date: Wed, 2 Dec 2015 23:46:04 +0000 (-0500) Subject: analysis: add statistics core tests for segments X-Git-Url: http://git.efficios.com/?a=commitdiff_plain;h=e06c9955f2c9edc677500095eddeb97ad2aeb32e;p=deliverable%2Ftracecompass.git analysis: add statistics core tests for segments Change-Id: Ie2ac17401f7f1e155c694ad26fd8ea2cb9ce792e Signed-off-by: Matthew Khouzam Reviewed-on: https://git.eclipse.org/r/61835 Reviewed-by: Bernd Hufmann Tested-by: Bernd Hufmann Reviewed-by: Hudson CI --- diff --git a/analysis/org.eclipse.tracecompass.analysis.os.linux.core.tests/META-INF/MANIFEST.MF b/analysis/org.eclipse.tracecompass.analysis.os.linux.core.tests/META-INF/MANIFEST.MF index 4d8910dc91..89d0d6cea3 100644 --- a/analysis/org.eclipse.tracecompass.analysis.os.linux.core.tests/META-INF/MANIFEST.MF +++ b/analysis/org.eclipse.tracecompass.analysis.os.linux.core.tests/META-INF/MANIFEST.MF @@ -11,11 +11,13 @@ Require-Bundle: org.junit;bundle-version="4.0.0", org.eclipse.core.runtime, org.eclipse.tracecompass.common.core, org.eclipse.tracecompass.analysis.os.linux.core, + org.eclipse.tracecompass.segmentstore.core, org.eclipse.tracecompass.tmf.core, org.eclipse.tracecompass.tmf.core.tests, org.eclipse.tracecompass.tmf.ctf.core, org.eclipse.tracecompass.tmf.ctf.core.tests, - org.eclipse.core.resources + org.eclipse.core.resources, + org.eclipse.tracecompass.analysis.timing.core;bundle-version="1.0.0" Import-Package: com.google.common.collect, org.eclipse.tracecompass.testtraces.ctf;version="1.0.0" Export-Package: org.eclipse.tracecompass.analysis.os.linux.core.tests, diff --git a/analysis/org.eclipse.tracecompass.analysis.os.linux.core.tests/src/org/eclipse/tracecompass/analysis/os/linux/core/tests/AllTests.java b/analysis/org.eclipse.tracecompass.analysis.os.linux.core.tests/src/org/eclipse/tracecompass/analysis/os/linux/core/tests/AllTests.java index 9d353ae022..96fd24ac5f 100644 --- a/analysis/org.eclipse.tracecompass.analysis.os.linux.core.tests/src/org/eclipse/tracecompass/analysis/os/linux/core/tests/AllTests.java +++ b/analysis/org.eclipse.tracecompass.analysis.os.linux.core.tests/src/org/eclipse/tracecompass/analysis/os/linux/core/tests/AllTests.java @@ -21,7 +21,8 @@ import org.junit.runner.RunWith; @RunWith(DebugSuite.class) @DebugSuite.SuiteClasses({ org.eclipse.tracecompass.analysis.os.linux.core.tests.cpuusage.AllTests.class, - org.eclipse.tracecompass.analysis.os.linux.core.tests.kernelanalysis.AllTests.class + org.eclipse.tracecompass.analysis.os.linux.core.tests.kernelanalysis.AllTests.class, + org.eclipse.tracecompass.analysis.os.linux.core.tests.latency.AllTests.class }) public class AllTests { diff --git a/analysis/org.eclipse.tracecompass.analysis.os.linux.core.tests/src/org/eclipse/tracecompass/analysis/os/linux/core/tests/latency/AllTests.java b/analysis/org.eclipse.tracecompass.analysis.os.linux.core.tests/src/org/eclipse/tracecompass/analysis/os/linux/core/tests/latency/AllTests.java new file mode 100644 index 0000000000..1dcf13eabb --- /dev/null +++ b/analysis/org.eclipse.tracecompass.analysis.os.linux.core.tests/src/org/eclipse/tracecompass/analysis/os/linux/core/tests/latency/AllTests.java @@ -0,0 +1,24 @@ +/******************************************************************************* + * Copyright (c) 2015 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.os.linux.core.tests.latency; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +/** + * Test suite for the Latency packages + */ +@RunWith(Suite.class) +@Suite.SuiteClasses({ + SegmentStoreStatisticsTest.class +}) +public class AllTests { + +} \ No newline at end of file diff --git a/analysis/org.eclipse.tracecompass.analysis.os.linux.core.tests/src/org/eclipse/tracecompass/analysis/os/linux/core/tests/latency/OfflineStatisticsCalculator.java b/analysis/org.eclipse.tracecompass.analysis.os.linux.core.tests/src/org/eclipse/tracecompass/analysis/os/linux/core/tests/latency/OfflineStatisticsCalculator.java new file mode 100644 index 0000000000..6ffcac3b53 --- /dev/null +++ b/analysis/org.eclipse.tracecompass.analysis.os.linux.core.tests/src/org/eclipse/tracecompass/analysis/os/linux/core/tests/latency/OfflineStatisticsCalculator.java @@ -0,0 +1,95 @@ +/******************************************************************************* + * Copyright (c) 2015 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.os.linux.core.tests.latency; + +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); + } +} diff --git a/analysis/org.eclipse.tracecompass.analysis.os.linux.core.tests/src/org/eclipse/tracecompass/analysis/os/linux/core/tests/latency/SegmentStoreStatisticsTest.java b/analysis/org.eclipse.tracecompass.analysis.os.linux.core.tests/src/org/eclipse/tracecompass/analysis/os/linux/core/tests/latency/SegmentStoreStatisticsTest.java new file mode 100644 index 0000000000..12ae65c95c --- /dev/null +++ b/analysis/org.eclipse.tracecompass.analysis.os.linux.core.tests/src/org/eclipse/tracecompass/analysis/os/linux/core/tests/latency/SegmentStoreStatisticsTest.java @@ -0,0 +1,156 @@ +/******************************************************************************* + * Copyright (c) 2015 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.os.linux.core.tests.latency; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Random; + +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.tracecompass.analysis.os.linux.core.latency.SystemCall; +import org.eclipse.tracecompass.analysis.os.linux.core.latency.SystemCall.InitialInfo; +import org.eclipse.tracecompass.internal.analysis.os.linux.core.latency.statistics.SegmentStoreStatistics; +import org.eclipse.tracecompass.segmentstore.core.ISegment; +import org.junit.Test; + +/** + * Test the staticsmodule. This is done with two tests. + *
    + *
  1. test the values vs some sample points calculated by hand (sanity test) + *
  2. + *
  3. 2- test exhaustively vs a reference implementation.
  4. + *
+ * + * @author Matthew Khouzam + * + */ +public class SegmentStoreStatisticsTest { + + private static final int MEDIUM_AMOUNT_OF_SEGMENTS = 100; + private static final int LARGE_AMOUNT_OF_SEGMENTS = 1000000; + + private static final double NO_ERROR = 0.0; + private static final double ERROR = 0.000001; + + private static void testOnlineVsOffline(List<@NonNull SystemCall> fixture) { + SegmentStoreStatistics sss = getSegStoreStat(fixture); + OfflineStatisticsCalculator osc = new OfflineStatisticsCalculator(fixture); + assertEquals("Average", osc.getAvg(), sss.getAverage(), ERROR); + assertEquals("Standard Deviation", osc.getStdDev(), sss.getStdDev(), ERROR); + assertEquals("Min", osc.getMin(), sss.getMin()); + assertEquals("Max", osc.getMax(), sss.getMax()); + } + + /** + * Test incrementing + */ + @Test + public void climbTest() { + List<@NonNull SystemCall> fixture = new ArrayList<>(); + for (int i = 0; i < MEDIUM_AMOUNT_OF_SEGMENTS; i++) { + fixture.add(createAnonSyscall(i, i * 2)); + } + SegmentStoreStatistics sss = getSegStoreStat(fixture); + assertEquals("Average", 49.5, sss.getAverage(), ERROR); + assertEquals("Min", 0, sss.getMin()); + assertEquals("Max", 99, sss.getMax()); + assertEquals("Standard Deviation", 29.0, sss.getStdDev(), 0.02); + testOnlineVsOffline(fixture); + } + + private static SegmentStoreStatistics getSegStoreStat(List<@NonNull SystemCall> fixture) { + SegmentStoreStatistics sss = new SegmentStoreStatistics(); + for (ISegment seg : fixture) { + sss.update(seg); + } + return sss; + } + + /** + * Test decrementing + */ + @Test + public void decrementingTest() { + List<@NonNull SystemCall> fixture = new ArrayList<>(); + for (int i = MEDIUM_AMOUNT_OF_SEGMENTS; i >= 0; i--) { + fixture.add(createAnonSyscall(i, i * 2)); + } + SegmentStoreStatistics sss = getSegStoreStat(fixture); + assertEquals("Average", 50, sss.getAverage(), NO_ERROR); + assertEquals("Min", 0, sss.getMin()); + assertEquals("Max", 100, sss.getMax()); + assertEquals("Standard Deviation", 29.3, sss.getStdDev(), 0.01); + testOnlineVsOffline(fixture); + } + + /** + * Test small + */ + @Test + public void smallTest() { + List<@NonNull SystemCall> fixture = new ArrayList<>(); + for (int i = 1; i >= 0; i--) { + fixture.add(createAnonSyscall(i, i * 2)); + } + testOnlineVsOffline(fixture); + } + + /** + * Test large + */ + @Test + public void largeTest() { + List<@NonNull SystemCall> fixture = new ArrayList<>(); + for (int i = 1; i <= LARGE_AMOUNT_OF_SEGMENTS; i++) { + fixture.add(createAnonSyscall(i, i * 2)); + } + testOnlineVsOffline(fixture); + } + + /** + * Test noise + */ + @Test + public void noiseTest() { + Random rnd = new Random(); + rnd.setSeed(1234); + List<@NonNull SystemCall> fixture = new ArrayList<>(); + for (int i = 1; i <= LARGE_AMOUNT_OF_SEGMENTS; i++) { + int start = Math.abs(rnd.nextInt(100000000)); + int end = start + Math.abs(rnd.nextInt(1000000)); + fixture.add(createAnonSyscall(start, end)); + } + testOnlineVsOffline(fixture); + } + + /** + * Test gaussian noise + */ + @Test + public void gaussianNoiseTest() { + Random rnd = new Random(); + rnd.setSeed(1234); + List<@NonNull SystemCall> fixture = new ArrayList<>(); + for (int i = 1; i <= LARGE_AMOUNT_OF_SEGMENTS; i++) { + int start = Math.abs(rnd.nextInt(100000000)); + final int delta = Math.abs(rnd.nextInt(1000)); + int end = start + delta * delta; + fixture.add(createAnonSyscall(start, end)); + } + testOnlineVsOffline(fixture); + } + + private static @NonNull SystemCall createAnonSyscall(int start, int end) { + return new SystemCall(new InitialInfo(start, "", Collections.EMPTY_MAP), end, 0); + } +} diff --git a/analysis/org.eclipse.tracecompass.analysis.os.linux.core/META-INF/MANIFEST.MF b/analysis/org.eclipse.tracecompass.analysis.os.linux.core/META-INF/MANIFEST.MF index e55aee744f..0a9a26759f 100644 --- a/analysis/org.eclipse.tracecompass.analysis.os.linux.core/META-INF/MANIFEST.MF +++ b/analysis/org.eclipse.tracecompass.analysis.os.linux.core/META-INF/MANIFEST.MF @@ -26,4 +26,4 @@ Export-Package: org.eclipse.tracecompass.analysis.os.linux.core.cpuusage, org.eclipse.tracecompass.internal.analysis.os.linux.core;x-internal:=true, org.eclipse.tracecompass.internal.analysis.os.linux.core.kernel;x-friends:="org.eclipse.tracecompass.analysis.os.linux.core.tests", org.eclipse.tracecompass.internal.analysis.os.linux.core.kernel.handlers;x-friends:="org.eclipse.tracecompass.analysis.os.linux.core.tests", - org.eclipse.tracecompass.internal.analysis.os.linux.core.latency.statistics;x-friends:="org.eclipse.tracecompass.analysis.os.linux.ui" + org.eclipse.tracecompass.internal.analysis.os.linux.core.latency.statistics;x-friends:="org.eclipse.tracecompass.analysis.os.linux.ui,org.eclipse.tracecompass.analysis.os.linux.core.tests"