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 @SuppressWarnings("null")
113 public void testTmfTraceIndexing() {
114 assertEquals("getCacheSize", BLOCK_SIZE
, fExperiment
.getCacheSize());
115 assertEquals("getTraceSize", NB_EVENTS
, fExperiment
.getNbEvents());
116 assertEquals("getRange-start", 1, fExperiment
.getTimeRange().getStartTime().getValue());
117 assertEquals("getRange-end", NB_EVENTS
, fExperiment
.getTimeRange().getEndTime().getValue());
118 assertEquals("getStartTime", 1, fExperiment
.getStartTime().getValue());
119 assertEquals("getEndTime", NB_EVENTS
, fExperiment
.getEndTime().getValue());
121 List
<ITmfCheckpoint
> checkpoints
= fExperiment
.getIndexer().getCheckpoints();
122 int pageSize
= fExperiment
.getCacheSize();
123 assertTrue("Checkpoints exist", checkpoints
!= null);
124 assertEquals("Checkpoints size", NB_EVENTS
/ BLOCK_SIZE
, checkpoints
.size());
126 // Validate that each checkpoint points to the right event
127 for (int i
= 0; i
< checkpoints
.size(); i
++) {
128 ITmfCheckpoint checkpoint
= checkpoints
.get(i
);
129 ITmfLocation location
= checkpoint
.getLocation();
130 ITmfContext context
= fExperiment
.seekEvent(location
);
131 ITmfEvent event
= fExperiment
.parseEvent(context
);
132 assertTrue(context
.getRank() == i
* pageSize
);
133 assertTrue((checkpoint
.getTimestamp().compareTo(event
.getTimestamp(), false) == 0));
137 // ------------------------------------------------------------------------
139 // ------------------------------------------------------------------------
142 @SuppressWarnings("null")
143 public void testGrowingIndex() {
144 ITmfTrace
[] testTraces
= new TmfTraceStub
[2];
146 URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(DIRECTORY
+ File
.separator
+ TEST_STREAM1
), null);
147 File test
= new File(FileLocator
.toFileURL(location
).toURI());
148 final TmfTraceStub trace1
= new TmfTraceStub(test
.getPath(), 0, false);
149 testTraces
[0] = trace1
;
150 location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(DIRECTORY
+ File
.separator
+ TEST_STREAM2
), null);
151 test
= new File(FileLocator
.toFileURL(location
).toURI());
152 final TmfTraceStub trace2
= new TmfTraceStub(test
.getPath(), 0, false);
153 testTraces
[1] = trace2
;
154 } catch (final TmfTraceException e
) {
156 } catch (final URISyntaxException e
) {
158 } catch (final IOException e
) {
162 TmfExperimentStub experiment
= new TmfExperimentStub(EXPERIMENT
, testTraces
, BLOCK_SIZE
);
163 int pageSize
= experiment
.getCacheSize();
165 // Build the first half of the index
166 TmfTimeRange range
= new TmfTimeRange(new TmfTimestamp(1, -3), new TmfTimestamp(NB_EVENTS
/ 2 - 1, -3));
167 experiment
.getIndexer().buildIndex(0, range
, true);
169 // Validate that each checkpoint points to the right event
170 List
<ITmfCheckpoint
> checkpoints
= experiment
.getIndexer().getCheckpoints();
171 assertTrue("Checkpoints exist", checkpoints
!= null);
172 assertEquals("Checkpoints size", NB_EVENTS
/ BLOCK_SIZE
/ 2, checkpoints
.size());
174 // Build the second half of the index
175 experiment
.getIndexer().buildIndex(NB_EVENTS
/ 2, TmfTimeRange
.ETERNITY
, true);
177 // Validate that each checkpoint points to the right event
178 assertEquals("Checkpoints size", NB_EVENTS
/ BLOCK_SIZE
, checkpoints
.size());
179 for (int i
= 0; i
< checkpoints
.size(); i
++) {
180 ITmfCheckpoint checkpoint
= checkpoints
.get(i
);
181 ITmfLocation location
= checkpoint
.getLocation();
182 ITmfContext context
= experiment
.seekEvent(location
);
183 ITmfEvent event
= experiment
.parseEvent(context
);
184 assertTrue(context
.getRank() == i
* pageSize
);
185 assertTrue((checkpoint
.getTimestamp().compareTo(event
.getTimestamp(), false) == 0));
186 assertEquals("Checkpoint value", i
* pageSize
+ 1, checkpoint
.getTimestamp().getValue());
189 /* Clean up (since we didn't use the class-specific fixtures) */
190 experiment
.dispose();
191 for (ITmfTrace trace
: testTraces
) {