package org.eclipse.tracecompass.tmf.core.tests.synchronization;
import static org.junit.Assert.assertEquals;
-
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
import java.util.Collection;
import java.util.LinkedList;
+import org.eclipse.tracecompass.internal.tmf.core.synchronization.SyncAlgorithmFullyIncremental;
import org.eclipse.tracecompass.tmf.core.event.matching.TmfEventDependency;
import org.eclipse.tracecompass.tmf.core.synchronization.ITmfTimestampTransform;
import org.eclipse.tracecompass.tmf.core.synchronization.SynchronizationAlgorithm;
+import org.eclipse.tracecompass.tmf.core.synchronization.SynchronizationAlgorithm.SyncQuality;
import org.eclipse.tracecompass.tmf.core.synchronization.SynchronizationAlgorithmFactory;
import org.eclipse.tracecompass.tmf.core.synchronization.TimestampTransformFactory;
-import org.eclipse.tracecompass.tmf.core.synchronization.SynchronizationAlgorithm.SyncQuality;
import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
import org.eclipse.tracecompass.tmf.tests.stubs.event.TmfSyncEventStub;
syncAlgo.init(fTraces);
assertEquals(SyncQuality.ABSENT, syncAlgo.getSynchronizationQuality(t1, t2));
- syncAlgo.addMatch(
- new TmfEventDependency(new TmfSyncEventStub(t2, new TmfTimestamp(1)),
- new TmfSyncEventStub(t1, new TmfTimestamp(1))
- ));
+ addSyncMatch(syncAlgo, t2, 1, t1, 1);
assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1 beta 0 ]]", syncAlgo.toString());
assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
- syncAlgo.addMatch(
- new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(1)),
- new TmfSyncEventStub(t2, new TmfTimestamp(3))
- ));
+ addSyncMatch(syncAlgo, t1, 1, t2, 3);
assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1 beta 0 ]]", syncAlgo.toString());
assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
- syncAlgo.addMatch(
- new TmfEventDependency(new TmfSyncEventStub(t2, new TmfTimestamp(2)),
- new TmfSyncEventStub(t1, new TmfTimestamp(3))
- ));
+ addSyncMatch(syncAlgo, t2, 2, t1, 3);
assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1 beta 0.5 ]]", syncAlgo.toString());
assertEquals(SyncQuality.APPROXIMATE, syncAlgo.getSynchronizationQuality(t1, t2));
- syncAlgo.addMatch(
- new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(3)),
- new TmfSyncEventStub(t2, new TmfTimestamp(5))
- ));
+ addSyncMatch(syncAlgo, t1, 3, t2, 5);
assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 0.75 beta 1.25 ]]", syncAlgo.toString());
assertEquals(SyncQuality.ACCURATE, syncAlgo.getSynchronizationQuality(t1, t2));
- syncAlgo.addMatch(
- new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(4)),
- new TmfSyncEventStub(t2, new TmfTimestamp(8))
- ));
+ addSyncMatch(syncAlgo, t1, 4, t2, 8);
assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 0.75 beta 1.25 ]]", syncAlgo.toString());
assertEquals(SyncQuality.ACCURATE, syncAlgo.getSynchronizationQuality(t1, t2));
- syncAlgo.addMatch(
- new TmfEventDependency(new TmfSyncEventStub(t2, new TmfTimestamp(4)),
- new TmfSyncEventStub(t1, new TmfTimestamp(5))
- ));
+ addSyncMatch(syncAlgo, t2, 4, t1, 5);
assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1.125 beta 0.875 ]]", syncAlgo.toString());
assertEquals(SyncQuality.ACCURATE, syncAlgo.getSynchronizationQuality(t1, t2));
- syncAlgo.addMatch(
- new TmfEventDependency(new TmfSyncEventStub(t2, new TmfTimestamp(4)),
- new TmfSyncEventStub(t1, new TmfTimestamp(6))
- ));
+ addSyncMatch(syncAlgo, t2, 4, t1, 6);
assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1.125 beta 0.875 ]]", syncAlgo.toString());
assertEquals(SyncQuality.ACCURATE, syncAlgo.getSynchronizationQuality(t1, t2));
- syncAlgo.addMatch(
- new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(6)),
- new TmfSyncEventStub(t2, new TmfTimestamp(7))
- ));
+ addSyncMatch(syncAlgo, t1, 6, t2, 7);
assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 0.725 beta 1.275 ]]", syncAlgo.toString());
assertEquals(SyncQuality.ACCURATE, syncAlgo.getSynchronizationQuality(t1, t2));
assertEquals(syncAlgo.getTimestampTransform(t2.getHostId()), tt2);
/* Make the two hulls intersect */
- syncAlgo.addMatch(
- new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(7)),
- new TmfSyncEventStub(t2, new TmfTimestamp(4))
- ));
- syncAlgo.addMatch(
- new TmfEventDependency(new TmfSyncEventStub(t2, new TmfTimestamp(7)),
- new TmfSyncEventStub(t1, new TmfTimestamp(3))
- ));
+ addSyncMatch(syncAlgo, t1, 7, t2, 4);
+ addSyncMatch(syncAlgo, t2, 7, t1, 3);
assertEquals(SyncQuality.FAIL, syncAlgo.getSynchronizationQuality(t1, t2));
}
assertEquals(SyncQuality.ABSENT, syncAlgo.getSynchronizationQuality(t1, t2));
- syncAlgo.addMatch(
- new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(1)),
- new TmfSyncEventStub(t2, new TmfTimestamp(3)))
- );
+ addSyncMatch(syncAlgo, t1, 1, t2, 3);
assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
- syncAlgo.addMatch(
- new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(2)),
- new TmfSyncEventStub(t2, new TmfTimestamp(5)))
- );
-
+ addSyncMatch(syncAlgo, t1, 2, t2, 5);
assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
- syncAlgo.addMatch(
- new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(3)),
- new TmfSyncEventStub(t2, new TmfTimestamp(5)))
- );
+ addSyncMatch(syncAlgo, t1, 3, t2, 5);
assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
- syncAlgo.addMatch(
- new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(4)),
- new TmfSyncEventStub(t2, new TmfTimestamp(7)))
- );
+ addSyncMatch(syncAlgo, t1, 4, t2, 7);
assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1 beta 0 ]]", syncAlgo.toString());
assertEquals(SyncQuality.ABSENT, syncAlgo.getSynchronizationQuality(t1, t2));
- syncAlgo.addMatch(
- new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(1)),
- new TmfSyncEventStub(t2, new TmfTimestamp(3)))
- );
+ addSyncMatch(syncAlgo, t1, 1, t2, 3);
assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
- syncAlgo.addMatch(
- new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(2)),
- new TmfSyncEventStub(t2, new TmfTimestamp(5)))
- );
-
+ addSyncMatch(syncAlgo, t1, 2, t2, 5);
assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
- syncAlgo.addMatch(
- new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(3)),
- new TmfSyncEventStub(t2, new TmfTimestamp(5)))
- );
+ addSyncMatch(syncAlgo, t1, 3, t2, 5);
assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
- syncAlgo.addMatch(
- new TmfEventDependency(new TmfSyncEventStub(t1, new TmfTimestamp(4)),
- new TmfSyncEventStub(t2, new TmfTimestamp(7)))
- );
+ addSyncMatch(syncAlgo, t1, 4, t2, 7);
assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1 beta 0 ]]", syncAlgo.toString());
- syncAlgo.addMatch(
- new TmfEventDependency(new TmfSyncEventStub(t2, new TmfTimestamp(7)),
- new TmfSyncEventStub(t1, new TmfTimestamp(6)))
- );
+ addSyncMatch(syncAlgo, t2, 7, t1, 6);
assertEquals(SyncQuality.APPROXIMATE, syncAlgo.getSynchronizationQuality(t1, t2));
- syncAlgo.addMatch(
- new TmfEventDependency(new TmfSyncEventStub(t2, new TmfTimestamp(8)),
- new TmfSyncEventStub(t1, new TmfTimestamp(6)))
- );
+ addSyncMatch(syncAlgo, t2, 8, t1, 6);
assertEquals(SyncQuality.APPROXIMATE, syncAlgo.getSynchronizationQuality(t1, t2));
- syncAlgo.addMatch(
- new TmfEventDependency(new TmfSyncEventStub(t2, new TmfTimestamp(10)),
- new TmfSyncEventStub(t1, new TmfTimestamp(8)))
- );
+ addSyncMatch(syncAlgo, t2, 10, t1, 8);
assertEquals(SyncQuality.APPROXIMATE, syncAlgo.getSynchronizationQuality(t1, t2));
assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1 beta 2.5 ]]", syncAlgo.toString());
}
+
+ private static void addSyncMatch(SynchronizationAlgorithm algo, ITmfTrace sender, long sendTs, ITmfTrace receiver, long receiveTs) {
+ algo.addMatch(
+ new TmfEventDependency(
+ new TmfSyncEventStub(sender, new TmfTimestamp(sendTs)),
+ new TmfSyncEventStub(receiver, new TmfTimestamp(receiveTs))
+ ));
+ }
+
+ /**
+ * Testing the serialization of the fully incremental synchronization
+ * algorithm
+ */
+ @Test
+ public void testFullyIncrementalSerialization() {
+
+ /* Do a run of synchronization and check the results */
+ SynchronizationAlgorithm syncAlgo = SynchronizationAlgorithmFactory.getFullyIncrementalAlgorithm();
+
+ syncAlgo.init(fTraces);
+
+ addSyncMatch(syncAlgo, t2, 1, t1, 1);
+ addSyncMatch(syncAlgo, t1, 1, t2, 3);
+ addSyncMatch(syncAlgo, t2, 2, t1, 3);
+ addSyncMatch(syncAlgo, t1, 3, t2, 5);
+ addSyncMatch(syncAlgo, t1, 4, t2, 8);
+ addSyncMatch(syncAlgo, t2, 4, t1, 5);
+ addSyncMatch(syncAlgo, t2, 4, t1, 6);
+ addSyncMatch(syncAlgo, t1, 6, t2, 7);
+
+ ITmfTimestampTransform tt2 = syncAlgo.getTimestampTransform(t2);
+ ITmfTimestampTransform tt1 = syncAlgo.getTimestampTransform(t1);
+
+ assertEquals(SyncQuality.ACCURATE, syncAlgo.getSynchronizationQuality(t1, t2));
+ assertEquals(syncAlgo.getTimestampTransform(t1.getHostId()), tt1);
+ assertEquals(TimestampTransformFactory.getDefaultTransform(), tt1);
+ assertEquals(syncAlgo.getTimestampTransform(t2.getHostId()), tt2);
+
+ /* Serialize the object */
+ String filePath = null;
+ try {
+ File temp = File.createTempFile("serialSyncAlgo", ".tmp");
+ filePath = temp.getAbsolutePath();
+ } catch (IOException e) {
+ fail("Could not create temporary file for serialization");
+ }
+ assertNotNull(filePath);
+
+ try (FileOutputStream fileOut = new FileOutputStream(filePath);
+ ObjectOutputStream out = new ObjectOutputStream(fileOut);) {
+ out.writeObject(syncAlgo);
+
+ } catch (IOException e) {
+ fail("Error serializing the synchronization algorithm " + e.getMessage());
+ }
+
+ SynchronizationAlgorithm deserialAlgo = null;
+ /* De-Serialize the object */
+ try (FileInputStream fileIn = new FileInputStream(filePath);
+ ObjectInputStream in = new ObjectInputStream(fileIn);) {
+ deserialAlgo = (SynchronizationAlgorithm) in.readObject();
+ } catch (IOException | ClassNotFoundException e) {
+ fail("Error de-serializing the synchronization algorithm " + e.getMessage());
+ }
+
+ /* Check that the deserialize algorithm is equivalent to original */
+ assertNotNull(deserialAlgo);
+ assertTrue(deserialAlgo instanceof SyncAlgorithmFullyIncremental);
+ assertEquals(SyncQuality.ACCURATE, deserialAlgo.getSynchronizationQuality(t1, t2));
+ assertEquals(tt1, deserialAlgo.getTimestampTransform(t1));
+ assertEquals(tt2, deserialAlgo.getTimestampTransform(t2));
+
+ }
+
}
package org.eclipse.tracecompass.internal.tmf.core.synchronization;
import java.io.IOException;
-import java.io.ObjectOutputStream;
+import java.io.ObjectInputStream;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.MathContext;
/** @Serial */
private final List<ConvexHull> fSyncs;
- private SyncSpanningTree fTree = null;
+ private transient SyncSpanningTree fTree = null;
/**
* Initialization of the attributes
private static final long serialVersionUID = 8309351175030935291L;
+ private final String fReferenceHost;
+ private final String fOtherHost;
+
+ /**
+ * Slopes and ordinate at origin of respectively fLmin, fLmax and the
+ * bisector
+ */
+ private BigDecimal fAlphamin, fBetamax, fAlphamax, fBetamin, fAlpha, fBeta;
+ private int fNbMatches, fNbAccurateMatches;
+ private SyncQuality fQuality;
+
/**
* The list of meaningful points on the upper hull (received by the
* reference trace, below in a graph)
*/
- private final LinkedList<SyncPoint> fUpperBoundList = new LinkedList<>();
-
+ private transient LinkedList<SyncPoint> fUpperBoundList = new LinkedList<>();
/**
* The list of meaninful points on the lower hull (sent by the reference
* trace, above in a graph)
*/
- private final LinkedList<SyncPoint> fLowerBoundList = new LinkedList<>();
+ private transient LinkedList<SyncPoint> fLowerBoundList = new LinkedList<>();
/** Points forming the line with maximum slope */
- private final SyncPoint[] fLmax;
-
+ private transient SyncPoint[] fLmax = new SyncPoint[2];
/** Points forming the line with minimum slope */
- private final SyncPoint[] fLmin;
-
- /**
- * Slopes and ordinate at origin of respectively fLmin, fLmax and the
- * bisector
- */
- private BigDecimal fAlphamin, fBetamax, fAlphamax, fBetamin, fAlpha, fBeta;
-
- private int fNbMatches, fNbAccurateMatches;
- private String fReferenceHost = "", fOtherHost = ""; //$NON-NLS-1$//$NON-NLS-2$
- private SyncQuality fQuality;
+ private transient SyncPoint[] fLmin = new SyncPoint[2];
- private Map<String, Object> fStats = new LinkedHashMap<>();
+ private transient Map<String, Object> fStats = new LinkedHashMap<>();
/**
* Initialization of the attributes
fReferenceHost = host1;
fOtherHost = host2;
}
- fLmax = new SyncPoint[2];
- fLmin = new SyncPoint[2];
fAlpha = BigDecimal.ONE;
fAlphamax = BigDecimal.ONE;
fAlphamin = BigDecimal.ONE;
return ((fReferenceHost.equals(hostId1) && fOtherHost.equals(hostId2)) || (fReferenceHost.equals(hostId2) && fOtherHost.equals(hostId1)));
}
- private void writeObject(ObjectOutputStream s)
- throws IOException {
- /*
- * Remove calculation data because most of it is not serializable.
- * We have the statistics anyway
- */
- fUpperBoundList.clear();
- fLowerBoundList.clear();
- fLmin[0] = null;
- fLmin[1] = null;
- fLmax[0] = null;
- fLmax[1] = null;
- s.defaultWriteObject();
+ private void readObject(ObjectInputStream stream)
+ throws IOException, ClassNotFoundException {
+ stream.defaultReadObject();
+
+ /* Initialize transient fields */
+ fUpperBoundList = new LinkedList<>();
+ fLowerBoundList = new LinkedList<>();
+ fLmax = new SyncPoint[2];
+ fLmin = new SyncPoint[2];
+ fStats = new LinkedHashMap<>();
}
@SuppressWarnings("nls")
}
}
- private void writeObject(ObjectOutputStream s)
- throws IOException {
- /*
- * Remove the tree because it is not serializable
- */
- fTree = null;
- s.defaultWriteObject();
- }
-
}