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" })
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 // ------------------------------------------------------------------------
65 public TmfExperimentCheckpointIndexTest(final String name
) throws Exception
{
70 protected synchronized void setUp() throws Exception
{
72 if (fExperiment
== null) {
73 setupTrace(DIRECTORY
+ File
.separator
+ TEST_STREAM1
, DIRECTORY
+ File
.separator
+ TEST_STREAM2
);
74 fExperiment
= new TmfExperimentStub
<TmfEvent
>(EXPERIMENT
, fTestTraces
, BLOCK_SIZE
);
75 fExperiment
.getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, true);
80 protected void tearDown() throws Exception
{
82 fExperiment
.dispose();
87 @SuppressWarnings("unchecked")
88 private synchronized static ITmfTrace
<?
>[] setupTrace(final String path1
, final String path2
) {
89 if (fTestTraces
== null) {
90 fTestTraces
= new ITmfTrace
[2];
92 URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(path1
), null);
93 File test
= new File(FileLocator
.toFileURL(location
).toURI());
94 final TmfTraceStub trace1
= new TmfTraceStub(test
.getPath(), 0, true);
95 fTestTraces
[0] = trace1
;
96 location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(path2
), null);
97 test
= new File(FileLocator
.toFileURL(location
).toURI());
98 final TmfTraceStub trace2
= new TmfTraceStub(test
.getPath(), 0, true);
99 fTestTraces
[1] = trace2
;
100 } catch (final TmfTraceException e
) {
102 } catch (final URISyntaxException e
) {
104 } catch (final IOException e
) {
111 // ------------------------------------------------------------------------
112 // Verify checkpoints
113 // ------------------------------------------------------------------------
115 public void testTmfTraceIndexing() throws Exception
{
116 assertEquals("getCacheSize", BLOCK_SIZE
, fExperiment
.getCacheSize());
117 assertEquals("getTraceSize", NB_EVENTS
, fExperiment
.getNbEvents());
118 assertEquals("getRange-start", 1, fExperiment
.getTimeRange().getStartTime().getValue());
119 assertEquals("getRange-end", NB_EVENTS
, fExperiment
.getTimeRange().getEndTime().getValue());
120 assertEquals("getStartTime", 1, fExperiment
.getStartTime().getValue());
121 assertEquals("getEndTime", NB_EVENTS
, fExperiment
.getEndTime().getValue());
123 List
<ITmfCheckpoint
> checkpoints
= fExperiment
.getIndexer().getCheckpoints();
124 int pageSize
= fExperiment
.getCacheSize();
125 assertTrue("Checkpoints exist", checkpoints
!= null);
126 assertEquals("Checkpoints size", NB_EVENTS
/ BLOCK_SIZE
, checkpoints
.size());
128 // Validate that each checkpoint points to the right event
129 for (int i
= 0; i
< checkpoints
.size(); i
++) {
130 ITmfCheckpoint checkpoint
= checkpoints
.get(i
);
131 TmfExperimentLocation expLocation
= (TmfExperimentLocation
) checkpoint
.getLocation();
132 TmfLocationArray locations
= expLocation
.getLocation();
133 ITmfContext
[] trcContexts
= new ITmfContext
[2];
134 trcContexts
[0] = new TmfContext(locations
.getLocations()[0], (i
* pageSize
) / 2);
135 trcContexts
[1] = new TmfContext(locations
.getLocations()[1], (i
* pageSize
) / 2);
136 TmfExperimentContext expContext
= new TmfExperimentContext(trcContexts
);
137 expContext
.getEvents()[0] = fTestTraces
[0].getNext(fTestTraces
[0].seekEvent((i
* pageSize
) / 2));
138 expContext
.getEvents()[1] = fTestTraces
[1].getNext(fTestTraces
[1].seekEvent((i
* pageSize
) / 2));
139 ITmfEvent event
= fExperiment
.parseEvent(expContext
);
140 assertTrue(expContext
.getRank() == i
* pageSize
);
141 assertTrue((checkpoint
.getTimestamp().compareTo(event
.getTimestamp(), false) == 0));
145 // ------------------------------------------------------------------------
147 // ------------------------------------------------------------------------
149 public void testGrowingIndex() throws Exception
{
151 ITmfTrace
<TmfEvent
>[] testTraces
= new TmfTraceStub
[2];
153 URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(DIRECTORY
+ File
.separator
+ TEST_STREAM1
), null);
154 File test
= new File(FileLocator
.toFileURL(location
).toURI());
155 final TmfTraceStub trace1
= new TmfTraceStub(test
.getPath(), 0, false);
156 testTraces
[0] = trace1
;
157 location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(DIRECTORY
+ File
.separator
+ TEST_STREAM2
), null);
158 test
= new File(FileLocator
.toFileURL(location
).toURI());
159 final TmfTraceStub trace2
= new TmfTraceStub(test
.getPath(), 0, false);
160 testTraces
[1] = trace2
;
161 } catch (final TmfTraceException e
) {
163 } catch (final URISyntaxException e
) {
165 } catch (final IOException e
) {
169 TmfExperimentStub
<TmfEvent
> experiment
= new TmfExperimentStub
<TmfEvent
>(EXPERIMENT
, testTraces
, BLOCK_SIZE
);
170 int pageSize
= experiment
.getCacheSize();
172 // Build the first half of the index
173 TmfTimeRange range
= new TmfTimeRange(new TmfTimestamp(1, -3), new TmfTimestamp(NB_EVENTS
/ 2 - 1, -3));
174 experiment
.getIndexer().buildIndex(0, range
, true);
176 // Validate that each checkpoint points to the right event
177 List
<ITmfCheckpoint
> checkpoints
= experiment
.getIndexer().getCheckpoints();
178 assertTrue("Checkpoints exist", checkpoints
!= null);
179 assertEquals("Checkpoints size", NB_EVENTS
/ BLOCK_SIZE
/ 2, checkpoints
.size());
181 // Build the second half of the index
182 experiment
.getIndexer().buildIndex(NB_EVENTS
/ 2, TmfTimeRange
.ETERNITY
, true);
184 // Validate that each checkpoint points to the right event
185 assertEquals("Checkpoints size", NB_EVENTS
/ BLOCK_SIZE
, checkpoints
.size());
186 for (int i
= 0; i
< checkpoints
.size(); i
++) {
187 ITmfCheckpoint checkpoint
= checkpoints
.get(i
);
188 TmfExperimentLocation expLocation
= (TmfExperimentLocation
) checkpoint
.getLocation();
189 TmfLocationArray locations
= expLocation
.getLocation();
190 ITmfContext
[] trcContexts
= new ITmfContext
[2];
191 trcContexts
[0] = new TmfContext(locations
.getLocations()[0], (i
* pageSize
) / 2);
192 trcContexts
[1] = new TmfContext(locations
.getLocations()[1], (i
* pageSize
) / 2);
193 TmfExperimentContext expContext
= new TmfExperimentContext(trcContexts
);
194 expContext
.getEvents()[0] = testTraces
[0].getNext(testTraces
[0].seekEvent((i
* pageSize
) / 2));
195 expContext
.getEvents()[1] = testTraces
[1].getNext(testTraces
[1].seekEvent((i
* pageSize
) / 2));
196 ITmfEvent event
= experiment
.parseEvent(expContext
);
197 assertTrue(expContext
.getRank() == i
* pageSize
);
198 assertTrue((checkpoint
.getTimestamp().compareTo(event
.getTimestamp(), false) == 0));
199 assertEquals("Checkpoint value", i
* pageSize
+ 1, checkpoint
.getTimestamp().getValue());