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
.exceptions
.TmfTraceException
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.tests
.TmfCoreTestPlugin
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfCheckpoint
;
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", "restriction" })
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
<TmfEvent
>[] fTestTraces
;
58 private static TmfExperimentStub
<TmfEvent
> fExperiment
;
60 // ------------------------------------------------------------------------
62 // ------------------------------------------------------------------------
64 public TmfExperimentCheckpointIndexTest(final String name
) throws Exception
{
69 protected synchronized void setUp() throws Exception
{
71 setupTrace(DIRECTORY
+ File
.separator
+ TEST_STREAM1
, DIRECTORY
+ File
.separator
+ TEST_STREAM2
);
72 if (fExperiment
== null) {
73 fExperiment
= new TmfExperimentStub
<TmfEvent
>(EXPERIMENT
, fTestTraces
, BLOCK_SIZE
);
74 fExperiment
.getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, true);
79 protected void tearDown() throws Exception
{
81 fExperiment
.dispose();
85 @SuppressWarnings("unchecked")
86 private synchronized static ITmfTrace
<?
>[] setupTrace(final String path1
, final String path2
) {
87 if (fTestTraces
== null) {
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
) {
109 // ------------------------------------------------------------------------
110 // Verify checkpoints
111 // ------------------------------------------------------------------------
113 public void testTmfTraceIndexing() throws Exception
{
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
<TmfCheckpoint
> 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 TmfCheckpoint checkpoint
= checkpoints
.get(i
);
129 TmfExperimentLocation expLocation
= (TmfExperimentLocation
) checkpoint
.getLocation();
130 TmfLocationArray locations
= expLocation
.getLocation();
131 ITmfContext
[] trcContexts
= new ITmfContext
[2];
132 trcContexts
[0] = new TmfContext(locations
.getLocations()[0], (i
* pageSize
) / 2);
133 trcContexts
[1] = new TmfContext(locations
.getLocations()[1], (i
* pageSize
) / 2);
134 TmfExperimentContext expContext
= new TmfExperimentContext(trcContexts
);
135 expContext
.getEvents()[0] = fTestTraces
[0].getNext(fTestTraces
[0].seekEvent((i
* pageSize
) / 2));
136 expContext
.getEvents()[1] = fTestTraces
[1].getNext(fTestTraces
[1].seekEvent((i
* pageSize
) / 2));
137 ITmfEvent event
= fExperiment
.parseEvent(expContext
);
138 assertTrue(expContext
.getRank() == i
* pageSize
);
139 assertTrue((checkpoint
.getTimestamp().compareTo(event
.getTimestamp(), false) == 0));