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