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
1 /*******************************************************************************
2 * Copyright (c) 2009, 2014 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made 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 * Francois Chouinard - Initial API and implementation
11 * Francois Chouinard - Adjusted for new Trace Model
12 * Alexandre Montplaisir - Port to JUnit4
13 * Patrick Tasse - Updated for rank in experiment location
14 *******************************************************************************/
15
16 package org.eclipse.tracecompass.tmf.core.tests.trace;
17
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertNull;
22 import static org.junit.Assert.assertTrue;
23
24 import java.io.File;
25 import java.io.IOException;
26 import java.net.URISyntaxException;
27 import java.net.URL;
28 import java.util.Map;
29 import java.util.Vector;
30
31 import org.eclipse.core.runtime.FileLocator;
32 import org.eclipse.core.runtime.Path;
33 import org.eclipse.jdt.annotation.NonNull;
34 import org.eclipse.tracecompass.internal.tmf.core.component.TmfProviderManager;
35 import org.eclipse.tracecompass.internal.tmf.core.synchronization.SyncAlgorithmFullyIncremental;
36 import org.eclipse.tracecompass.internal.tmf.core.trace.experiment.TmfExperimentContext;
37 import org.eclipse.tracecompass.internal.tmf.core.trace.experiment.TmfExperimentLocation;
38 import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule;
39 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
40 import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
41 import org.eclipse.tracecompass.tmf.core.project.model.ITmfPropertiesProvider;
42 import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest;
43 import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest.ExecutionType;
44 import org.eclipse.tracecompass.tmf.core.request.TmfEventRequest;
45 import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal;
46 import org.eclipse.tracecompass.tmf.core.synchronization.ITmfTimestampTransform;
47 import org.eclipse.tracecompass.tmf.core.synchronization.SynchronizationAlgorithm;
48 import org.eclipse.tracecompass.tmf.core.synchronization.TimestampTransformFactory;
49 import org.eclipse.tracecompass.tmf.core.tests.TmfCoreTestPlugin;
50 import org.eclipse.tracecompass.tmf.core.tests.analysis.AnalysisManagerTest;
51 import org.eclipse.tracecompass.tmf.core.tests.shared.TmfTestTrace;
52 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
53 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
54 import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
55 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
56 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
57 import org.eclipse.tracecompass.tmf.core.trace.experiment.TmfExperiment;
58 import org.eclipse.tracecompass.tmf.core.trace.location.ITmfLocation;
59 import org.eclipse.tracecompass.tmf.core.trace.location.TmfLongLocation;
60 import org.eclipse.tracecompass.tmf.tests.stubs.analysis.TestExperimentAnalysis;
61 import org.eclipse.tracecompass.tmf.tests.stubs.trace.TmfExperimentStub;
62 import org.eclipse.tracecompass.tmf.tests.stubs.trace.TmfTraceStub;
63 import org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.TmfXmlTraceStub;
64 import org.junit.After;
65 import org.junit.Before;
66 import org.junit.Test;
67
68 import com.google.common.collect.ImmutableMap;
69
70 /**
71 * Test suite for the TmfExperiment class (single trace).
72 */
73 @SuppressWarnings("javadoc")
74 public class TmfExperimentTest {
75
76 // ------------------------------------------------------------------------
77 // Attributes
78 // ------------------------------------------------------------------------
79
80 private static final String EXPERIMENT = "MyExperiment";
81 private static int NB_EVENTS = 10000;
82 private static int BLOCK_SIZE = 1000;
83
84 private static final double DELTA = 1e-15;
85
86 private ITmfTrace[] fTestTraces;
87 private TmfExperimentStub fExperiment;
88
89 private static byte SCALE = (byte) -3;
90
91 // ------------------------------------------------------------------------
92 // Housekeeping
93 // ------------------------------------------------------------------------
94
95 private synchronized ITmfTrace[] setupTrace(final String path) {
96 if (fTestTraces == null) {
97 fTestTraces = new ITmfTrace[1];
98 try {
99 final URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(path), null);
100 final File test = new File(FileLocator.toFileURL(location).toURI());
101 final TmfTraceStub trace = new TmfTraceStub(test.getPath(), 0, true, null);
102 fTestTraces[0] = trace;
103 } catch (final TmfTraceException e) {
104 e.printStackTrace();
105 } catch (final URISyntaxException e) {
106 e.printStackTrace();
107 } catch (final IOException e) {
108 e.printStackTrace();
109 }
110 }
111 return fTestTraces;
112 }
113
114 private synchronized void setupExperiment() {
115 if (fExperiment == null) {
116 fExperiment = new TmfExperimentStub(EXPERIMENT, fTestTraces, BLOCK_SIZE);
117 fExperiment.getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, true);
118 }
119 }
120
121 @Before
122 public void setUp() {
123 setupTrace(TmfTestTrace.A_TEST_10K.getFullPath());
124 setupExperiment();
125 }
126
127 @After
128 public void tearDown() {
129 if (fExperiment != null) {
130 fExperiment.dispose();
131 }
132 assertEquals(0, TmfProviderManager.getProviders(ITmfEvent.class).length);
133 }
134
135 // ------------------------------------------------------------------------
136 // Constructor
137 // ------------------------------------------------------------------------
138
139 @Test
140 public void testSimpleTmfExperimentConstructor() {
141 TmfExperiment experiment = new TmfExperiment(ITmfEvent.class, EXPERIMENT,
142 fTestTraces, TmfExperiment.DEFAULT_INDEX_PAGE_SIZE, null);
143 assertEquals("GetId", EXPERIMENT, experiment.getName());
144 assertEquals("GetCacheSize", TmfExperiment.DEFAULT_INDEX_PAGE_SIZE, experiment.getCacheSize());
145 experiment.dispose();
146
147 experiment = new TmfExperiment(ITmfEvent.class, EXPERIMENT, null,
148 TmfExperiment.DEFAULT_INDEX_PAGE_SIZE, null);
149 experiment.dispose();
150 }
151
152 @Test
153 public void testNormalTmfExperimentConstructor() {
154 assertEquals("GetId", EXPERIMENT, fExperiment.getName());
155 assertEquals("GetNbEvents", NB_EVENTS, fExperiment.getNbEvents());
156
157 final long nbExperimentEvents = fExperiment.getNbEvents();
158 assertEquals("GetNbEvents", NB_EVENTS, nbExperimentEvents);
159
160 final long nbTraceEvents = fExperiment.getTraces().get(0).getNbEvents();
161 assertEquals("GetNbEvents", NB_EVENTS, nbTraceEvents);
162
163 final TmfTimeRange timeRange = fExperiment.getTimeRange();
164 assertEquals("getStartTime", 1, timeRange.getStartTime().getValue());
165 assertEquals("getEndTime", NB_EVENTS, timeRange.getEndTime().getValue());
166 }
167
168 // ------------------------------------------------------------------------
169 // Experiment setup
170 // ------------------------------------------------------------------------
171
172 @Test
173 public void testExperimentInitialization() {
174 /*
175 * Calling default constructor, then init should be equivalent to
176 * calling the full constructor
177 */
178
179 TmfExperimentStub experiment = new TmfExperimentStub(EXPERIMENT, fTestTraces, 5000);
180 experiment.getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, true);
181
182 assertEquals("GetId", EXPERIMENT, fExperiment.getName());
183 assertEquals("GetNbEvents", NB_EVENTS, fExperiment.getNbEvents());
184
185 final long nbExperimentEvents = fExperiment.getNbEvents();
186 assertEquals("GetNbEvents", NB_EVENTS, nbExperimentEvents);
187
188 final long nbTraceEvents = fExperiment.getTraces().get(0).getNbEvents();
189 assertEquals("GetNbEvents", NB_EVENTS, nbTraceEvents);
190
191 final TmfTimeRange timeRange = fExperiment.getTimeRange();
192 assertEquals("getStartTime", 1, timeRange.getStartTime().getValue());
193 assertEquals("getEndTime", NB_EVENTS, timeRange.getEndTime().getValue());
194
195 experiment.dispose();
196 }
197
198 // ------------------------------------------------------------------------
199 // getTimestamp
200 // ------------------------------------------------------------------------
201
202 @Test
203 public void testGetTimestamp() {
204 assertEquals("getTimestamp", TmfTimestamp.create( 1, (byte) -3), fExperiment.getTimestamp( 0));
205 assertEquals("getTimestamp", TmfTimestamp.create( 2, (byte) -3), fExperiment.getTimestamp( 1));
206 assertEquals("getTimestamp", TmfTimestamp.create( 11, (byte) -3), fExperiment.getTimestamp( 10));
207 assertEquals("getTimestamp", TmfTimestamp.create( 101, (byte) -3), fExperiment.getTimestamp( 100));
208 assertEquals("getTimestamp", TmfTimestamp.create( 1001, (byte) -3), fExperiment.getTimestamp(1000));
209 assertEquals("getTimestamp", TmfTimestamp.create( 2001, (byte) -3), fExperiment.getTimestamp(2000));
210 assertEquals("getTimestamp", TmfTimestamp.create( 2501, (byte) -3), fExperiment.getTimestamp(2500));
211 assertEquals("getTimestamp", TmfTimestamp.create(10000, (byte) -3), fExperiment.getTimestamp(9999));
212 assertNull("getTimestamp", fExperiment.getTimestamp(10000));
213 }
214
215 // ------------------------------------------------------------------------
216 // State system, statistics and modules methods
217 // ------------------------------------------------------------------------
218
219 @Test
220 public void testGetAnalysisModules() {
221 TmfExperiment experiment = fExperiment;
222 assertNotNull(experiment);
223
224 /* There should not be any modules at this point */
225 Iterable<IAnalysisModule> modules = experiment.getAnalysisModules();
226 assertFalse(modules.iterator().hasNext());
227
228 /* Open the experiment, the modules should be populated */
229 experiment.traceOpened(new TmfTraceOpenedSignal(this, experiment, null));
230 modules = experiment.getAnalysisModules();
231 Iterable<TestExperimentAnalysis> testModules = TmfTraceUtils.getAnalysisModulesOfClass(experiment, TestExperimentAnalysis.class);
232 assertTrue(modules.iterator().hasNext());
233 assertTrue(testModules.iterator().hasNext());
234
235 /*
236 * Test that a module that applies to one of its trace is present in an
237 * experiment
238 */
239 ITmfTrace trace1 = TmfTestTrace.A_TEST_10K.getTrace();
240 ITmfTrace trace2 = TmfTestTrace.A_TEST_10K2.getTrace();
241 ITmfTrace trace3 = TmfTestTrace.A_TEST_10K2.getTraceAsStub2();
242
243 /*
244 * Create an experiment with TmfTraceStub, the module other should not
245 * be there
246 */
247 ITmfTrace[] tracesExp1 = { trace1, trace2 };
248 TmfExperiment exp1 = new TmfExperiment(tracesExp1[0].getEventType(), "Experiment 1", tracesExp1, TmfExperiment.DEFAULT_INDEX_PAGE_SIZE, null);
249
250 /*
251 * Create an experiment containing some TmfTraceStub2, the module other
252 * should be present
253 */
254 ITmfTrace[] tracesExp2 = { trace1, trace3 };
255 TmfExperiment exp2 = new TmfExperiment(tracesExp2[0].getEventType(), "Experiment 1", tracesExp2, TmfExperiment.DEFAULT_INDEX_PAGE_SIZE, null);
256
257 try {
258 /* Open the experiment, the modules should be populated */
259 exp1.traceOpened(new TmfTraceOpenedSignal(this, exp1, null));
260 assertNull(exp1.getAnalysisModule(AnalysisManagerTest.MODULE_SECOND));
261
262 /* Open the experiment, the modules should be populated */
263 exp2.traceOpened(new TmfTraceOpenedSignal(this, exp2, null));
264 assertNotNull(exp2.getAnalysisModule(AnalysisManagerTest.MODULE_SECOND));
265 } finally {
266 trace1.dispose();
267 trace2.dispose();
268 trace3.dispose();
269 exp1.dispose();
270 exp2.dispose();
271 }
272
273 }
274
275 // ------------------------------------------------------------------------
276 // seekEvent by location
277 // ------------------------------------------------------------------------
278
279 @Test
280 public void testSeekBadLocation() {
281 ITmfContext context = fExperiment.seekEvent(new TmfLongLocation(0L));
282 assertNull("seekEvent", context);
283 }
284
285 @Test
286 public void testSeekNoTrace() {
287 TmfExperiment experiment = new TmfExperiment(ITmfEvent.class, EXPERIMENT,
288 null, TmfExperiment.DEFAULT_INDEX_PAGE_SIZE, null);
289 ITmfContext context = experiment.seekEvent((TmfExperimentLocation) null);
290 validateContextRanks(context);
291 experiment.dispose();
292 }
293
294 // ------------------------------------------------------------------------
295 // seekEvent on ratio
296 // ------------------------------------------------------------------------
297
298 @Test
299 public void testSeekEventOnRatio() {
300 // First event
301 ITmfContext context = fExperiment.seekEvent(0.0);
302 assertEquals("Context rank", 0, context.getRank());
303 ITmfEvent event = fExperiment.parseEvent(context);
304 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
305 assertEquals("Context rank", 0, context.getRank());
306
307 // Middle event
308 int midTrace = NB_EVENTS / 2;
309 context = fExperiment.seekEvent(0.5);
310 assertEquals("Context rank", midTrace, context.getRank());
311 event = fExperiment.parseEvent(context);
312 assertEquals("Event timestamp", midTrace + 1, event.getTimestamp().getValue());
313 assertEquals("Context rank", midTrace, context.getRank());
314
315 // Last event
316 context = fExperiment.seekEvent(1.0);
317 assertEquals("Context rank", NB_EVENTS, context.getRank());
318 event = fExperiment.parseEvent(context);
319 assertNull("Event timestamp", event);
320 assertEquals("Context rank", NB_EVENTS, context.getRank());
321
322 // Beyond last event
323 context = fExperiment.seekEvent(1.1);
324 assertEquals("Context rank", NB_EVENTS, context.getRank());
325 event = fExperiment.parseEvent(context);
326 assertNull("Event timestamp", event);
327 assertEquals("Context rank", NB_EVENTS, context.getRank());
328
329 // Negative ratio
330 context = fExperiment.seekEvent(-0.5);
331 assertEquals("Context rank", 0, context.getRank());
332 event = fExperiment.parseEvent(context);
333 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
334 assertEquals("Context rank", 0, context.getRank());
335 }
336
337 @Test
338 public void testGetLocationRatio() {
339 // First event
340 ITmfContext context = fExperiment.seekEvent((ITmfLocation) null);
341 double ratio = fExperiment.getLocationRatio(context.getLocation());
342 assertEquals("getLocationRatio", 0.0, ratio, DELTA);
343
344 // Middle event
345 context = fExperiment.seekEvent(NB_EVENTS / 2);
346 ratio = fExperiment.getLocationRatio(context.getLocation());
347 assertEquals("getLocationRatio", (double) (NB_EVENTS / 2) / NB_EVENTS, ratio, DELTA);
348
349 // Last event
350 context = fExperiment.seekEvent(NB_EVENTS - 1);
351 ratio = fExperiment.getLocationRatio(context.getLocation());
352 assertEquals("getLocationRatio", (double) (NB_EVENTS - 1) / NB_EVENTS, ratio, DELTA);
353 }
354
355 // @SuppressWarnings("rawtypes")
356 // public void testGetCurrentLocation() {
357 // ITmfContext context = fExperiment.seekEvent((ITmfLocation) null);
358 // ITmfLocation location = fExperiment.getCurrentLocation();
359 // assertEquals("getCurrentLocation", location, context.getLocation());
360 // }
361
362 // ------------------------------------------------------------------------
363 // seekEvent on rank
364 // ------------------------------------------------------------------------
365
366 @Test
367 public void testSeekRankOnCacheBoundary() {
368 long cacheSize = fExperiment.getCacheSize();
369
370 // On lower bound, returns the first event (TS = 1)
371 ITmfContext context = fExperiment.seekEvent(0);
372 assertEquals("Context rank", 0, context.getRank());
373
374 ITmfEvent event = fExperiment.getNext(context);
375 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
376 assertEquals("Context rank", 1, context.getRank());
377
378 // Position trace at event rank [cacheSize]
379 context = fExperiment.seekEvent(cacheSize);
380 assertEquals("Context rank", cacheSize, context.getRank());
381
382 event = fExperiment.getNext(context);
383 assertEquals("Event timestamp", cacheSize + 1, event.getTimestamp().getValue());
384 assertEquals("Context rank", cacheSize + 1, context.getRank());
385
386 // Position trace at event rank [4 * cacheSize]
387 context = fExperiment.seekEvent(4 * cacheSize);
388 assertEquals("Context rank", 4 * cacheSize, context.getRank());
389
390 event = fExperiment.getNext(context);
391 assertEquals("Event timestamp", 4 * cacheSize + 1, event.getTimestamp().getValue());
392 assertEquals("Context rank", 4 * cacheSize + 1, context.getRank());
393 }
394
395 @Test
396 public void testSeekRankNotOnCacheBoundary() {
397 long cacheSize = fExperiment.getCacheSize();
398
399 // Position trace at event rank 9
400 ITmfContext context = fExperiment.seekEvent(9);
401 assertEquals("Context rank", 9, context.getRank());
402
403 ITmfEvent event = fExperiment.getNext(context);
404 assertEquals("Event timestamp", 10, event.getTimestamp().getValue());
405 assertEquals("Context rank", 10, context.getRank());
406
407 // Position trace at event rank [cacheSize - 1]
408 context = fExperiment.seekEvent(cacheSize - 1);
409 assertEquals("Context rank", cacheSize - 1, context.getRank());
410
411 event = fExperiment.getNext(context);
412 assertEquals("Event timestamp", cacheSize, event.getTimestamp().getValue());
413 assertEquals("Context rank", cacheSize, context.getRank());
414
415 // Position trace at event rank [cacheSize + 1]
416 context = fExperiment.seekEvent(cacheSize + 1);
417 assertEquals("Context rank", cacheSize + 1, context.getRank());
418
419 event = fExperiment.getNext(context);
420 assertEquals("Event timestamp", cacheSize + 2, event.getTimestamp().getValue());
421 assertEquals("Context rank", cacheSize + 2, context.getRank());
422
423 // Position trace at event rank 4500
424 context = fExperiment.seekEvent(4500);
425 assertEquals("Context rank", 4500, context.getRank());
426
427 event = fExperiment.getNext(context);
428 assertEquals("Event timestamp", 4501, event.getTimestamp().getValue());
429 assertEquals("Context rank", 4501, context.getRank());
430 }
431
432 @Test
433 public void testSeekRankOutOfScope() {
434 // Position trace at beginning
435 ITmfContext context = fExperiment.seekEvent(-1);
436 assertEquals("Event rank", 0, context.getRank());
437
438 ITmfEvent event = fExperiment.getNext(context);
439 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
440 assertEquals("Context rank", 1, context.getRank());
441
442 // Position trace at event passed the end
443 context = fExperiment.seekEvent(NB_EVENTS);
444 assertEquals("Context rank", NB_EVENTS, context.getRank());
445
446 event = fExperiment.getNext(context);
447 assertNull("Event", event);
448 assertEquals("Context rank", NB_EVENTS, context.getRank());
449 }
450
451 // ------------------------------------------------------------------------
452 // seekEvent on timestamp
453 // ------------------------------------------------------------------------
454
455 @Test
456 public void testSeekTimestampOnCacheBoundary() {
457 long cacheSize = fExperiment.getCacheSize();
458
459 // Position trace at event rank 0
460 ITmfContext context = fExperiment.seekEvent(TmfTimestamp.create(1, SCALE));
461 assertEquals("Context rank", 0, context.getRank());
462
463 ITmfEvent event = fExperiment.getNext(context);
464 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
465 assertEquals("Context rank", 1, context.getRank());
466
467 // Position trace at event rank [cacheSize]
468 context = fExperiment.seekEvent(TmfTimestamp.create(cacheSize + 1, SCALE));
469 assertEquals("Event rank", cacheSize, context.getRank());
470
471 event = fExperiment.getNext(context);
472 assertEquals("Event timestamp", cacheSize + 1, event.getTimestamp().getValue());
473 assertEquals("Context rank", cacheSize + 1, context.getRank());
474
475 // Position trace at event rank [4 * cacheSize]
476 context = fExperiment.seekEvent(TmfTimestamp.create(4 * cacheSize + 1, SCALE));
477 assertEquals("Context rank", 4 * cacheSize, context.getRank());
478
479 event = fExperiment.getNext(context);
480 assertEquals("Event timestamp", 4 * cacheSize + 1, event.getTimestamp().getValue());
481 assertEquals("Context rank", 4 * cacheSize + 1, context.getRank());
482 }
483
484 @Test
485 public void testSeekTimestampNotOnCacheBoundary() {
486 // Position trace at event rank 1 (TS = 2)
487 ITmfContext context = fExperiment.seekEvent(TmfTimestamp.create(2, SCALE));
488 assertEquals("Context rank", 1, context.getRank());
489
490 ITmfEvent event = fExperiment.getNext(context);
491 assertEquals("Event timestamp", 2, event.getTimestamp().getValue());
492 assertEquals("Context rank", 2, context.getRank());
493
494 // Position trace at event rank 9 (TS = 10)
495 context = fExperiment.seekEvent(TmfTimestamp.create(10, SCALE));
496 assertEquals("Context rank", 9, context.getRank());
497
498 event = fExperiment.getNext(context);
499 assertEquals("Event timestamp", 10, event.getTimestamp().getValue());
500 assertEquals("Context rank", 10, context.getRank());
501
502 // Position trace at event rank 999 (TS = 1000)
503 context = fExperiment.seekEvent(TmfTimestamp.create(1000, SCALE));
504 assertEquals("Context rank", 999, context.getRank());
505
506 event = fExperiment.getNext(context);
507 assertEquals("Event timestamp", 1000, event.getTimestamp().getValue());
508 assertEquals("Context rank", 1000, context.getRank());
509
510 // Position trace at event rank 1001 (TS = 1002)
511 context = fExperiment.seekEvent(TmfTimestamp.create(1002, SCALE));
512 assertEquals("Context rank", 1001, context.getRank());
513
514 event = fExperiment.getNext(context);
515 assertEquals("Event timestamp", 1002, event.getTimestamp().getValue());
516 assertEquals("Context rank", 1002, context.getRank());
517
518 // Position trace at event rank 4500 (TS = 4501)
519 context = fExperiment.seekEvent(TmfTimestamp.create(4501, SCALE));
520 assertEquals("Context rank", 4500, context.getRank());
521
522 event = fExperiment.getNext(context);
523 assertEquals("Event timestamp", 4501, event.getTimestamp().getValue());
524 assertEquals("Context rank", 4501, context.getRank());
525 }
526
527 @Test
528 public void testSeekTimestampOutOfScope() {
529 // Position trace at beginning
530 ITmfContext context = fExperiment.seekEvent(TmfTimestamp.create(-1, SCALE));
531 assertEquals("Event rank", 0, context.getRank());
532
533 ITmfEvent event = fExperiment.getNext(context);
534 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
535 assertEquals("Event rank", 1, context.getRank());
536
537 // Position trace at event passed the end
538 context = fExperiment.seekEvent(TmfTimestamp.create(NB_EVENTS + 1, SCALE));
539 event = fExperiment.getNext(context);
540 assertNull("Event location", event);
541 assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
542 }
543
544 // ------------------------------------------------------------------------
545 // seekEvent by location (context rank is undefined)
546 // ------------------------------------------------------------------------
547
548 @Test
549 public void testSeekLocationOnCacheBoundary() {
550 long cacheSize = fExperiment.getCacheSize();
551
552 // Position trace at event rank 0
553 ITmfContext tmpContext = fExperiment.seekEvent(0);
554 ITmfContext context = fExperiment.seekEvent(tmpContext.getLocation());
555
556 ITmfEvent event = fExperiment.getNext(context);
557 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
558
559 event = fExperiment.getNext(context);
560 assertEquals("Event timestamp", 2, event.getTimestamp().getValue());
561
562 // Position trace at event rank 'cacheSize'
563 tmpContext = fExperiment.seekEvent(cacheSize);
564 context = fExperiment.seekEvent(tmpContext.getLocation());
565
566 event = fExperiment.getNext(context);
567 assertEquals("Event timestamp", cacheSize + 1, event.getTimestamp().getValue());
568
569 event = fExperiment.getNext(context);
570 assertEquals("Event timestamp", cacheSize + 2, event.getTimestamp().getValue());
571
572 // Position trace at event rank 4 * 'cacheSize'
573 tmpContext = fExperiment.seekEvent(4 * cacheSize);
574 context = fExperiment.seekEvent(tmpContext.getLocation());
575
576 event = fExperiment.getNext(context);
577 assertEquals("Event timestamp", 4 * cacheSize + 1, event.getTimestamp().getValue());
578
579 event = fExperiment.getNext(context);
580 assertEquals("Event timestamp", 4 * cacheSize + 2, event.getTimestamp().getValue());
581 }
582
583 @Test
584 public void testSeekLocationNotOnCacheBoundary() {
585 long cacheSize = fExperiment.getCacheSize();
586
587 // Position trace at event 'cacheSize' - 1
588 ITmfContext tmpContext = fExperiment.seekEvent(cacheSize - 1);
589 ITmfContext context = fExperiment.seekEvent(tmpContext.getLocation());
590
591 ITmfEvent event = fExperiment.getNext(context);
592 assertEquals("Event timestamp", cacheSize, event.getTimestamp().getValue());
593
594 event = fExperiment.getNext(context);
595 assertEquals("Event timestamp", cacheSize + 1, event.getTimestamp().getValue());
596
597 // Position trace at event rank 2 * 'cacheSize' - 1
598 tmpContext = fExperiment.seekEvent(2 * cacheSize - 1);
599 context = fExperiment.seekEvent(tmpContext.getLocation());
600 context = fExperiment.seekEvent(2 * cacheSize - 1);
601
602 event = fExperiment.getNext(context);
603 assertEquals("Event timestamp", 2 * cacheSize, event.getTimestamp().getValue());
604
605 event = fExperiment.getNext(context);
606 assertEquals("Event timestamp", 2 * cacheSize + 1, event.getTimestamp().getValue());
607
608 // Position trace at event rank 4500
609 tmpContext = fExperiment.seekEvent(4500);
610 context = fExperiment.seekEvent(tmpContext.getLocation());
611
612 event = fExperiment.getNext(context);
613 assertEquals("Event timestamp", 4501, event.getTimestamp().getValue());
614
615 event = fExperiment.getNext(context);
616 assertEquals("Event timestamp", 4502, event.getTimestamp().getValue());
617 }
618
619 @Test
620 public void testSeekLocationOutOfScope() {
621 // Position trace at beginning
622 ITmfContext context = fExperiment.seekEvent((ITmfLocation) null);
623
624 ITmfEvent event = fExperiment.getNext(context);
625 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
626 }
627
628 // ------------------------------------------------------------------------
629 // getNext - updates the context
630 // ------------------------------------------------------------------------
631
632 private static void validateContextRanks(ITmfContext context) {
633 assertTrue("Experiment context type", context instanceof TmfExperimentContext);
634 TmfExperimentContext ctx = (TmfExperimentContext) context;
635
636 int nbTraces = ctx.getNbTraces();
637
638 // expRank = sum(trace ranks) - nbTraces + 1 (if lastTraceRead != NO_TRACE)
639 long expRank = -nbTraces + ((ctx.getLastTrace() != TmfExperimentContext.NO_TRACE) ? 1 : 0);
640 for (int i = 0; i < nbTraces; i++) {
641 ITmfContext subContext = ctx.getContext(i);
642 assertNotNull(subContext);
643 long rank = subContext.getRank();
644 if (rank == -1) {
645 expRank = -1;
646 break;
647 }
648 expRank += rank;
649 }
650 assertEquals("Experiment context rank", expRank, ctx.getRank());
651 }
652
653 @Test
654 public void testGetNextAfteSeekingOnTS_1() {
655
656 final long INITIAL_TS = 1;
657 final int NB_READS = 20;
658
659 // On lower bound, returns the first event (ts = 1)
660 final ITmfContext context = fExperiment.seekEvent(TmfTimestamp.create(INITIAL_TS, SCALE));
661
662 validateContextRanks(context);
663
664 // Read NB_EVENTS
665 ITmfEvent event;
666 for (int i = 0; i < NB_READS; i++) {
667 event = fExperiment.getNext(context);
668 assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue());
669 assertEquals("Event rank", INITIAL_TS + i, context.getRank());
670 }
671
672 // Make sure we stay positioned
673 event = fExperiment.parseEvent(context);
674 assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue());
675 assertEquals("Event rank", INITIAL_TS + NB_READS - 1, context.getRank());
676
677 validateContextRanks(context);
678 }
679
680 @Test
681 public void testGetNextAfteSeekingOnTS_2() {
682 final long INITIAL_TS = 2;
683 final int NB_READS = 20;
684
685 // On lower bound, returns the first event (ts = 2)
686 final ITmfContext context = fExperiment.seekEvent(TmfTimestamp.create(INITIAL_TS, SCALE));
687
688 validateContextRanks(context);
689
690 // Read NB_EVENTS
691 ITmfEvent event;
692 for (int i = 0; i < NB_READS; i++) {
693 event = fExperiment.getNext(context);
694 assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue());
695 assertEquals("Event rank", INITIAL_TS + i, context.getRank());
696 }
697
698 // Make sure we stay positioned
699 event = fExperiment.parseEvent(context);
700 assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue());
701 assertEquals("Event rank", INITIAL_TS + NB_READS - 1, context.getRank());
702
703 validateContextRanks(context);
704 }
705
706 @Test
707 public void testGetNextAfteSeekingOnTS_3() {
708
709 final long INITIAL_TS = 500;
710 final int NB_READS = 20;
711
712 // On lower bound, returns the first event (ts = 500)
713 final ITmfContext context = fExperiment.seekEvent(TmfTimestamp.create(INITIAL_TS, SCALE));
714
715 validateContextRanks(context);
716
717 // Read NB_EVENTS
718 ITmfEvent event;
719 for (int i = 0; i < NB_READS; i++) {
720 event = fExperiment.getNext(context);
721 assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue());
722 assertEquals("Event rank", INITIAL_TS + i, context.getRank());
723 }
724
725 // Make sure we stay positioned
726 event = fExperiment.parseEvent(context);
727 assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue());
728 assertEquals("Event rank", INITIAL_TS + NB_READS - 1, context.getRank());
729
730 validateContextRanks(context);
731 }
732
733 @Test
734 public void testGetNextAfterSeekingOnRank_1() {
735 final long INITIAL_RANK = 0L;
736 final int NB_READS = 20;
737
738 // On lower bound, returns the first event (rank = 0)
739 final ITmfContext context = fExperiment.seekEvent(INITIAL_RANK);
740
741 validateContextRanks(context);
742
743 // Read NB_EVENTS
744 ITmfEvent event;
745 for (int i = 0; i < NB_READS; i++) {
746 event = fExperiment.getNext(context);
747 assertEquals("Event timestamp", INITIAL_RANK + i + 1, event.getTimestamp().getValue());
748 assertEquals("Event rank", INITIAL_RANK + i + 1, context.getRank());
749 }
750
751 // Make sure we stay positioned
752 event = fExperiment.parseEvent(context);
753 assertEquals("Event timestamp", INITIAL_RANK + NB_READS + 1, event.getTimestamp().getValue());
754 assertEquals("Event rank", INITIAL_RANK + NB_READS, context.getRank());
755
756 validateContextRanks(context);
757 }
758
759 @Test
760 public void testGetNextAfterSeekingOnRank_2() {
761 final long INITIAL_RANK = 1L;
762 final int NB_READS = 20;
763
764 // On lower bound, returns the first event (rank = 0)
765 final ITmfContext context = fExperiment.seekEvent(INITIAL_RANK);
766
767 validateContextRanks(context);
768
769 // Read NB_EVENTS
770 ITmfEvent event;
771 for (int i = 0; i < NB_READS; i++) {
772 event = fExperiment.getNext(context);
773 assertEquals("Event timestamp", INITIAL_RANK + i + 1, event.getTimestamp().getValue());
774 assertEquals("Event rank", INITIAL_RANK + i + 1, context.getRank());
775 }
776
777 // Make sure we stay positioned
778 event = fExperiment.parseEvent(context);
779 assertEquals("Event timestamp", INITIAL_RANK + NB_READS + 1, event.getTimestamp().getValue());
780 assertEquals("Event rank", INITIAL_RANK + NB_READS, context.getRank());
781
782 validateContextRanks(context);
783 }
784
785 @Test
786 public void testGetNextAfterSeekingOnRank_3() {
787 final long INITIAL_RANK = 500L;
788 final int NB_READS = 20;
789
790 // On lower bound, returns the first event (rank = 0)
791 final ITmfContext context = fExperiment.seekEvent(INITIAL_RANK);
792
793 validateContextRanks(context);
794
795 // Read NB_EVENTS
796 ITmfEvent event;
797 for (int i = 0; i < NB_READS; i++) {
798 event = fExperiment.getNext(context);
799 assertEquals("Event timestamp", INITIAL_RANK + i + 1, event.getTimestamp().getValue());
800 assertEquals("Event rank", INITIAL_RANK + i + 1, context.getRank());
801 }
802
803 // Make sure we stay positioned
804 event = fExperiment.parseEvent(context);
805 assertEquals("Event timestamp", INITIAL_RANK + NB_READS + 1, event.getTimestamp().getValue());
806 assertEquals("Event rank", INITIAL_RANK + NB_READS, context.getRank());
807
808 validateContextRanks(context);
809 }
810
811 @Test
812 public void testGetNextAfterSeekingOnLocation_1() {
813 final ITmfLocation INITIAL_LOC = null;
814 final long INITIAL_TS = 1;
815 final int NB_READS = 20;
816
817 // On lower bound, returns the first event (ts = 1)
818 final ITmfContext context = fExperiment.seekEvent(INITIAL_LOC);
819
820 validateContextRanks(context);
821
822 // Read NB_EVENTS
823 ITmfEvent event;
824 for (int i = 0; i < NB_READS; i++) {
825 event = fExperiment.getNext(context);
826 assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue());
827 assertEquals("Event rank", INITIAL_TS + i, context.getRank());
828 }
829
830 // Make sure we stay positioned
831 event = fExperiment.parseEvent(context);
832 assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue());
833 assertEquals("Event rank", INITIAL_TS + NB_READS - 1, context.getRank());
834
835 validateContextRanks(context);
836 }
837
838 @Test
839 public void testGetNextAfterSeekingOnLocation_2() {
840 final ITmfLocation INITIAL_LOC = fExperiment.seekEvent(1L).getLocation();
841 final long INITIAL_TS = 2;
842 final int NB_READS = 20;
843
844 // On lower bound, returns the first event (ts = 2)
845 final ITmfContext context = fExperiment.seekEvent(INITIAL_LOC);
846
847 validateContextRanks(context);
848
849 // Read NB_EVENTS
850 ITmfEvent event;
851 for (int i = 0; i < NB_READS; i++) {
852 event = fExperiment.getNext(context);
853 assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue());
854 }
855
856 // Make sure we stay positioned
857 event = fExperiment.parseEvent(context);
858 assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue());
859
860 validateContextRanks(context);
861 }
862
863 @Test
864 public void testGetNextAfterSeekingOnLocation_3() {
865 final ITmfLocation INITIAL_LOC = fExperiment.seekEvent(500L).getLocation();
866 final long INITIAL_TS = 501;
867 final int NB_READS = 20;
868
869 // On lower bound, returns the first event (ts = 501)
870 final ITmfContext context = fExperiment.seekEvent(INITIAL_LOC);
871
872 validateContextRanks(context);
873
874 // Read NB_EVENTS
875 ITmfEvent event;
876 for (int i = 0; i < NB_READS; i++) {
877 event = fExperiment.getNext(context);
878 assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue());
879 }
880
881 // Make sure we stay positioned
882 event = fExperiment.parseEvent(context);
883 assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue());
884
885 validateContextRanks(context);
886 }
887
888 @Test
889 public void testGetNextLocation() {
890 ITmfContext context1 = fExperiment.seekEvent(0);
891 fExperiment.getNext(context1);
892 ITmfLocation location = context1.getLocation();
893 ITmfEvent event1 = fExperiment.getNext(context1);
894 ITmfContext context2 = fExperiment.seekEvent(location);
895 ITmfEvent event2 = fExperiment.getNext(context2);
896 assertEquals("Event timestamp", event1.getTimestamp().getValue(), event2.getTimestamp().getValue());
897 }
898
899 @Test
900 public void testGetNextEndLocation() {
901 ITmfContext context1 = fExperiment.seekEvent(fExperiment.getNbEvents() - 1);
902 fExperiment.getNext(context1);
903 ITmfLocation location = context1.getLocation();
904 ITmfContext context2 = fExperiment.seekEvent(location);
905 ITmfEvent event = fExperiment.getNext(context2);
906 assertNull("Event", event);
907 }
908
909 // ------------------------------------------------------------------------
910 // processRequest
911 // ------------------------------------------------------------------------
912
913 @Test
914 public void testProcessRequestForNbEvents() throws InterruptedException {
915 final int nbEvents = 1000;
916 final Vector<ITmfEvent> requestedEvents = new Vector<>();
917
918 final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
919 final TmfEventRequest request = new TmfEventRequest(ITmfEvent.class,
920 range, 0, nbEvents, ExecutionType.FOREGROUND) {
921 @Override
922 public void handleData(final ITmfEvent event) {
923 super.handleData(event);
924 requestedEvents.add(event);
925 }
926 };
927 fExperiment.sendRequest(request);
928 request.waitForCompletion();
929
930 assertEquals("nbEvents", nbEvents, requestedEvents.size());
931 assertTrue("isCompleted", request.isCompleted());
932 assertFalse("isCancelled", request.isCancelled());
933
934 // Ensure that we have distinct events.
935 // Don't go overboard: we are not validating the stub!
936 for (int i = 0; i < nbEvents; i++) {
937 assertEquals("Distinct events", i+1, requestedEvents.get(i).getTimestamp().getValue());
938 }
939 }
940
941 @Test
942 public void testProcessRequestForAllEvents() throws InterruptedException {
943 final int nbEvents = ITmfEventRequest.ALL_DATA;
944 final Vector<ITmfEvent> requestedEvents = new Vector<>();
945 final long nbExpectedEvents = NB_EVENTS;
946
947 final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
948 final TmfEventRequest request = new TmfEventRequest(ITmfEvent.class,
949 range, 0, nbEvents, ExecutionType.FOREGROUND) {
950 @Override
951 public void handleData(final ITmfEvent event) {
952 super.handleData(event);
953 requestedEvents.add(event);
954 }
955 };
956 fExperiment.sendRequest(request);
957 request.waitForCompletion();
958
959 assertEquals("nbEvents", nbExpectedEvents, requestedEvents.size());
960 assertTrue("isCompleted", request.isCompleted());
961 assertFalse("isCancelled", request.isCancelled());
962
963 // Ensure that we have distinct events.
964 // Don't go overboard: we are not validating the stub!
965 for (int i = 0; i < nbExpectedEvents; i++) {
966 assertEquals("Distinct events", i+1, requestedEvents.get(i).getTimestamp().getValue());
967 }
968 }
969
970 // ------------------------------------------------------------------------
971 // cancel
972 // ------------------------------------------------------------------------
973
974 @Test
975 public void testCancel() throws InterruptedException {
976 final int nbEvents = NB_EVENTS;
977 final int limit = BLOCK_SIZE;
978 final Vector<ITmfEvent> requestedEvents = new Vector<>();
979
980 final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
981 final TmfEventRequest request = new TmfEventRequest(ITmfEvent.class,
982 range, 0, nbEvents, ExecutionType.FOREGROUND) {
983 int nbRead = 0;
984
985 @Override
986 public void handleData(final ITmfEvent event) {
987 super.handleData(event);
988 requestedEvents.add(event);
989 if (++nbRead == limit) {
990 cancel();
991 }
992 }
993
994 @Override
995 public void handleCancel() {
996 if (requestedEvents.size() < limit) {
997 System.out.println("aie");
998 }
999 }
1000 };
1001 fExperiment.sendRequest(request);
1002 request.waitForCompletion();
1003
1004 assertEquals("nbEvents", limit, requestedEvents.size());
1005 assertTrue("isCompleted", request.isCompleted());
1006 assertTrue("isCancelled", request.isCancelled());
1007 }
1008
1009 private static abstract class TestTrace extends TmfXmlTraceStub implements ITmfPropertiesProvider {
1010
1011 }
1012
1013 /**
1014 * Tests that experiment with traces from the same host and a clock offset
1015 * are well synchronized
1016 */
1017 @Test
1018 public void testWithSingleHostClockOffset() {
1019 // Data for this specific test
1020 String hostId = "Test Host 1";
1021 long minOffset = 2000;
1022 long offset = 1000;
1023 String clockOffset = "clock_offset";
1024
1025 ITmfTrace t1 = new TestTrace() {
1026 @Override
1027 public @NonNull String getHostId() {
1028 return hostId;
1029 }
1030 @Override
1031 public @NonNull Map<@NonNull String, @NonNull String> getProperties() {
1032 return ImmutableMap.of(clockOffset, String.valueOf(minOffset));
1033 }
1034 };
1035
1036 ITmfTrace t2 = new TestTrace() {
1037 @Override
1038 public @NonNull String getHostId() {
1039 return hostId;
1040 }
1041 @Override
1042 public @NonNull Map<@NonNull String, @NonNull String> getProperties() {
1043 return ImmutableMap.of(clockOffset, String.valueOf(minOffset + offset));
1044 }
1045 };
1046
1047 TmfExperiment exp = new TmfExperimentStub(EXPERIMENT, new ITmfTrace[] { t1, t2 }, BLOCK_SIZE);
1048
1049 try {
1050 assertEquals(TimestampTransformFactory.createWithOffset(offset / 2), t1.getTimestampTransform());
1051 assertEquals(TimestampTransformFactory.createWithOffset(-offset / 2), t2.getTimestampTransform());
1052
1053 } finally {
1054 exp.dispose();
1055 }
1056 }
1057
1058 /**
1059 * Tests that opening an experiment whose traces already have a
1060 * synchronization formula will not eliminate that formula. This test makes
1061 * the supposition that the experiment was synchronized and the
1062 * synchronization added the clock offset correction to the total formula.
1063 */
1064 @Test
1065 public void testWithMultiHostClockOffset() {
1066 // Data for this specific test
1067 String hostId = "Test Host 1";
1068 String hostId2 = "Test Host 2";
1069 long minOffset = 2000;
1070 long offset = 1000;
1071 String clockOffset = "clock_offset";
1072
1073 ITmfTimestampTransform tt1 = TimestampTransformFactory.createLinear(2.0, offset / 2);
1074 ITmfTimestampTransform tt2 = TimestampTransformFactory.createLinear(2.0, -offset / 2);
1075 ITmfTimestampTransform tt3 = TimestampTransformFactory.createWithOffset(offset);
1076
1077 ITmfTrace t1 = new TestTrace() {
1078 @Override
1079 public @NonNull String getHostId() {
1080 return hostId;
1081 }
1082 @Override
1083 public @NonNull Map<@NonNull String, @NonNull String> getProperties() {
1084 return ImmutableMap.of(clockOffset, String.valueOf(minOffset));
1085 }
1086
1087 };
1088 t1.setTimestampTransform(tt1);
1089
1090 ITmfTrace t2 = new TestTrace() {
1091 @Override
1092 public @NonNull String getHostId() {
1093 return hostId;
1094 }
1095 @Override
1096 public @NonNull Map<@NonNull String, @NonNull String> getProperties() {
1097 return ImmutableMap.of(clockOffset, String.valueOf(minOffset + offset));
1098 }
1099 };
1100 t2.setTimestampTransform(tt2);
1101
1102 ITmfTrace t3 = new TmfXmlTraceStub() {
1103 @Override
1104 public @NonNull String getHostId() {
1105 return hostId2;
1106 }
1107 };
1108 t3.setTimestampTransform(tt3);
1109
1110 TmfExperiment exp = new TmfExperimentStub(EXPERIMENT, new ITmfTrace[] { t1, t2, t3 }, BLOCK_SIZE) {
1111
1112 @Override
1113 public SynchronizationAlgorithm synchronizeTraces() {
1114 return new SyncAlgorithmFullyIncremental() {
1115
1116 private static final long serialVersionUID = 4206172498287480153L;
1117
1118 @Override
1119 public ITmfTimestampTransform getTimestampTransform(String h) {
1120 if (hostId.equals(h)) {
1121 return TimestampTransformFactory.createLinear(2.0, 0);
1122 }
1123 return tt3;
1124 }
1125 };
1126 }
1127 };
1128
1129 try {
1130 assertEquals(tt1, t1.getTimestampTransform());
1131 assertEquals(tt2, t2.getTimestampTransform());
1132 assertEquals(tt3, t3.getTimestampTransform());
1133
1134 } finally {
1135 exp.dispose();
1136 }
1137 }
1138
1139 }
This page took 0.056363 seconds and 5 git commands to generate.