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