1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Francois Chouinard - Initial API and implementation
11 * Alexandre Montplaisir - Port to JUnit4
12 * Patrick Tasse - Updated for ranks in experiment location
13 *******************************************************************************/
15 package org
.eclipse
.linuxtools
.tmf
.core
.tests
.trace
.indexer
.checkpoint
;
17 import static org
.junit
.Assert
.assertEquals
;
18 import static org
.junit
.Assert
.assertTrue
;
21 import java
.io
.IOException
;
22 import java
.net
.URISyntaxException
;
25 import org
.eclipse
.core
.runtime
.FileLocator
;
26 import org
.eclipse
.core
.runtime
.Path
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.tests
.TmfCoreTestPlugin
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.tests
.shared
.TmfTestTrace
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimeRange
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.checkpoint
.ITmfCheckpoint
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.checkpoint
.ITmfCheckpointIndex
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.location
.ITmfLocation
;
38 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfExperimentStub
;
39 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfTraceStub
;
40 import org
.junit
.After
;
41 import org
.junit
.Before
;
42 import org
.junit
.Test
;
45 * Test suite for the TmfCheckpointIndexTest class.
47 @SuppressWarnings("javadoc")
48 public class TmfExperimentCheckpointIndexTest
{
50 // ------------------------------------------------------------------------
52 // ------------------------------------------------------------------------
54 private static final String EXPERIMENT
= "MyExperiment";
55 private static final TmfTestTrace TEST_TRACE1
= TmfTestTrace
.O_TEST_10K
;
56 private static final TmfTestTrace TEST_TRACE2
= TmfTestTrace
.E_TEST_10K
;
57 private static int NB_EVENTS
= 20000;
58 private static int BLOCK_SIZE
= 1000;
60 private static ITmfTrace
[] fTestTraces
;
61 private static TmfExperimentStub fExperiment
;
63 // ------------------------------------------------------------------------
65 // ------------------------------------------------------------------------
70 fExperiment
= new TmfExperimentStub(EXPERIMENT
, fTestTraces
, BLOCK_SIZE
);
71 fExperiment
.getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, true);
75 public void tearDown() {
76 fExperiment
.dispose();
78 for (ITmfTrace trace
: fTestTraces
) {
84 private static void setupTraces() {
86 fTestTraces
= new ITmfTrace
[2];
88 URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(TEST_TRACE1
.getFullPath()), null);
89 File test
= new File(FileLocator
.toFileURL(location
).toURI());
90 final TmfTraceStub trace1
= new TmfTraceStub(test
.getPath(), 0, true, null, null);
91 fTestTraces
[0] = trace1
;
92 location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(TEST_TRACE2
.getFullPath()), null);
93 test
= new File(FileLocator
.toFileURL(location
).toURI());
94 final TmfTraceStub trace2
= new TmfTraceStub(test
.getPath(), 0, true, null, null);
95 fTestTraces
[1] = trace2
;
96 } catch (final TmfTraceException e
) {
98 } catch (final URISyntaxException e
) {
100 } catch (final IOException e
) {
105 // ------------------------------------------------------------------------
106 // Verify checkpoints
107 // ------------------------------------------------------------------------
110 public void testTmfTraceIndexing() {
111 assertEquals("getCacheSize", BLOCK_SIZE
, fExperiment
.getCacheSize());
112 assertEquals("getTraceSize", NB_EVENTS
, fExperiment
.getNbEvents());
113 assertEquals("getRange-start", 1, fExperiment
.getTimeRange().getStartTime().getValue());
114 assertEquals("getRange-end", NB_EVENTS
, fExperiment
.getTimeRange().getEndTime().getValue());
115 assertEquals("getStartTime", 1, fExperiment
.getStartTime().getValue());
116 assertEquals("getEndTime", NB_EVENTS
, fExperiment
.getEndTime().getValue());
118 ITmfCheckpointIndex checkpoints
= fExperiment
.getIndexer().getCheckpoints();
119 int pageSize
= fExperiment
.getCacheSize();
120 assertTrue("Checkpoints exist", checkpoints
!= null);
121 assertEquals("Checkpoints size", NB_EVENTS
/ BLOCK_SIZE
, checkpoints
.size());
123 // Validate that each checkpoint points to the right event
124 for (int i
= 0; i
< checkpoints
.size(); i
++) {
125 ITmfCheckpoint checkpoint
= checkpoints
.get(i
);
126 ITmfLocation location
= checkpoint
.getLocation();
127 ITmfContext context
= fExperiment
.seekEvent(location
);
128 ITmfEvent event
= fExperiment
.parseEvent(context
);
129 assertTrue(context
.getRank() == i
* pageSize
);
130 assertTrue((checkpoint
.getTimestamp().compareTo(event
.getTimestamp(), false) == 0));
134 // ------------------------------------------------------------------------
136 // ------------------------------------------------------------------------
139 public void testGrowingIndex() {
140 ITmfTrace
[] testTraces
= new TmfTraceStub
[2];
142 URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(TEST_TRACE1
.getFullPath()), null);
143 File test
= new File(FileLocator
.toFileURL(location
).toURI());
144 final TmfTraceStub trace1
= new TmfTraceStub(test
.getPath(), 0, false, null, null);
145 testTraces
[0] = trace1
;
146 location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(TEST_TRACE2
.getFullPath()), null);
147 test
= new File(FileLocator
.toFileURL(location
).toURI());
148 final TmfTraceStub trace2
= new TmfTraceStub(test
.getPath(), 0, false, null, null);
149 testTraces
[1] = trace2
;
150 } catch (final TmfTraceException e
) {
152 } catch (final URISyntaxException e
) {
154 } catch (final IOException e
) {
158 TmfExperimentStub experiment
= new TmfExperimentStub(EXPERIMENT
, testTraces
, BLOCK_SIZE
);
159 int pageSize
= experiment
.getCacheSize();
161 // Build the first half of the index
162 TmfTimeRange range
= new TmfTimeRange(new TmfTimestamp(1, -3), new TmfTimestamp(NB_EVENTS
/ 2 - 1, -3));
163 experiment
.getIndexer().buildIndex(0, range
, true);
165 // Validate that each checkpoint points to the right event
166 ITmfCheckpointIndex checkpoints
= experiment
.getIndexer().getCheckpoints();
167 assertTrue("Checkpoints exist", checkpoints
!= null);
168 assertEquals("Checkpoints size", NB_EVENTS
/ BLOCK_SIZE
/ 2, checkpoints
.size());
170 // Build the second half of the index
171 experiment
.getIndexer().buildIndex(NB_EVENTS
/ 2, TmfTimeRange
.ETERNITY
, true);
173 // Validate that each checkpoint points to the right event
174 assertEquals("Checkpoints size", NB_EVENTS
/ BLOCK_SIZE
, checkpoints
.size());
175 for (int i
= 0; i
< checkpoints
.size(); i
++) {
176 ITmfCheckpoint checkpoint
= checkpoints
.get(i
);
177 ITmfLocation location
= checkpoint
.getLocation();
178 ITmfContext context
= experiment
.seekEvent(location
);
179 ITmfEvent event
= experiment
.parseEvent(context
);
180 assertTrue(context
.getRank() == i
* pageSize
);
181 assertTrue((checkpoint
.getTimestamp().compareTo(event
.getTimestamp(), false) == 0));
182 assertEquals("Checkpoint value", i
* pageSize
+ 1, checkpoint
.getTimestamp().getValue());
185 /* Clean up (since we didn't use the class-specific fixtures) */
186 experiment
.dispose();
187 for (ITmfTrace trace
: testTraces
) {