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
;
17 import static org
.junit
.Assert
.assertEquals
;
18 import static org
.junit
.Assert
.assertTrue
;
21 import java
.io
.IOException
;
22 import java
.net
.URISyntaxException
;
24 import java
.util
.List
;
26 import org
.eclipse
.core
.runtime
.FileLocator
;
27 import org
.eclipse
.core
.runtime
.Path
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.tests
.TmfCoreTestPlugin
;
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
.ITmfCheckpoint
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
37 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfExperimentStub
;
38 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfTraceStub
;
39 import org
.junit
.After
;
40 import org
.junit
.Before
;
41 import org
.junit
.Test
;
44 * Test suite for the TmfCheckpointIndexTest class.
46 @SuppressWarnings("javadoc")
47 public class TmfExperimentCheckpointIndexTest
{
49 // ------------------------------------------------------------------------
51 // ------------------------------------------------------------------------
53 private static final String DIRECTORY
= "testfiles";
54 private static final String TEST_STREAM1
= "O-Test-10K";
55 private static final String TEST_STREAM2
= "E-Test-10K";
56 private static final String EXPERIMENT
= "MyExperiment";
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() {
85 final String path1
= DIRECTORY
+ File
.separator
+ TEST_STREAM1
;
86 final String path2
= DIRECTORY
+ File
.separator
+ TEST_STREAM2
;
88 fTestTraces
= new ITmfTrace
[2];
90 URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(path1
), null);
91 File test
= new File(FileLocator
.toFileURL(location
).toURI());
92 final TmfTraceStub trace1
= new TmfTraceStub(test
.getPath(), 0, true);
93 fTestTraces
[0] = trace1
;
94 location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(path2
), null);
95 test
= new File(FileLocator
.toFileURL(location
).toURI());
96 final TmfTraceStub trace2
= new TmfTraceStub(test
.getPath(), 0, true);
97 fTestTraces
[1] = trace2
;
98 } catch (final TmfTraceException e
) {
100 } catch (final URISyntaxException e
) {
102 } catch (final IOException e
) {
107 // ------------------------------------------------------------------------
108 // Verify checkpoints
109 // ------------------------------------------------------------------------
112 public void testTmfTraceIndexing() {
113 assertEquals("getCacheSize", BLOCK_SIZE
, fExperiment
.getCacheSize());
114 assertEquals("getTraceSize", NB_EVENTS
, fExperiment
.getNbEvents());
115 assertEquals("getRange-start", 1, fExperiment
.getTimeRange().getStartTime().getValue());
116 assertEquals("getRange-end", NB_EVENTS
, fExperiment
.getTimeRange().getEndTime().getValue());
117 assertEquals("getStartTime", 1, fExperiment
.getStartTime().getValue());
118 assertEquals("getEndTime", NB_EVENTS
, fExperiment
.getEndTime().getValue());
120 List
<ITmfCheckpoint
> checkpoints
= fExperiment
.getIndexer().getCheckpoints();
121 int pageSize
= fExperiment
.getCacheSize();
122 assertTrue("Checkpoints exist", checkpoints
!= null);
123 assertEquals("Checkpoints size", NB_EVENTS
/ BLOCK_SIZE
, checkpoints
.size());
125 // Validate that each checkpoint points to the right event
126 for (int i
= 0; i
< checkpoints
.size(); i
++) {
127 ITmfCheckpoint checkpoint
= checkpoints
.get(i
);
128 ITmfLocation location
= checkpoint
.getLocation();
129 ITmfContext context
= fExperiment
.seekEvent(location
);
130 ITmfEvent event
= fExperiment
.parseEvent(context
);
131 assertTrue(context
.getRank() == i
* pageSize
);
132 assertTrue((checkpoint
.getTimestamp().compareTo(event
.getTimestamp(), false) == 0));
136 // ------------------------------------------------------------------------
138 // ------------------------------------------------------------------------
141 public void testGrowingIndex() {
142 ITmfTrace
[] testTraces
= new TmfTraceStub
[2];
144 URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(DIRECTORY
+ File
.separator
+ TEST_STREAM1
), null);
145 File test
= new File(FileLocator
.toFileURL(location
).toURI());
146 final TmfTraceStub trace1
= new TmfTraceStub(test
.getPath(), 0, false);
147 testTraces
[0] = trace1
;
148 location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(DIRECTORY
+ File
.separator
+ TEST_STREAM2
), null);
149 test
= new File(FileLocator
.toFileURL(location
).toURI());
150 final TmfTraceStub trace2
= new TmfTraceStub(test
.getPath(), 0, false);
151 testTraces
[1] = trace2
;
152 } catch (final TmfTraceException e
) {
154 } catch (final URISyntaxException e
) {
156 } catch (final IOException e
) {
160 TmfExperimentStub experiment
= new TmfExperimentStub(EXPERIMENT
, testTraces
, BLOCK_SIZE
);
161 int pageSize
= experiment
.getCacheSize();
163 // Build the first half of the index
164 TmfTimeRange range
= new TmfTimeRange(new TmfTimestamp(1, -3), new TmfTimestamp(NB_EVENTS
/ 2 - 1, -3));
165 experiment
.getIndexer().buildIndex(0, range
, true);
167 // Validate that each checkpoint points to the right event
168 List
<ITmfCheckpoint
> checkpoints
= experiment
.getIndexer().getCheckpoints();
169 assertTrue("Checkpoints exist", checkpoints
!= null);
170 assertEquals("Checkpoints size", NB_EVENTS
/ BLOCK_SIZE
/ 2, checkpoints
.size());
172 // Build the second half of the index
173 experiment
.getIndexer().buildIndex(NB_EVENTS
/ 2, TmfTimeRange
.ETERNITY
, true);
175 // Validate that each checkpoint points to the right event
176 assertEquals("Checkpoints size", NB_EVENTS
/ BLOCK_SIZE
, checkpoints
.size());
177 for (int i
= 0; i
< checkpoints
.size(); i
++) {
178 ITmfCheckpoint checkpoint
= checkpoints
.get(i
);
179 ITmfLocation location
= checkpoint
.getLocation();
180 ITmfContext context
= experiment
.seekEvent(location
);
181 ITmfEvent event
= experiment
.parseEvent(context
);
182 assertTrue(context
.getRank() == i
* pageSize
);
183 assertTrue((checkpoint
.getTimestamp().compareTo(event
.getTimestamp(), false) == 0));
184 assertEquals("Checkpoint value", i
* pageSize
+ 1, checkpoint
.getTimestamp().getValue());
187 /* Clean up (since we didn't use the class-specific fixtures) */
188 experiment
.dispose();
189 for (ITmfTrace trace
: testTraces
) {