tmf.core: Introduce TmfTimestamp factory methods
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core.tests / src / org / eclipse / tracecompass / tmf / core / tests / synchronization / SyncTest.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 École Polytechnique de Montréal
3 *
4 * All rights reserved. This program and the accompanying materials are made
5 * available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Geneviève Bastien - Initial implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.core.tests.synchronization;
14
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertNotNull;
17 import static org.junit.Assert.assertTrue;
18 import static org.junit.Assert.fail;
19
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.ObjectInputStream;
25 import java.io.ObjectOutputStream;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.LinkedList;
29
30 import org.eclipse.jdt.annotation.NonNull;
31 import org.eclipse.tracecompass.internal.tmf.core.synchronization.SyncAlgorithmFullyIncremental;
32 import org.eclipse.tracecompass.tmf.core.event.matching.TmfEventDependency;
33 import org.eclipse.tracecompass.tmf.core.synchronization.ITmfTimestampTransform;
34 import org.eclipse.tracecompass.tmf.core.synchronization.SynchronizationAlgorithm;
35 import org.eclipse.tracecompass.tmf.core.synchronization.SynchronizationAlgorithm.SyncQuality;
36 import org.eclipse.tracecompass.tmf.core.synchronization.SynchronizationAlgorithmFactory;
37 import org.eclipse.tracecompass.tmf.core.synchronization.TimestampTransformFactory;
38 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
39 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
40 import org.eclipse.tracecompass.tmf.tests.stubs.event.TmfSyncEventStub;
41 import org.eclipse.tracecompass.tmf.tests.stubs.trace.TmfTraceStub;
42 import org.junit.Before;
43 import org.junit.Test;
44
45 /**
46 * Tests for {@link SynchronizationAlgorithm} and its descendants
47 *
48 * @author Geneviève Bastien
49 */
50 @SuppressWarnings("nls")
51 public class SyncTest {
52
53 private TmfTraceStub t1, t2;
54 private @NonNull Collection<ITmfTrace> fTraces = Collections.EMPTY_LIST;
55
56 /**
57 * Initializing the traces
58 */
59 @Before
60 public void init() {
61 t1 = new TmfTraceStub();
62 t1.init("t1");
63 t2 = new TmfTraceStub();
64 t2.init("t2");
65
66 Collection<ITmfTrace> traces = new LinkedList<>();
67 traces.add(t1);
68 traces.add(t2);
69 fTraces = traces;
70 }
71
72 /**
73 * Testing fully incremental algorithm with communication between the two
74 * traces
75 */
76 @Test
77 public void testFullyIncremental() {
78
79 SynchronizationAlgorithm syncAlgo = SynchronizationAlgorithmFactory.getFullyIncrementalAlgorithm();
80
81 syncAlgo.init(fTraces);
82
83 assertEquals(SyncQuality.ABSENT, syncAlgo.getSynchronizationQuality(t1, t2));
84 addSyncMatch(syncAlgo, t2, 1, t1, 1);
85 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1 beta 0 ]]", syncAlgo.toString());
86 assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
87
88 addSyncMatch(syncAlgo, t1, 1, t2, 3);
89 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1 beta 0 ]]", syncAlgo.toString());
90 assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
91
92 addSyncMatch(syncAlgo, t2, 2, t1, 3);
93 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1 beta 0.5 ]]", syncAlgo.toString());
94 assertEquals(SyncQuality.APPROXIMATE, syncAlgo.getSynchronizationQuality(t1, t2));
95
96 addSyncMatch(syncAlgo, t1, 3, t2, 5);
97 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 0.75 beta 1.25 ]]", syncAlgo.toString());
98 assertEquals(SyncQuality.ACCURATE, syncAlgo.getSynchronizationQuality(t1, t2));
99
100 addSyncMatch(syncAlgo, t1, 4, t2, 8);
101 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 0.75 beta 1.25 ]]", syncAlgo.toString());
102 assertEquals(SyncQuality.ACCURATE, syncAlgo.getSynchronizationQuality(t1, t2));
103
104 addSyncMatch(syncAlgo, t2, 4, t1, 5);
105 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1.125 beta 0.875 ]]", syncAlgo.toString());
106 assertEquals(SyncQuality.ACCURATE, syncAlgo.getSynchronizationQuality(t1, t2));
107
108 addSyncMatch(syncAlgo, t2, 4, t1, 6);
109 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1.125 beta 0.875 ]]", syncAlgo.toString());
110 assertEquals(SyncQuality.ACCURATE, syncAlgo.getSynchronizationQuality(t1, t2));
111
112 addSyncMatch(syncAlgo, t1, 6, t2, 7);
113 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 0.725 beta 1.275 ]]", syncAlgo.toString());
114 assertEquals(SyncQuality.ACCURATE, syncAlgo.getSynchronizationQuality(t1, t2));
115
116 ITmfTimestampTransform tt2 = syncAlgo.getTimestampTransform(t2);
117 ITmfTimestampTransform tt1 = syncAlgo.getTimestampTransform(t1);
118
119 assertEquals(syncAlgo.getTimestampTransform(t1.getHostId()), tt1);
120 assertEquals(TimestampTransformFactory.getDefaultTransform(), tt1);
121 assertEquals(syncAlgo.getTimestampTransform(t2.getHostId()), tt2);
122
123 /* Make the two hulls intersect */
124 addSyncMatch(syncAlgo, t1, 7, t2, 4);
125 addSyncMatch(syncAlgo, t2, 7, t1, 3);
126 assertEquals(SyncQuality.FAIL, syncAlgo.getSynchronizationQuality(t1, t2));
127 }
128
129 /**
130 * Testing the fully incremental synchronization algorithm when
131 * communication goes in only one direction
132 */
133 @Test
134 public void testOneHull() {
135
136 SynchronizationAlgorithm syncAlgo = SynchronizationAlgorithmFactory.getFullyIncrementalAlgorithm();
137
138 syncAlgo.init(fTraces);
139
140 assertEquals(SyncQuality.ABSENT, syncAlgo.getSynchronizationQuality(t1, t2));
141
142 addSyncMatch(syncAlgo, t1, 1, t2, 3);
143 assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
144
145 addSyncMatch(syncAlgo, t1, 2, t2, 5);
146 assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
147
148 addSyncMatch(syncAlgo, t1, 3, t2, 5);
149 assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
150
151 addSyncMatch(syncAlgo, t1, 4, t2, 7);
152 assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
153 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1 beta 0 ]]", syncAlgo.toString());
154
155 }
156
157 /**
158 * Testing the fully incremental synchronization algorithm when all
159 * communication from trace1 to trace2 happens before all communication from
160 * trace2 to trace1
161 */
162 @Test
163 public void testDisjoint() {
164
165 SynchronizationAlgorithm syncAlgo = SynchronizationAlgorithmFactory.getFullyIncrementalAlgorithm();
166
167 syncAlgo.init(fTraces);
168
169 assertEquals(SyncQuality.ABSENT, syncAlgo.getSynchronizationQuality(t1, t2));
170
171 addSyncMatch(syncAlgo, t1, 1, t2, 3);
172 assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
173
174 addSyncMatch(syncAlgo, t1, 2, t2, 5);
175 assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
176
177 addSyncMatch(syncAlgo, t1, 3, t2, 5);
178 assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
179
180 addSyncMatch(syncAlgo, t1, 4, t2, 7);
181 assertEquals(SyncQuality.INCOMPLETE, syncAlgo.getSynchronizationQuality(t1, t2));
182 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1 beta 0 ]]", syncAlgo.toString());
183
184 addSyncMatch(syncAlgo, t2, 7, t1, 6);
185 assertEquals(SyncQuality.APPROXIMATE, syncAlgo.getSynchronizationQuality(t1, t2));
186
187 addSyncMatch(syncAlgo, t2, 8, t1, 6);
188 assertEquals(SyncQuality.APPROXIMATE, syncAlgo.getSynchronizationQuality(t1, t2));
189
190 addSyncMatch(syncAlgo, t2, 10, t1, 8);
191 assertEquals(SyncQuality.APPROXIMATE, syncAlgo.getSynchronizationQuality(t1, t2));
192 assertEquals("SyncAlgorithmFullyIncremental [Between t1 and t2 [ alpha 1 beta 2.5 ]]", syncAlgo.toString());
193 }
194
195 private static void addSyncMatch(SynchronizationAlgorithm algo, ITmfTrace sender, long sendTs, ITmfTrace receiver, long receiveTs) {
196 algo.addMatch(
197 new TmfEventDependency(
198 new TmfSyncEventStub(sender, TmfTimestamp.fromSeconds(sendTs)),
199 new TmfSyncEventStub(receiver, TmfTimestamp.fromSeconds(receiveTs))
200 ));
201 }
202
203 /**
204 * Testing the serialization of the fully incremental synchronization
205 * algorithm
206 */
207 @Test
208 public void testFullyIncrementalSerialization() {
209
210 /* Do a run of synchronization and check the results */
211 SynchronizationAlgorithm syncAlgo = SynchronizationAlgorithmFactory.getFullyIncrementalAlgorithm();
212
213 syncAlgo.init(fTraces);
214
215 addSyncMatch(syncAlgo, t2, 1, t1, 1);
216 addSyncMatch(syncAlgo, t1, 1, t2, 3);
217 addSyncMatch(syncAlgo, t2, 2, t1, 3);
218 addSyncMatch(syncAlgo, t1, 3, t2, 5);
219 addSyncMatch(syncAlgo, t1, 4, t2, 8);
220 addSyncMatch(syncAlgo, t2, 4, t1, 5);
221 addSyncMatch(syncAlgo, t2, 4, t1, 6);
222 addSyncMatch(syncAlgo, t1, 6, t2, 7);
223
224 ITmfTimestampTransform tt2 = syncAlgo.getTimestampTransform(t2);
225 ITmfTimestampTransform tt1 = syncAlgo.getTimestampTransform(t1);
226
227 assertEquals(SyncQuality.ACCURATE, syncAlgo.getSynchronizationQuality(t1, t2));
228 assertEquals(syncAlgo.getTimestampTransform(t1.getHostId()), tt1);
229 assertEquals(TimestampTransformFactory.getDefaultTransform(), tt1);
230 assertEquals(syncAlgo.getTimestampTransform(t2.getHostId()), tt2);
231
232 /* Serialize the object */
233 String filePath = null;
234 try {
235 File temp = File.createTempFile("serialSyncAlgo", ".tmp");
236 filePath = temp.getAbsolutePath();
237 } catch (IOException e) {
238 fail("Could not create temporary file for serialization");
239 }
240 assertNotNull(filePath);
241
242 try (FileOutputStream fileOut = new FileOutputStream(filePath);
243 ObjectOutputStream out = new ObjectOutputStream(fileOut);) {
244 out.writeObject(syncAlgo);
245
246 } catch (IOException e) {
247 fail("Error serializing the synchronization algorithm " + e.getMessage());
248 }
249
250 SynchronizationAlgorithm deserialAlgo = null;
251 /* De-Serialize the object */
252 try (FileInputStream fileIn = new FileInputStream(filePath);
253 ObjectInputStream in = new ObjectInputStream(fileIn);) {
254 deserialAlgo = (SynchronizationAlgorithm) in.readObject();
255 } catch (IOException | ClassNotFoundException e) {
256 fail("Error de-serializing the synchronization algorithm " + e.getMessage());
257 }
258
259 /* Check that the deserialize algorithm is equivalent to original */
260 assertNotNull(deserialAlgo);
261 assertTrue(deserialAlgo instanceof SyncAlgorithmFullyIncremental);
262 assertEquals(SyncQuality.ACCURATE, deserialAlgo.getSynchronizationQuality(t1, t2));
263 assertEquals(tt1, deserialAlgo.getTimestampTransform(t1));
264 assertEquals(tt2, deserialAlgo.getTimestampTransform(t2));
265
266 }
267
268 }
This page took 0.038903 seconds and 5 git commands to generate.