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