tmf: Automatically sync experiments set up with the same hosts
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core.tests / src / org / eclipse / tracecompass / tmf / core / tests / trace / TmfExperimentTest.java
index 1649a39655b9a071b4c9fd8b9b9f4712ffdb88d6..e0ccd766ae3e5e2e1bd3a6d694d84229e0788267 100644 (file)
@@ -25,21 +25,29 @@ import java.io.File;
 import java.io.IOException;
 import java.net.URISyntaxException;
 import java.net.URL;
+import java.util.Map;
 import java.util.Vector;
 
 import org.eclipse.core.runtime.FileLocator;
 import org.eclipse.core.runtime.Path;
+import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.tracecompass.internal.tmf.core.component.TmfProviderManager;
+import org.eclipse.tracecompass.internal.tmf.core.synchronization.SyncAlgorithmFullyIncremental;
 import org.eclipse.tracecompass.internal.tmf.core.trace.experiment.TmfExperimentContext;
 import org.eclipse.tracecompass.internal.tmf.core.trace.experiment.TmfExperimentLocation;
 import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule;
 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
 import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
+import org.eclipse.tracecompass.tmf.core.project.model.ITmfPropertiesProvider;
 import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest;
 import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest.ExecutionType;
 import org.eclipse.tracecompass.tmf.core.request.TmfEventRequest;
 import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal;
+import org.eclipse.tracecompass.tmf.core.synchronization.ITmfTimestampTransform;
+import org.eclipse.tracecompass.tmf.core.synchronization.SynchronizationAlgorithm;
+import org.eclipse.tracecompass.tmf.core.synchronization.TimestampTransformFactory;
 import org.eclipse.tracecompass.tmf.core.tests.TmfCoreTestPlugin;
+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.timestamp.TmfTimestamp;
@@ -52,10 +60,13 @@ import org.eclipse.tracecompass.tmf.core.trace.location.TmfLongLocation;
 import org.eclipse.tracecompass.tmf.tests.stubs.analysis.TestExperimentAnalysis;
 import org.eclipse.tracecompass.tmf.tests.stubs.trace.TmfExperimentStub;
 import org.eclipse.tracecompass.tmf.tests.stubs.trace.TmfTraceStub;
+import org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.TmfXmlTraceStub;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
+import com.google.common.collect.ImmutableMap;
+
 /**
  * Test suite for the TmfExperiment class (single trace).
  */
@@ -220,6 +231,45 @@ public class TmfExperimentTest {
         Iterable<TestExperimentAnalysis> testModules = TmfTraceUtils.getAnalysisModulesOfClass(experiment, TestExperimentAnalysis.class);
         assertTrue(modules.iterator().hasNext());
         assertTrue(testModules.iterator().hasNext());
+
+        /*
+         * Test that a module that applies to one of its trace is present in an
+         * experiment
+         */
+        ITmfTrace trace1 = TmfTestTrace.A_TEST_10K.getTrace();
+        ITmfTrace trace2 = TmfTestTrace.A_TEST_10K2.getTrace();
+        ITmfTrace trace3 = TmfTestTrace.A_TEST_10K2.getTraceAsStub2();
+
+        /*
+         * Create an experiment with TmfTraceStub, the module other should not
+         * be there
+         */
+        ITmfTrace[] tracesExp1 = { trace1, trace2 };
+        TmfExperiment exp1 = new TmfExperiment(tracesExp1[0].getEventType(), "Experiment 1", tracesExp1, TmfExperiment.DEFAULT_INDEX_PAGE_SIZE, null);
+
+        /*
+         * Create an experiment containing some TmfTraceStub2, the module other
+         * should be present
+         */
+        ITmfTrace[] tracesExp2 = { trace1, trace3 };
+        TmfExperiment exp2 = new TmfExperiment(tracesExp2[0].getEventType(), "Experiment 1", tracesExp2, TmfExperiment.DEFAULT_INDEX_PAGE_SIZE, null);
+
+        try {
+            /* Open the experiment, the modules should be populated */
+            exp1.traceOpened(new TmfTraceOpenedSignal(this, exp1, null));
+            assertNull(exp1.getAnalysisModule(AnalysisManagerTest.MODULE_SECOND));
+
+            /* Open the experiment, the modules should be populated */
+            exp2.traceOpened(new TmfTraceOpenedSignal(this, exp2, null));
+            assertNotNull(exp2.getAnalysisModule(AnalysisManagerTest.MODULE_SECOND));
+        } finally {
+            trace1.dispose();
+            trace2.dispose();
+            trace3.dispose();
+            exp1.dispose();
+            exp2.dispose();
+        }
+
     }
 
     // ------------------------------------------------------------------------
@@ -956,4 +1006,134 @@ public class TmfExperimentTest {
         assertTrue("isCancelled", request.isCancelled());
     }
 
+    private static abstract class TestTrace extends TmfXmlTraceStub implements ITmfPropertiesProvider {
+
+    }
+
+    /**
+     * Tests that experiment with traces from the same host and a clock offset
+     * are well synchronized
+     */
+    @Test
+    public void testWithSingleHostClockOffset() {
+        // Data for this specific test
+        String hostId = "Test Host 1";
+        long minOffset = 2000;
+        long offset = 1000;
+        String clockOffset = "clock_offset";
+
+        ITmfTrace t1 = new TestTrace() {
+            @Override
+            public @NonNull String getHostId() {
+                return hostId;
+            }
+            @Override
+            public @NonNull Map<@NonNull String, @NonNull String> getProperties() {
+                return ImmutableMap.of(clockOffset, String.valueOf(minOffset));
+            }
+        };
+
+        ITmfTrace t2 = new TestTrace() {
+            @Override
+            public @NonNull String getHostId() {
+                return hostId;
+            }
+            @Override
+            public @NonNull Map<@NonNull String, @NonNull String> getProperties() {
+                return ImmutableMap.of(clockOffset, String.valueOf(minOffset + offset));
+            }
+        };
+
+        TmfExperiment exp = new TmfExperimentStub(EXPERIMENT, new ITmfTrace[] { t1, t2 }, BLOCK_SIZE);
+
+        try {
+            assertEquals(TimestampTransformFactory.createWithOffset(offset / 2), t1.getTimestampTransform());
+            assertEquals(TimestampTransformFactory.createWithOffset(-offset / 2), t2.getTimestampTransform());
+
+        } finally {
+            exp.dispose();
+        }
+    }
+
+    /**
+     * Tests that opening an experiment whose traces already have a
+     * synchronization formula will not eliminate that formula. This test makes
+     * the supposition that the experiment was synchronized and the
+     * synchronization added the clock offset correction to the total formula.
+     */
+    @Test
+    public void testWithMultiHostClockOffset() {
+        // Data for this specific test
+        String hostId = "Test Host 1";
+        String hostId2 = "Test Host 2";
+        long minOffset = 2000;
+        long offset = 1000;
+        String clockOffset = "clock_offset";
+
+        ITmfTimestampTransform tt1 = TimestampTransformFactory.createLinear(2.0, offset / 2);
+        ITmfTimestampTransform tt2 = TimestampTransformFactory.createLinear(2.0, -offset / 2);
+        ITmfTimestampTransform tt3 = TimestampTransformFactory.createWithOffset(offset);
+
+        ITmfTrace t1 = new TestTrace() {
+            @Override
+            public @NonNull String getHostId() {
+                return hostId;
+            }
+            @Override
+            public @NonNull Map<@NonNull String, @NonNull String> getProperties() {
+                return ImmutableMap.of(clockOffset, String.valueOf(minOffset));
+            }
+
+        };
+        t1.setTimestampTransform(tt1);
+
+        ITmfTrace t2 = new TestTrace() {
+            @Override
+            public @NonNull String getHostId() {
+                return hostId;
+            }
+            @Override
+            public @NonNull Map<@NonNull String, @NonNull String> getProperties() {
+                return ImmutableMap.of(clockOffset, String.valueOf(minOffset + offset));
+            }
+        };
+        t2.setTimestampTransform(tt2);
+
+        ITmfTrace t3 = new TmfXmlTraceStub() {
+            @Override
+            public @NonNull String getHostId() {
+                return hostId2;
+            }
+        };
+        t3.setTimestampTransform(tt3);
+
+        TmfExperiment exp = new TmfExperimentStub(EXPERIMENT, new ITmfTrace[] { t1, t2, t3 }, BLOCK_SIZE) {
+
+            @Override
+            public SynchronizationAlgorithm synchronizeTraces() {
+                return new SyncAlgorithmFullyIncremental() {
+
+                    private static final long serialVersionUID = 4206172498287480153L;
+
+                    @Override
+                    public ITmfTimestampTransform getTimestampTransform(String h) {
+                        if (hostId.equals(h)) {
+                            return TimestampTransformFactory.createLinear(2.0, 0);
+                        }
+                        return tt3;
+                    }
+                };
+            }
+        };
+
+        try {
+            assertEquals(tt1, t1.getTimestampTransform());
+            assertEquals(tt2, t2.getTimestampTransform());
+            assertEquals(tt3, t3.getTimestampTransform());
+
+        } finally {
+            exp.dispose();
+        }
+    }
+
 }
This page took 0.040456 seconds and 5 git commands to generate.