1 /*******************************************************************************
2 * Copyright (c) 2009, 2014 Ericsson
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
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 *******************************************************************************/
16 package org
.eclipse
.tracecompass
.tmf
.core
.tests
.trace
;
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
;
25 import java
.io
.IOException
;
26 import java
.net
.URISyntaxException
;
29 import java
.util
.Vector
;
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
;
68 import com
.google
.common
.collect
.ImmutableMap
;
71 * Test suite for the TmfExperiment class (single trace).
73 @SuppressWarnings("javadoc")
74 public class TmfExperimentTest
{
76 // ------------------------------------------------------------------------
78 // ------------------------------------------------------------------------
80 private static final String EXPERIMENT
= "MyExperiment";
81 private static int NB_EVENTS
= 10000;
82 private static int BLOCK_SIZE
= 1000;
84 private static final double DELTA
= 1e-15;
86 private ITmfTrace
[] fTestTraces
;
87 private TmfExperimentStub fExperiment
;
89 private static byte SCALE
= (byte) -3;
91 // ------------------------------------------------------------------------
93 // ------------------------------------------------------------------------
95 private synchronized ITmfTrace
[] setupTrace(final String path
) {
96 if (fTestTraces
== null) {
97 fTestTraces
= new ITmfTrace
[1];
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
) {
105 } catch (final URISyntaxException e
) {
107 } catch (final IOException e
) {
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);
122 public void setUp() {
123 setupTrace(TmfTestTrace
.A_TEST_10K
.getFullPath());
128 public void tearDown() {
129 if (fExperiment
!= null) {
130 fExperiment
.dispose();
132 assertEquals(0, TmfProviderManager
.getProviders(ITmfEvent
.class).length
);
135 // ------------------------------------------------------------------------
137 // ------------------------------------------------------------------------
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();
147 experiment
= new TmfExperiment(ITmfEvent
.class, EXPERIMENT
, null,
148 TmfExperiment
.DEFAULT_INDEX_PAGE_SIZE
, null);
149 experiment
.dispose();
153 public void testNormalTmfExperimentConstructor() {
154 assertEquals("GetId", EXPERIMENT
, fExperiment
.getName());
155 assertEquals("GetNbEvents", NB_EVENTS
, fExperiment
.getNbEvents());
157 final long nbExperimentEvents
= fExperiment
.getNbEvents();
158 assertEquals("GetNbEvents", NB_EVENTS
, nbExperimentEvents
);
160 final long nbTraceEvents
= fExperiment
.getTraces().get(0).getNbEvents();
161 assertEquals("GetNbEvents", NB_EVENTS
, nbTraceEvents
);
163 final TmfTimeRange timeRange
= fExperiment
.getTimeRange();
164 assertEquals("getStartTime", 1, timeRange
.getStartTime().getValue());
165 assertEquals("getEndTime", NB_EVENTS
, timeRange
.getEndTime().getValue());
168 // ------------------------------------------------------------------------
170 // ------------------------------------------------------------------------
173 public void testExperimentInitialization() {
175 * Calling default constructor, then init should be equivalent to
176 * calling the full constructor
179 TmfExperimentStub experiment
= new TmfExperimentStub(EXPERIMENT
, fTestTraces
, 5000);
180 experiment
.getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, true);
182 assertEquals("GetId", EXPERIMENT
, fExperiment
.getName());
183 assertEquals("GetNbEvents", NB_EVENTS
, fExperiment
.getNbEvents());
185 final long nbExperimentEvents
= fExperiment
.getNbEvents();
186 assertEquals("GetNbEvents", NB_EVENTS
, nbExperimentEvents
);
188 final long nbTraceEvents
= fExperiment
.getTraces().get(0).getNbEvents();
189 assertEquals("GetNbEvents", NB_EVENTS
, nbTraceEvents
);
191 final TmfTimeRange timeRange
= fExperiment
.getTimeRange();
192 assertEquals("getStartTime", 1, timeRange
.getStartTime().getValue());
193 assertEquals("getEndTime", NB_EVENTS
, timeRange
.getEndTime().getValue());
195 experiment
.dispose();
198 // ------------------------------------------------------------------------
200 // ------------------------------------------------------------------------
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));
215 // ------------------------------------------------------------------------
216 // State system, statistics and modules methods
217 // ------------------------------------------------------------------------
220 public void testGetAnalysisModules() {
221 TmfExperiment experiment
= fExperiment
;
222 assertNotNull(experiment
);
224 /* There should not be any modules at this point */
225 Iterable
<IAnalysisModule
> modules
= experiment
.getAnalysisModules();
226 assertFalse(modules
.iterator().hasNext());
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());
236 * Test that a module that applies to one of its trace is present in an
239 ITmfTrace trace1
= TmfTestTrace
.A_TEST_10K
.getTrace();
240 ITmfTrace trace2
= TmfTestTrace
.A_TEST_10K2
.getTrace();
241 ITmfTrace trace3
= TmfTestTrace
.A_TEST_10K2
.getTraceAsStub2();
244 * Create an experiment with TmfTraceStub, the module other should not
247 ITmfTrace
[] tracesExp1
= { trace1
, trace2
};
248 TmfExperiment exp1
= new TmfExperiment(tracesExp1
[0].getEventType(), "Experiment 1", tracesExp1
, TmfExperiment
.DEFAULT_INDEX_PAGE_SIZE
, null);
251 * Create an experiment containing some TmfTraceStub2, the module other
254 ITmfTrace
[] tracesExp2
= { trace1
, trace3
};
255 TmfExperiment exp2
= new TmfExperiment(tracesExp2
[0].getEventType(), "Experiment 1", tracesExp2
, TmfExperiment
.DEFAULT_INDEX_PAGE_SIZE
, null);
258 /* Open the experiment, the modules should be populated */
259 exp1
.traceOpened(new TmfTraceOpenedSignal(this, exp1
, null));
260 assertNull(exp1
.getAnalysisModule(AnalysisManagerTest
.MODULE_SECOND
));
262 /* Open the experiment, the modules should be populated */
263 exp2
.traceOpened(new TmfTraceOpenedSignal(this, exp2
, null));
264 assertNotNull(exp2
.getAnalysisModule(AnalysisManagerTest
.MODULE_SECOND
));
275 // ------------------------------------------------------------------------
276 // seekEvent by location
277 // ------------------------------------------------------------------------
280 public void testSeekBadLocation() {
281 ITmfContext context
= fExperiment
.seekEvent(new TmfLongLocation(0L));
282 assertNull("seekEvent", context
);
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();
294 // ------------------------------------------------------------------------
295 // seekEvent on ratio
296 // ------------------------------------------------------------------------
299 public void testSeekEventOnRatio() {
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());
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());
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());
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());
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());
338 public void testGetLocationRatio() {
340 ITmfContext context
= fExperiment
.seekEvent((ITmfLocation
) null);
341 double ratio
= fExperiment
.getLocationRatio(context
.getLocation());
342 assertEquals("getLocationRatio", 0.0, ratio
, DELTA
);
345 context
= fExperiment
.seekEvent(NB_EVENTS
/ 2);
346 ratio
= fExperiment
.getLocationRatio(context
.getLocation());
347 assertEquals("getLocationRatio", (double) (NB_EVENTS
/ 2) / NB_EVENTS
, ratio
, DELTA
);
350 context
= fExperiment
.seekEvent(NB_EVENTS
- 1);
351 ratio
= fExperiment
.getLocationRatio(context
.getLocation());
352 assertEquals("getLocationRatio", (double) (NB_EVENTS
- 1) / NB_EVENTS
, ratio
, DELTA
);
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());
362 // ------------------------------------------------------------------------
364 // ------------------------------------------------------------------------
367 public void testSeekRankOnCacheBoundary() {
368 long cacheSize
= fExperiment
.getCacheSize();
370 // On lower bound, returns the first event (TS = 1)
371 ITmfContext context
= fExperiment
.seekEvent(0);
372 assertEquals("Context rank", 0, context
.getRank());
374 ITmfEvent event
= fExperiment
.getNext(context
);
375 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
376 assertEquals("Context rank", 1, context
.getRank());
378 // Position trace at event rank [cacheSize]
379 context
= fExperiment
.seekEvent(cacheSize
);
380 assertEquals("Context rank", cacheSize
, context
.getRank());
382 event
= fExperiment
.getNext(context
);
383 assertEquals("Event timestamp", cacheSize
+ 1, event
.getTimestamp().getValue());
384 assertEquals("Context rank", cacheSize
+ 1, context
.getRank());
386 // Position trace at event rank [4 * cacheSize]
387 context
= fExperiment
.seekEvent(4 * cacheSize
);
388 assertEquals("Context rank", 4 * cacheSize
, context
.getRank());
390 event
= fExperiment
.getNext(context
);
391 assertEquals("Event timestamp", 4 * cacheSize
+ 1, event
.getTimestamp().getValue());
392 assertEquals("Context rank", 4 * cacheSize
+ 1, context
.getRank());
396 public void testSeekRankNotOnCacheBoundary() {
397 long cacheSize
= fExperiment
.getCacheSize();
399 // Position trace at event rank 9
400 ITmfContext context
= fExperiment
.seekEvent(9);
401 assertEquals("Context rank", 9, context
.getRank());
403 ITmfEvent event
= fExperiment
.getNext(context
);
404 assertEquals("Event timestamp", 10, event
.getTimestamp().getValue());
405 assertEquals("Context rank", 10, context
.getRank());
407 // Position trace at event rank [cacheSize - 1]
408 context
= fExperiment
.seekEvent(cacheSize
- 1);
409 assertEquals("Context rank", cacheSize
- 1, context
.getRank());
411 event
= fExperiment
.getNext(context
);
412 assertEquals("Event timestamp", cacheSize
, event
.getTimestamp().getValue());
413 assertEquals("Context rank", cacheSize
, context
.getRank());
415 // Position trace at event rank [cacheSize + 1]
416 context
= fExperiment
.seekEvent(cacheSize
+ 1);
417 assertEquals("Context rank", cacheSize
+ 1, context
.getRank());
419 event
= fExperiment
.getNext(context
);
420 assertEquals("Event timestamp", cacheSize
+ 2, event
.getTimestamp().getValue());
421 assertEquals("Context rank", cacheSize
+ 2, context
.getRank());
423 // Position trace at event rank 4500
424 context
= fExperiment
.seekEvent(4500);
425 assertEquals("Context rank", 4500, context
.getRank());
427 event
= fExperiment
.getNext(context
);
428 assertEquals("Event timestamp", 4501, event
.getTimestamp().getValue());
429 assertEquals("Context rank", 4501, context
.getRank());
433 public void testSeekRankOutOfScope() {
434 // Position trace at beginning
435 ITmfContext context
= fExperiment
.seekEvent(-1);
436 assertEquals("Event rank", 0, context
.getRank());
438 ITmfEvent event
= fExperiment
.getNext(context
);
439 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
440 assertEquals("Context rank", 1, context
.getRank());
442 // Position trace at event passed the end
443 context
= fExperiment
.seekEvent(NB_EVENTS
);
444 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
446 event
= fExperiment
.getNext(context
);
447 assertNull("Event", event
);
448 assertEquals("Context rank", NB_EVENTS
, context
.getRank());
451 // ------------------------------------------------------------------------
452 // seekEvent on timestamp
453 // ------------------------------------------------------------------------
456 public void testSeekTimestampOnCacheBoundary() {
457 long cacheSize
= fExperiment
.getCacheSize();
459 // Position trace at event rank 0
460 ITmfContext context
= fExperiment
.seekEvent(TmfTimestamp
.create(1, SCALE
));
461 assertEquals("Context rank", 0, context
.getRank());
463 ITmfEvent event
= fExperiment
.getNext(context
);
464 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
465 assertEquals("Context rank", 1, context
.getRank());
467 // Position trace at event rank [cacheSize]
468 context
= fExperiment
.seekEvent(TmfTimestamp
.create(cacheSize
+ 1, SCALE
));
469 assertEquals("Event rank", cacheSize
, context
.getRank());
471 event
= fExperiment
.getNext(context
);
472 assertEquals("Event timestamp", cacheSize
+ 1, event
.getTimestamp().getValue());
473 assertEquals("Context rank", cacheSize
+ 1, context
.getRank());
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());
479 event
= fExperiment
.getNext(context
);
480 assertEquals("Event timestamp", 4 * cacheSize
+ 1, event
.getTimestamp().getValue());
481 assertEquals("Context rank", 4 * cacheSize
+ 1, context
.getRank());
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());
490 ITmfEvent event
= fExperiment
.getNext(context
);
491 assertEquals("Event timestamp", 2, event
.getTimestamp().getValue());
492 assertEquals("Context rank", 2, context
.getRank());
494 // Position trace at event rank 9 (TS = 10)
495 context
= fExperiment
.seekEvent(TmfTimestamp
.create(10, SCALE
));
496 assertEquals("Context rank", 9, context
.getRank());
498 event
= fExperiment
.getNext(context
);
499 assertEquals("Event timestamp", 10, event
.getTimestamp().getValue());
500 assertEquals("Context rank", 10, context
.getRank());
502 // Position trace at event rank 999 (TS = 1000)
503 context
= fExperiment
.seekEvent(TmfTimestamp
.create(1000, SCALE
));
504 assertEquals("Context rank", 999, context
.getRank());
506 event
= fExperiment
.getNext(context
);
507 assertEquals("Event timestamp", 1000, event
.getTimestamp().getValue());
508 assertEquals("Context rank", 1000, context
.getRank());
510 // Position trace at event rank 1001 (TS = 1002)
511 context
= fExperiment
.seekEvent(TmfTimestamp
.create(1002, SCALE
));
512 assertEquals("Context rank", 1001, context
.getRank());
514 event
= fExperiment
.getNext(context
);
515 assertEquals("Event timestamp", 1002, event
.getTimestamp().getValue());
516 assertEquals("Context rank", 1002, context
.getRank());
518 // Position trace at event rank 4500 (TS = 4501)
519 context
= fExperiment
.seekEvent(TmfTimestamp
.create(4501, SCALE
));
520 assertEquals("Context rank", 4500, context
.getRank());
522 event
= fExperiment
.getNext(context
);
523 assertEquals("Event timestamp", 4501, event
.getTimestamp().getValue());
524 assertEquals("Context rank", 4501, context
.getRank());
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());
533 ITmfEvent event
= fExperiment
.getNext(context
);
534 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
535 assertEquals("Event rank", 1, context
.getRank());
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());
544 // ------------------------------------------------------------------------
545 // seekEvent by location (context rank is undefined)
546 // ------------------------------------------------------------------------
549 public void testSeekLocationOnCacheBoundary() {
550 long cacheSize
= fExperiment
.getCacheSize();
552 // Position trace at event rank 0
553 ITmfContext tmpContext
= fExperiment
.seekEvent(0);
554 ITmfContext context
= fExperiment
.seekEvent(tmpContext
.getLocation());
556 ITmfEvent event
= fExperiment
.getNext(context
);
557 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
559 event
= fExperiment
.getNext(context
);
560 assertEquals("Event timestamp", 2, event
.getTimestamp().getValue());
562 // Position trace at event rank 'cacheSize'
563 tmpContext
= fExperiment
.seekEvent(cacheSize
);
564 context
= fExperiment
.seekEvent(tmpContext
.getLocation());
566 event
= fExperiment
.getNext(context
);
567 assertEquals("Event timestamp", cacheSize
+ 1, event
.getTimestamp().getValue());
569 event
= fExperiment
.getNext(context
);
570 assertEquals("Event timestamp", cacheSize
+ 2, event
.getTimestamp().getValue());
572 // Position trace at event rank 4 * 'cacheSize'
573 tmpContext
= fExperiment
.seekEvent(4 * cacheSize
);
574 context
= fExperiment
.seekEvent(tmpContext
.getLocation());
576 event
= fExperiment
.getNext(context
);
577 assertEquals("Event timestamp", 4 * cacheSize
+ 1, event
.getTimestamp().getValue());
579 event
= fExperiment
.getNext(context
);
580 assertEquals("Event timestamp", 4 * cacheSize
+ 2, event
.getTimestamp().getValue());
584 public void testSeekLocationNotOnCacheBoundary() {
585 long cacheSize
= fExperiment
.getCacheSize();
587 // Position trace at event 'cacheSize' - 1
588 ITmfContext tmpContext
= fExperiment
.seekEvent(cacheSize
- 1);
589 ITmfContext context
= fExperiment
.seekEvent(tmpContext
.getLocation());
591 ITmfEvent event
= fExperiment
.getNext(context
);
592 assertEquals("Event timestamp", cacheSize
, event
.getTimestamp().getValue());
594 event
= fExperiment
.getNext(context
);
595 assertEquals("Event timestamp", cacheSize
+ 1, event
.getTimestamp().getValue());
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);
602 event
= fExperiment
.getNext(context
);
603 assertEquals("Event timestamp", 2 * cacheSize
, event
.getTimestamp().getValue());
605 event
= fExperiment
.getNext(context
);
606 assertEquals("Event timestamp", 2 * cacheSize
+ 1, event
.getTimestamp().getValue());
608 // Position trace at event rank 4500
609 tmpContext
= fExperiment
.seekEvent(4500);
610 context
= fExperiment
.seekEvent(tmpContext
.getLocation());
612 event
= fExperiment
.getNext(context
);
613 assertEquals("Event timestamp", 4501, event
.getTimestamp().getValue());
615 event
= fExperiment
.getNext(context
);
616 assertEquals("Event timestamp", 4502, event
.getTimestamp().getValue());
620 public void testSeekLocationOutOfScope() {
621 // Position trace at beginning
622 ITmfContext context
= fExperiment
.seekEvent((ITmfLocation
) null);
624 ITmfEvent event
= fExperiment
.getNext(context
);
625 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
628 // ------------------------------------------------------------------------
629 // getNext - updates the context
630 // ------------------------------------------------------------------------
632 private static void validateContextRanks(ITmfContext context
) {
633 assertTrue("Experiment context type", context
instanceof TmfExperimentContext
);
634 TmfExperimentContext ctx
= (TmfExperimentContext
) context
;
636 int nbTraces
= ctx
.getNbTraces();
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();
650 assertEquals("Experiment context rank", expRank
, ctx
.getRank());
654 public void testGetNextAfteSeekingOnTS_1() {
656 final long INITIAL_TS
= 1;
657 final int NB_READS
= 20;
659 // On lower bound, returns the first event (ts = 1)
660 final ITmfContext context
= fExperiment
.seekEvent(TmfTimestamp
.create(INITIAL_TS
, SCALE
));
662 validateContextRanks(context
);
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());
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());
677 validateContextRanks(context
);
681 public void testGetNextAfteSeekingOnTS_2() {
682 final long INITIAL_TS
= 2;
683 final int NB_READS
= 20;
685 // On lower bound, returns the first event (ts = 2)
686 final ITmfContext context
= fExperiment
.seekEvent(TmfTimestamp
.create(INITIAL_TS
, SCALE
));
688 validateContextRanks(context
);
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());
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());
703 validateContextRanks(context
);
707 public void testGetNextAfteSeekingOnTS_3() {
709 final long INITIAL_TS
= 500;
710 final int NB_READS
= 20;
712 // On lower bound, returns the first event (ts = 500)
713 final ITmfContext context
= fExperiment
.seekEvent(TmfTimestamp
.create(INITIAL_TS
, SCALE
));
715 validateContextRanks(context
);
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());
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());
730 validateContextRanks(context
);
734 public void testGetNextAfterSeekingOnRank_1() {
735 final long INITIAL_RANK
= 0L;
736 final int NB_READS
= 20;
738 // On lower bound, returns the first event (rank = 0)
739 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_RANK
);
741 validateContextRanks(context
);
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());
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());
756 validateContextRanks(context
);
760 public void testGetNextAfterSeekingOnRank_2() {
761 final long INITIAL_RANK
= 1L;
762 final int NB_READS
= 20;
764 // On lower bound, returns the first event (rank = 0)
765 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_RANK
);
767 validateContextRanks(context
);
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());
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());
782 validateContextRanks(context
);
786 public void testGetNextAfterSeekingOnRank_3() {
787 final long INITIAL_RANK
= 500L;
788 final int NB_READS
= 20;
790 // On lower bound, returns the first event (rank = 0)
791 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_RANK
);
793 validateContextRanks(context
);
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());
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());
808 validateContextRanks(context
);
812 public void testGetNextAfterSeekingOnLocation_1() {
813 final ITmfLocation INITIAL_LOC
= null;
814 final long INITIAL_TS
= 1;
815 final int NB_READS
= 20;
817 // On lower bound, returns the first event (ts = 1)
818 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_LOC
);
820 validateContextRanks(context
);
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());
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());
835 validateContextRanks(context
);
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;
844 // On lower bound, returns the first event (ts = 2)
845 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_LOC
);
847 validateContextRanks(context
);
851 for (int i
= 0; i
< NB_READS
; i
++) {
852 event
= fExperiment
.getNext(context
);
853 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
856 // Make sure we stay positioned
857 event
= fExperiment
.parseEvent(context
);
858 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
860 validateContextRanks(context
);
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;
869 // On lower bound, returns the first event (ts = 501)
870 final ITmfContext context
= fExperiment
.seekEvent(INITIAL_LOC
);
872 validateContextRanks(context
);
876 for (int i
= 0; i
< NB_READS
; i
++) {
877 event
= fExperiment
.getNext(context
);
878 assertEquals("Event timestamp", INITIAL_TS
+ i
, event
.getTimestamp().getValue());
881 // Make sure we stay positioned
882 event
= fExperiment
.parseEvent(context
);
883 assertEquals("Event timestamp", INITIAL_TS
+ NB_READS
, event
.getTimestamp().getValue());
885 validateContextRanks(context
);
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());
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
);
909 // ------------------------------------------------------------------------
911 // ------------------------------------------------------------------------
914 public void testProcessRequestForNbEvents() throws InterruptedException
{
915 final int nbEvents
= 1000;
916 final Vector
<ITmfEvent
> requestedEvents
= new Vector
<>();
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
) {
922 public void handleData(final ITmfEvent event
) {
923 super.handleData(event
);
924 requestedEvents
.add(event
);
927 fExperiment
.sendRequest(request
);
928 request
.waitForCompletion();
930 assertEquals("nbEvents", nbEvents
, requestedEvents
.size());
931 assertTrue("isCompleted", request
.isCompleted());
932 assertFalse("isCancelled", request
.isCancelled());
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());
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
;
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
) {
951 public void handleData(final ITmfEvent event
) {
952 super.handleData(event
);
953 requestedEvents
.add(event
);
956 fExperiment
.sendRequest(request
);
957 request
.waitForCompletion();
959 assertEquals("nbEvents", nbExpectedEvents
, requestedEvents
.size());
960 assertTrue("isCompleted", request
.isCompleted());
961 assertFalse("isCancelled", request
.isCancelled());
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());
970 // ------------------------------------------------------------------------
972 // ------------------------------------------------------------------------
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
<>();
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
) {
986 public void handleData(final ITmfEvent event
) {
987 super.handleData(event
);
988 requestedEvents
.add(event
);
989 if (++nbRead
== limit
) {
995 public void handleCancel() {
996 if (requestedEvents
.size() < limit
) {
997 System
.out
.println("aie");
1001 fExperiment
.sendRequest(request
);
1002 request
.waitForCompletion();
1004 assertEquals("nbEvents", limit
, requestedEvents
.size());
1005 assertTrue("isCompleted", request
.isCompleted());
1006 assertTrue("isCancelled", request
.isCancelled());
1009 private static abstract class TestTrace
extends TmfXmlTraceStub
implements ITmfPropertiesProvider
{
1014 * Tests that experiment with traces from the same host and a clock offset
1015 * are well synchronized
1018 public void testWithSingleHostClockOffset() {
1019 // Data for this specific test
1020 String hostId
= "Test Host 1";
1021 long minOffset
= 2000;
1023 String clockOffset
= "clock_offset";
1025 ITmfTrace t1
= new TestTrace() {
1027 public @NonNull String
getHostId() {
1031 public @NonNull Map
<@NonNull String
, @NonNull String
> getProperties() {
1032 return ImmutableMap
.of(clockOffset
, String
.valueOf(minOffset
));
1036 ITmfTrace t2
= new TestTrace() {
1038 public @NonNull String
getHostId() {
1042 public @NonNull Map
<@NonNull String
, @NonNull String
> getProperties() {
1043 return ImmutableMap
.of(clockOffset
, String
.valueOf(minOffset
+ offset
));
1047 TmfExperiment exp
= new TmfExperimentStub(EXPERIMENT
, new ITmfTrace
[] { t1
, t2
}, BLOCK_SIZE
);
1050 assertEquals(TimestampTransformFactory
.createWithOffset(offset
/ 2), t1
.getTimestampTransform());
1051 assertEquals(TimestampTransformFactory
.createWithOffset(-offset
/ 2), t2
.getTimestampTransform());
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.
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;
1071 String clockOffset
= "clock_offset";
1073 ITmfTimestampTransform tt1
= TimestampTransformFactory
.createLinear(2.0, offset
/ 2);
1074 ITmfTimestampTransform tt2
= TimestampTransformFactory
.createLinear(2.0, -offset
/ 2);
1075 ITmfTimestampTransform tt3
= TimestampTransformFactory
.createWithOffset(offset
);
1077 ITmfTrace t1
= new TestTrace() {
1079 public @NonNull String
getHostId() {
1083 public @NonNull Map
<@NonNull String
, @NonNull String
> getProperties() {
1084 return ImmutableMap
.of(clockOffset
, String
.valueOf(minOffset
));
1088 t1
.setTimestampTransform(tt1
);
1090 ITmfTrace t2
= new TestTrace() {
1092 public @NonNull String
getHostId() {
1096 public @NonNull Map
<@NonNull String
, @NonNull String
> getProperties() {
1097 return ImmutableMap
.of(clockOffset
, String
.valueOf(minOffset
+ offset
));
1100 t2
.setTimestampTransform(tt2
);
1102 ITmfTrace t3
= new TmfXmlTraceStub() {
1104 public @NonNull String
getHostId() {
1108 t3
.setTimestampTransform(tt3
);
1110 TmfExperiment exp
= new TmfExperimentStub(EXPERIMENT
, new ITmfTrace
[] { t1
, t2
, t3
}, BLOCK_SIZE
) {
1113 public SynchronizationAlgorithm
synchronizeTraces() {
1114 return new SyncAlgorithmFullyIncremental() {
1116 private static final long serialVersionUID
= 4206172498287480153L;
1119 public ITmfTimestampTransform
getTimestampTransform(String h
) {
1120 if (hostId
.equals(h
)) {
1121 return TimestampTransformFactory
.createLinear(2.0, 0);
1130 assertEquals(tt1
, t1
.getTimestampTransform());
1131 assertEquals(tt2
, t2
.getTimestampTransform());
1132 assertEquals(tt3
, t3
.getTimestampTransform());