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
.TmfEvent
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimeRange
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.tests
.TmfCoreTestPlugin
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfCheckpoint
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
38 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfExperimentStub
;
39 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfTraceStub
;
42 * Test suite for the TmfCheckpointIndexTest class.
44 @SuppressWarnings({"nls","javadoc"})
45 public class TmfExperimentCheckpointIndexTest
extends TestCase
{
47 // ------------------------------------------------------------------------
49 // ------------------------------------------------------------------------
51 private static final String DIRECTORY
= "testfiles";
52 private static final String TEST_STREAM1
= "O-Test-10K";
53 private static final String TEST_STREAM2
= "E-Test-10K";
54 private static final String EXPERIMENT
= "MyExperiment";
55 private static int NB_EVENTS
= 20000;
56 private static int BLOCK_SIZE
= 1000;
58 private static ITmfTrace
<TmfEvent
>[] fTestTraces
;
59 private static TmfExperimentStub
<TmfEvent
> fExperiment
;
61 // ------------------------------------------------------------------------
63 // ------------------------------------------------------------------------
66 * @param name the test name
68 public TmfExperimentCheckpointIndexTest(final String name
) {
73 protected synchronized void setUp() throws Exception
{
75 if (fExperiment
== null) {
76 setupTrace(DIRECTORY
+ File
.separator
+ TEST_STREAM1
, DIRECTORY
+ File
.separator
+ TEST_STREAM2
);
77 fExperiment
= new TmfExperimentStub
<TmfEvent
>(EXPERIMENT
, fTestTraces
, BLOCK_SIZE
);
78 fExperiment
.getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, true);
83 protected void tearDown() throws Exception
{
85 fExperiment
.dispose();
90 @SuppressWarnings("unchecked")
91 private synchronized static ITmfTrace
<?
>[] setupTrace(final String path1
, final String path2
) {
92 if (fTestTraces
== null) {
93 fTestTraces
= new ITmfTrace
[2];
95 URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(path1
), null);
96 File test
= new File(FileLocator
.toFileURL(location
).toURI());
97 final TmfTraceStub trace1
= new TmfTraceStub(test
.getPath(), 0, true);
98 fTestTraces
[0] = trace1
;
99 location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(path2
), null);
100 test
= new File(FileLocator
.toFileURL(location
).toURI());
101 final TmfTraceStub trace2
= new TmfTraceStub(test
.getPath(), 0, true);
102 fTestTraces
[1] = trace2
;
103 } catch (final TmfTraceException e
) {
105 } catch (final URISyntaxException e
) {
107 } catch (final IOException e
) {
114 // ------------------------------------------------------------------------
115 // Verify checkpoints
116 // ------------------------------------------------------------------------
118 @SuppressWarnings("null")
119 public void testTmfTraceIndexing() {
120 assertEquals("getCacheSize", BLOCK_SIZE
, fExperiment
.getCacheSize());
121 assertEquals("getTraceSize", NB_EVENTS
, fExperiment
.getNbEvents());
122 assertEquals("getRange-start", 1, fExperiment
.getTimeRange().getStartTime().getValue());
123 assertEquals("getRange-end", NB_EVENTS
, fExperiment
.getTimeRange().getEndTime().getValue());
124 assertEquals("getStartTime", 1, fExperiment
.getStartTime().getValue());
125 assertEquals("getEndTime", NB_EVENTS
, fExperiment
.getEndTime().getValue());
127 List
<ITmfCheckpoint
> checkpoints
= fExperiment
.getIndexer().getCheckpoints();
128 int pageSize
= fExperiment
.getCacheSize();
129 assertTrue("Checkpoints exist", checkpoints
!= null);
130 assertEquals("Checkpoints size", NB_EVENTS
/ BLOCK_SIZE
, checkpoints
.size());
132 // Validate that each checkpoint points to the right event
133 for (int i
= 0; i
< checkpoints
.size(); i
++) {
134 ITmfCheckpoint checkpoint
= checkpoints
.get(i
);
135 TmfExperimentLocation expLocation
= (TmfExperimentLocation
) checkpoint
.getLocation();
136 TmfLocationArray locations
= expLocation
.getLocation();
137 ITmfContext
[] trcContexts
= new ITmfContext
[2];
138 trcContexts
[0] = new TmfContext(locations
.getLocations()[0], (i
* pageSize
) / 2);
139 trcContexts
[1] = new TmfContext(locations
.getLocations()[1], (i
* pageSize
) / 2);
140 TmfExperimentContext expContext
= new TmfExperimentContext(trcContexts
);
141 expContext
.getEvents()[0] = fTestTraces
[0].getNext(fTestTraces
[0].seekEvent((i
* pageSize
) / 2));
142 expContext
.getEvents()[1] = fTestTraces
[1].getNext(fTestTraces
[1].seekEvent((i
* pageSize
) / 2));
143 ITmfEvent event
= fExperiment
.parseEvent(expContext
);
144 assertTrue(expContext
.getRank() == i
* pageSize
);
145 assertTrue((checkpoint
.getTimestamp().compareTo(event
.getTimestamp(), false) == 0));
149 // ------------------------------------------------------------------------
151 // ------------------------------------------------------------------------
153 @SuppressWarnings("null")
154 public void testGrowingIndex() {
156 ITmfTrace
<TmfEvent
>[] testTraces
= new TmfTraceStub
[2];
158 URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(DIRECTORY
+ File
.separator
+ TEST_STREAM1
), null);
159 File test
= new File(FileLocator
.toFileURL(location
).toURI());
160 final TmfTraceStub trace1
= new TmfTraceStub(test
.getPath(), 0, false);
161 testTraces
[0] = trace1
;
162 location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(DIRECTORY
+ File
.separator
+ TEST_STREAM2
), null);
163 test
= new File(FileLocator
.toFileURL(location
).toURI());
164 final TmfTraceStub trace2
= new TmfTraceStub(test
.getPath(), 0, false);
165 testTraces
[1] = trace2
;
166 } catch (final TmfTraceException e
) {
168 } catch (final URISyntaxException e
) {
170 } catch (final IOException e
) {
174 TmfExperimentStub
<TmfEvent
> experiment
= new TmfExperimentStub
<TmfEvent
>(EXPERIMENT
, testTraces
, BLOCK_SIZE
);
175 int pageSize
= experiment
.getCacheSize();
177 // Build the first half of the index
178 TmfTimeRange range
= new TmfTimeRange(new TmfTimestamp(1, -3), new TmfTimestamp(NB_EVENTS
/ 2 - 1, -3));
179 experiment
.getIndexer().buildIndex(0, range
, true);
181 // Validate that each checkpoint points to the right event
182 List
<ITmfCheckpoint
> checkpoints
= experiment
.getIndexer().getCheckpoints();
183 assertTrue("Checkpoints exist", checkpoints
!= null);
184 assertEquals("Checkpoints size", NB_EVENTS
/ BLOCK_SIZE
/ 2, checkpoints
.size());
186 // Build the second half of the index
187 experiment
.getIndexer().buildIndex(NB_EVENTS
/ 2, TmfTimeRange
.ETERNITY
, true);
189 // Validate that each checkpoint points to the right event
190 assertEquals("Checkpoints size", NB_EVENTS
/ BLOCK_SIZE
, checkpoints
.size());
191 for (int i
= 0; i
< checkpoints
.size(); i
++) {
192 ITmfCheckpoint checkpoint
= checkpoints
.get(i
);
193 TmfExperimentLocation expLocation
= (TmfExperimentLocation
) checkpoint
.getLocation();
194 TmfLocationArray locations
= expLocation
.getLocation();
195 ITmfContext
[] trcContexts
= new ITmfContext
[2];
196 trcContexts
[0] = new TmfContext(locations
.getLocations()[0], (i
* pageSize
) / 2);
197 trcContexts
[1] = new TmfContext(locations
.getLocations()[1], (i
* pageSize
) / 2);
198 TmfExperimentContext expContext
= new TmfExperimentContext(trcContexts
);
199 expContext
.getEvents()[0] = testTraces
[0].getNext(testTraces
[0].seekEvent((i
* pageSize
) / 2));
200 expContext
.getEvents()[1] = testTraces
[1].getNext(testTraces
[1].seekEvent((i
* pageSize
) / 2));
201 ITmfEvent event
= experiment
.parseEvent(expContext
);
202 assertTrue(expContext
.getRank() == i
* pageSize
);
203 assertTrue((checkpoint
.getTimestamp().compareTo(event
.getTimestamp(), false) == 0));
204 assertEquals("Checkpoint value", i
* pageSize
+ 1, checkpoint
.getTimestamp().getValue());