1 /*******************************************************************************
2 * Copyright (c) 2012 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 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.core
.tests
.trace
;
16 import java
.io
.IOException
;
17 import java
.net
.URISyntaxException
;
19 import java
.util
.List
;
21 import junit
.framework
.TestCase
;
23 import org
.eclipse
.core
.runtime
.FileLocator
;
24 import org
.eclipse
.core
.runtime
.Path
;
25 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.trace
.TmfExperimentContext
;
26 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.trace
.TmfExperimentLocation
;
27 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.trace
.TmfLocationArray
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimeRange
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.tests
.TmfCoreTestPlugin
;
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
.ITmfTrace
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
37 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfExperimentStub
;
38 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfTraceStub
;
41 * Test suite for the TmfCheckpointIndexTest class.
43 @SuppressWarnings({"nls","javadoc"})
44 public class TmfExperimentCheckpointIndexTest
extends TestCase
{
46 // ------------------------------------------------------------------------
48 // ------------------------------------------------------------------------
50 private static final String DIRECTORY
= "testfiles";
51 private static final String TEST_STREAM1
= "O-Test-10K";
52 private static final String TEST_STREAM2
= "E-Test-10K";
53 private static final String EXPERIMENT
= "MyExperiment";
54 private static int NB_EVENTS
= 20000;
55 private static int BLOCK_SIZE
= 1000;
57 private static ITmfTrace
[] fTestTraces
;
58 private static TmfExperimentStub fExperiment
;
60 // ------------------------------------------------------------------------
62 // ------------------------------------------------------------------------
65 * @param name the test name
67 public TmfExperimentCheckpointIndexTest(final String name
) {
72 protected synchronized void setUp() throws Exception
{
74 if (fExperiment
== null) {
75 setupTrace(DIRECTORY
+ File
.separator
+ TEST_STREAM1
, DIRECTORY
+ File
.separator
+ TEST_STREAM2
);
76 fExperiment
= new TmfExperimentStub(EXPERIMENT
, fTestTraces
, BLOCK_SIZE
);
77 fExperiment
.getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, true);
82 protected void tearDown() throws Exception
{
84 fExperiment
.dispose();
89 private synchronized static ITmfTrace
[] setupTrace(final String path1
, final String path2
) {
90 if (fTestTraces
== null) {
91 fTestTraces
= new ITmfTrace
[2];
93 URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(path1
), null);
94 File test
= new File(FileLocator
.toFileURL(location
).toURI());
95 final TmfTraceStub trace1
= new TmfTraceStub(test
.getPath(), 0, true);
96 fTestTraces
[0] = trace1
;
97 location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(path2
), null);
98 test
= new File(FileLocator
.toFileURL(location
).toURI());
99 final TmfTraceStub trace2
= new TmfTraceStub(test
.getPath(), 0, true);
100 fTestTraces
[1] = trace2
;
101 } catch (final TmfTraceException e
) {
103 } catch (final URISyntaxException e
) {
105 } catch (final IOException e
) {
112 // ------------------------------------------------------------------------
113 // Verify checkpoints
114 // ------------------------------------------------------------------------
116 @SuppressWarnings("null")
117 public void testTmfTraceIndexing() {
118 assertEquals("getCacheSize", BLOCK_SIZE
, fExperiment
.getCacheSize());
119 assertEquals("getTraceSize", NB_EVENTS
, fExperiment
.getNbEvents());
120 assertEquals("getRange-start", 1, fExperiment
.getTimeRange().getStartTime().getValue());
121 assertEquals("getRange-end", NB_EVENTS
, fExperiment
.getTimeRange().getEndTime().getValue());
122 assertEquals("getStartTime", 1, fExperiment
.getStartTime().getValue());
123 assertEquals("getEndTime", NB_EVENTS
, fExperiment
.getEndTime().getValue());
125 List
<ITmfCheckpoint
> checkpoints
= fExperiment
.getIndexer().getCheckpoints();
126 int pageSize
= fExperiment
.getCacheSize();
127 assertTrue("Checkpoints exist", checkpoints
!= null);
128 assertEquals("Checkpoints size", NB_EVENTS
/ BLOCK_SIZE
, checkpoints
.size());
130 // Validate that each checkpoint points to the right event
131 for (int i
= 0; i
< checkpoints
.size(); i
++) {
132 ITmfCheckpoint checkpoint
= checkpoints
.get(i
);
133 TmfExperimentLocation expLocation
= (TmfExperimentLocation
) checkpoint
.getLocation();
134 TmfLocationArray locations
= expLocation
.getLocation();
135 ITmfContext
[] trcContexts
= new ITmfContext
[2];
136 trcContexts
[0] = new TmfContext(locations
.getLocations()[0], (i
* pageSize
) / 2);
137 trcContexts
[1] = new TmfContext(locations
.getLocations()[1], (i
* pageSize
) / 2);
138 TmfExperimentContext expContext
= new TmfExperimentContext(trcContexts
);
139 expContext
.getEvents()[0] = fTestTraces
[0].getNext(fTestTraces
[0].seekEvent((i
* pageSize
) / 2));
140 expContext
.getEvents()[1] = fTestTraces
[1].getNext(fTestTraces
[1].seekEvent((i
* pageSize
) / 2));
141 ITmfEvent event
= fExperiment
.parseEvent(expContext
);
142 assertTrue(expContext
.getRank() == i
* pageSize
);
143 assertTrue((checkpoint
.getTimestamp().compareTo(event
.getTimestamp(), false) == 0));
147 // ------------------------------------------------------------------------
149 // ------------------------------------------------------------------------
151 @SuppressWarnings("null")
152 public void testGrowingIndex() {
154 ITmfTrace
[] testTraces
= new TmfTraceStub
[2];
156 URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(DIRECTORY
+ File
.separator
+ TEST_STREAM1
), null);
157 File test
= new File(FileLocator
.toFileURL(location
).toURI());
158 final TmfTraceStub trace1
= new TmfTraceStub(test
.getPath(), 0, false);
159 testTraces
[0] = trace1
;
160 location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(DIRECTORY
+ File
.separator
+ TEST_STREAM2
), null);
161 test
= new File(FileLocator
.toFileURL(location
).toURI());
162 final TmfTraceStub trace2
= new TmfTraceStub(test
.getPath(), 0, false);
163 testTraces
[1] = trace2
;
164 } catch (final TmfTraceException e
) {
166 } catch (final URISyntaxException e
) {
168 } catch (final IOException e
) {
172 TmfExperimentStub experiment
= new TmfExperimentStub(EXPERIMENT
, testTraces
, BLOCK_SIZE
);
173 int pageSize
= experiment
.getCacheSize();
175 // Build the first half of the index
176 TmfTimeRange range
= new TmfTimeRange(new TmfTimestamp(1, -3), new TmfTimestamp(NB_EVENTS
/ 2 - 1, -3));
177 experiment
.getIndexer().buildIndex(0, range
, true);
179 // Validate that each checkpoint points to the right event
180 List
<ITmfCheckpoint
> checkpoints
= experiment
.getIndexer().getCheckpoints();
181 assertTrue("Checkpoints exist", checkpoints
!= null);
182 assertEquals("Checkpoints size", NB_EVENTS
/ BLOCK_SIZE
/ 2, checkpoints
.size());
184 // Build the second half of the index
185 experiment
.getIndexer().buildIndex(NB_EVENTS
/ 2, TmfTimeRange
.ETERNITY
, true);
187 // Validate that each checkpoint points to the right event
188 assertEquals("Checkpoints size", NB_EVENTS
/ BLOCK_SIZE
, checkpoints
.size());
189 for (int i
= 0; i
< checkpoints
.size(); i
++) {
190 ITmfCheckpoint checkpoint
= checkpoints
.get(i
);
191 TmfExperimentLocation expLocation
= (TmfExperimentLocation
) checkpoint
.getLocation();
192 TmfLocationArray locations
= expLocation
.getLocation();
193 ITmfContext
[] trcContexts
= new ITmfContext
[2];
194 trcContexts
[0] = new TmfContext(locations
.getLocations()[0], (i
* pageSize
) / 2);
195 trcContexts
[1] = new TmfContext(locations
.getLocations()[1], (i
* pageSize
) / 2);
196 TmfExperimentContext expContext
= new TmfExperimentContext(trcContexts
);
197 expContext
.getEvents()[0] = testTraces
[0].getNext(testTraces
[0].seekEvent((i
* pageSize
) / 2));
198 expContext
.getEvents()[1] = testTraces
[1].getNext(testTraces
[1].seekEvent((i
* pageSize
) / 2));
199 ITmfEvent event
= experiment
.parseEvent(expContext
);
200 assertTrue(expContext
.getRank() == i
* pageSize
);
201 assertTrue((checkpoint
.getTimestamp().compareTo(event
.getTimestamp(), false) == 0));
202 assertEquals("Checkpoint value", i
* pageSize
+ 1, checkpoint
.getTimestamp().getValue());