TMF: Add an experiment utility class to get data from experiment's traces
authorGeneviève Bastien <gbastien+lttng@versatic.net>
Thu, 20 Nov 2014 21:46:54 +0000 (16:46 -0500)
committerGenevieve Bastien <gbastien+lttng@versatic.net>
Thu, 27 Nov 2014 21:05:18 +0000 (16:05 -0500)
Change-Id: Ifbc9e4a773bca227150f136bfcbe2a1410a91de5
Signed-off-by: Geneviève Bastien <gbastien+lttng@versatic.net>
Reviewed-on: https://git.eclipse.org/r/36822
Tested-by: Hudson CI
Reviewed-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
org.eclipse.tracecompass.tmf.core.tests/shared/org/eclipse/tracecompass/tmf/core/tests/shared/TmfTestTrace.java
org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/trace/AllTests.java
org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/trace/TmfExperimentUtilsTest.java [new file with mode: 0644]
org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/trace/experiment/TmfExperimentUtils.java [new file with mode: 0644]

index 8aef35218bb38712cc17aa019f39c12a07d48032..5a13c16fb002b3f4b1f51a60a9fae936ebb74771 100644 (file)
@@ -44,11 +44,11 @@ public enum TmfTestTrace {
     /** And oh! a fifth trace */
     R_TEST_10K("R-Test-10K");
 
-    private final String fPath;
+    private final @NonNull String fPath;
     private final String fDirectory = "../org.eclipse.tracecompass.tmf.core.tests/testfiles";
     private ITmfTrace fTrace = null;
 
-    private TmfTestTrace(String file) {
+    private TmfTestTrace(@NonNull String file) {
         fPath = file;
     }
 
@@ -57,7 +57,7 @@ public enum TmfTestTrace {
      *
      * @return The path of this trace
      */
-    public String getPath() {
+    public @NonNull String getPath() {
         return fPath;
     }
 
index 0036e07ffe56665996ba6b12ab07dfad9e79b0c4..abc62de7928d55cb9350ba777dc18d4cb0920c11 100644 (file)
@@ -24,6 +24,7 @@ import org.junit.runners.Suite;
 @Suite.SuiteClasses({
     TmfContextTest.class,
     TmfExperimentTest.class,
+    TmfExperimentUtilsTest.class,
     TmfMultiTraceExperimentTest.class,
     TmfTraceTest.class
 })
diff --git a/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/trace/TmfExperimentUtilsTest.java b/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/trace/TmfExperimentUtilsTest.java
new file mode 100644 (file)
index 0000000..ac880fb
--- /dev/null
@@ -0,0 +1,167 @@
+/*******************************************************************************
+ * Copyright (c) 2014 École Polytechnique de Montréal
+ *
+ * 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
+ *
+ * Contributors:
+ *   Geneviève Bastien - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.tracecompass.tmf.core.tests.trace;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule;
+import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager;
+import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal;
+import org.eclipse.tracecompass.tmf.core.tests.analysis.AnalysisManagerTest;
+import org.eclipse.tracecompass.tmf.core.tests.shared.TmfTestTrace;
+import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
+import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
+import org.eclipse.tracecompass.tmf.core.trace.experiment.TmfExperiment;
+import org.eclipse.tracecompass.tmf.core.trace.experiment.TmfExperimentUtils;
+import org.eclipse.tracecompass.tmf.tests.stubs.analysis.TestAnalysis;
+import org.eclipse.tracecompass.tmf.tests.stubs.analysis.TestAnalysis2;
+import org.eclipse.tracecompass.tmf.tests.stubs.trace.TmfExperimentStub;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Test the {@link TmfExperimentUtils} class
+ *
+ * @author Geneviève Bastien
+ */
+public class TmfExperimentUtilsTest {
+
+    private static final String EXPERIMENT = "MyExperiment";
+    private static int BLOCK_SIZE = 1000;
+
+    private TmfExperimentStub fExperiment;
+    private ITmfTrace[] fTraces;
+
+    /**
+     * Setup the experiment
+     */
+    @Before
+    public void setupExperiment() {
+        fTraces = new ITmfTrace[2];
+        fTraces[0] = TmfTestTrace.A_TEST_10K.getTrace();
+        fTraces[1] = TmfTestTrace.A_TEST_10K2.getTraceAsStub2();
+        /* Re-register the trace to the signal manager */
+        TmfSignalManager.register(fTraces[1]);
+        fExperiment = new TmfExperimentStub(EXPERIMENT, fTraces, BLOCK_SIZE);
+        fExperiment.getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, true);
+        fExperiment.broadcast(new TmfTraceOpenedSignal(this, fExperiment, null));
+    }
+
+    /**
+     * Cleanup after the test
+     */
+    @After
+    public void cleanUp() {
+        fExperiment.dispose();
+    }
+
+    /**
+     * Test the
+     * {@link TmfExperimentUtils#getAnalysisModuleForHost(TmfExperiment, String, String)}
+     * method
+     */
+    @Test
+    public void testGetModuleById() {
+        String commonModule = AnalysisManagerTest.MODULE_PARAM;
+        String notCommonModule = AnalysisManagerTest.MODULE_SECOND;
+        String host1 = TmfTestTrace.A_TEST_10K.getPath();
+        String host2 = TmfTestTrace.A_TEST_10K2.getPath();
+        TmfExperiment experiment = fExperiment;
+        assertNotNull(experiment);
+
+        /* First module for trace 1 */
+        IAnalysisModule module = TmfExperimentUtils.getAnalysisModuleForHost(experiment, host1, commonModule);
+        assertNotNull(module);
+        IAnalysisModule traceModule = fTraces[0].getAnalysisModule(commonModule);
+        assertNotNull(traceModule);
+        assertEquals(module, traceModule);
+
+        /* Second inexistent module for trace 1 */
+        assertNull(TmfExperimentUtils.getAnalysisModuleForHost(experiment, host1, notCommonModule));
+        traceModule = fTraces[0].getAnalysisModule(notCommonModule);
+        assertNull(traceModule);
+
+        /* First module for trace 2 */
+        module = TmfExperimentUtils.getAnalysisModuleForHost(experiment, host2, commonModule);
+        assertNotNull(module);
+        traceModule = fTraces[1].getAnalysisModule(commonModule);
+        assertNotNull(traceModule);
+        assertEquals(module, traceModule);
+
+        /* Second module for trace 2 */
+        module = TmfExperimentUtils.getAnalysisModuleForHost(experiment, host2, notCommonModule);
+        assertNotNull(module);
+        traceModule = fTraces[1].getAnalysisModule(notCommonModule);
+        assertNotNull(traceModule);
+        assertEquals(module, traceModule);
+    }
+
+    /**
+     * Test the
+     * {@link TmfExperimentUtils#getAnalysisModuleOfClassForHost(TmfExperiment, String, Class)}
+     * method
+     */
+    @Test
+    public void testGetModuleByClass() {
+        Class<TestAnalysis> commonClass = TestAnalysis.class;
+        Class<TestAnalysis2> notCommonClass = TestAnalysis2.class;
+        String host1 = TmfTestTrace.A_TEST_10K.getPath();
+        String host2 = TmfTestTrace.A_TEST_10K2.getPath();
+        TmfExperiment experiment = fExperiment;
+        assertNotNull(experiment);
+
+        /* Common module for trace 1 */
+        TestAnalysis module1 = TmfExperimentUtils.getAnalysisModuleOfClassForHost(experiment, host1, commonClass);
+        assertNotNull(module1);
+        /* Make sure this module belongs to the trace */
+        IAnalysisModule sameModule = null;
+        for (IAnalysisModule mod : fTraces[0].getAnalysisModules()) {
+            if (mod == module1) {
+                sameModule = mod;
+            }
+        }
+        assertNotNull(sameModule);
+
+        /* Uncommon module from trace 1 */
+        TestAnalysis2 module2 = TmfExperimentUtils.getAnalysisModuleOfClassForHost(experiment, host1, notCommonClass);
+        assertNull(module2);
+
+        /* Common module for trace 1 */
+        module1 = TmfExperimentUtils.getAnalysisModuleOfClassForHost(experiment, host2, commonClass);
+        assertNotNull(module1);
+        /* Make sure this module belongs to the trace */
+        sameModule = null;
+        for (IAnalysisModule mod : fTraces[1].getAnalysisModules()) {
+            if (mod == module1) {
+                sameModule = mod;
+            }
+        }
+        assertNotNull(sameModule);
+
+        /* Uncommon module from trace 1 */
+        module2 = TmfExperimentUtils.getAnalysisModuleOfClassForHost(experiment, host2, notCommonClass);
+        assertNotNull(module2);
+        /* Make sure this module belongs to the trace */
+        sameModule = null;
+        for (IAnalysisModule mod : fTraces[1].getAnalysisModules()) {
+            if (mod == module1) {
+                sameModule = mod;
+            }
+        }
+        assertNotNull(sameModule);
+    }
+
+}
diff --git a/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/trace/experiment/TmfExperimentUtils.java b/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/trace/experiment/TmfExperimentUtils.java
new file mode 100644 (file)
index 0000000..1695ad1
--- /dev/null
@@ -0,0 +1,96 @@
+/*******************************************************************************
+ * Copyright (c) 2014 École Polytechnique de Montréal
+ *
+ * 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
+ *
+ * Contributors:
+ *   Geneviève Bastien - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.tracecompass.tmf.core.trace.experiment;
+
+import java.util.Collection;
+import java.util.HashSet;
+
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
+import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule;
+import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
+
+/**
+ * This utility class contains some utility methods to retrieve specific traces
+ * or analysis in an experiment.
+ *
+ * @author Geneviève Bastien
+ */
+@NonNullByDefault
+public final class TmfExperimentUtils {
+
+    private TmfExperimentUtils() {
+
+    }
+
+    // ------------------------------------------------------------------------
+    // Utility methods for analysis modules
+    // ------------------------------------------------------------------------
+
+    private static Iterable<ITmfTrace> getTracesFromHost(TmfExperiment experiment, String hostId) {
+        Collection<ITmfTrace> hostTraces = new HashSet<>();
+        for (ITmfTrace trace : experiment.getTraces()) {
+            if (trace.getHostId().equals(hostId)) {
+                hostTraces.add(trace);
+            }
+        }
+        return hostTraces;
+    }
+
+    /**
+     * Get a specific analysis module from a specific host of an experiment. It
+     * will return the first module with the given ID from the first trace of
+     * the host that has such a module.
+     *
+     * @param experiment
+     *            The experiment the host belongs to
+     * @param hostId
+     *            The ID of the host for which we want the specified analysis
+     * @param analysisId
+     *            The ID of the requested analysis
+     * @return The requested analysis module or {@code null} if no module found
+     */
+    public static @Nullable IAnalysisModule getAnalysisModuleForHost(TmfExperiment experiment, String hostId, String analysisId) {
+        for (ITmfTrace trace : getTracesFromHost(experiment, hostId)) {
+            IAnalysisModule module = trace.getAnalysisModule(analysisId);
+            if (module != null) {
+                return module;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Get an analysis module of a specific class from a specific host of an
+     * experiment. It will return the first module of the given class from the
+     * first trace of the host that has such a module.
+     *
+     * @param experiment
+     *            The experiment the host belongs to
+     * @param hostId
+     *            The ID of the host for which we want the specified analysis
+     * @param moduleClass
+     *            The class of the analysis module to return
+     * @return The first analysis module of the given class or {@code null} if
+     *         no module found
+     */
+    public static @Nullable <T extends IAnalysisModule> T getAnalysisModuleOfClassForHost(TmfExperiment experiment, String hostId, Class<T> moduleClass) {
+        for (ITmfTrace trace : getTracesFromHost(experiment, hostId)) {
+            for (T module : trace.getAnalysisModulesOfClass(moduleClass)) {
+                return module;
+            }
+        }
+        return null;
+    }
+
+}
This page took 0.02949 seconds and 5 git commands to generate.