tmf: Drop generics from ITmfTrace and TmfExperiment
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / trace / TmfExperimentCheckpointIndexTest.java
1 /*******************************************************************************
2 * Copyright (c) 2012 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.core.tests.trace;
14
15 import java.io.File;
16 import java.io.IOException;
17 import java.net.URISyntaxException;
18 import java.net.URL;
19 import java.util.List;
20
21 import junit.framework.TestCase;
22
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;
39
40 /**
41 * Test suite for the TmfCheckpointIndexTest class.
42 */
43 @SuppressWarnings({"nls","javadoc"})
44 public class TmfExperimentCheckpointIndexTest extends TestCase {
45
46 // ------------------------------------------------------------------------
47 // Attributes
48 // ------------------------------------------------------------------------
49
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;
56
57 private static ITmfTrace[] fTestTraces;
58 private static TmfExperimentStub fExperiment;
59
60 // ------------------------------------------------------------------------
61 // Housekeeping
62 // ------------------------------------------------------------------------
63
64 /**
65 * @param name the test name
66 */
67 public TmfExperimentCheckpointIndexTest(final String name) {
68 super(name);
69 }
70
71 @Override
72 protected synchronized void setUp() throws Exception {
73 super.setUp();
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);
78 }
79 }
80
81 @Override
82 protected void tearDown() throws Exception {
83 super.tearDown();
84 fExperiment.dispose();
85 fExperiment = null;
86 fTestTraces = null;
87 }
88
89 private synchronized static ITmfTrace[] setupTrace(final String path1, final String path2) {
90 if (fTestTraces == null) {
91 fTestTraces = new ITmfTrace[2];
92 try {
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) {
102 e.printStackTrace();
103 } catch (final URISyntaxException e) {
104 e.printStackTrace();
105 } catch (final IOException e) {
106 e.printStackTrace();
107 }
108 }
109 return fTestTraces;
110 }
111
112 // ------------------------------------------------------------------------
113 // Verify checkpoints
114 // ------------------------------------------------------------------------
115
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());
124
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());
129
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));
144 }
145 }
146
147 // ------------------------------------------------------------------------
148 // Streaming
149 // ------------------------------------------------------------------------
150
151 @SuppressWarnings("null")
152 public void testGrowingIndex() {
153
154 ITmfTrace[] testTraces = new TmfTraceStub[2];
155 try {
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) {
165 e.printStackTrace();
166 } catch (final URISyntaxException e) {
167 e.printStackTrace();
168 } catch (final IOException e) {
169 e.printStackTrace();
170 }
171
172 TmfExperimentStub experiment = new TmfExperimentStub(EXPERIMENT, testTraces, BLOCK_SIZE);
173 int pageSize = experiment.getCacheSize();
174
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);
178
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());
183
184 // Build the second half of the index
185 experiment.getIndexer().buildIndex(NB_EVENTS / 2, TmfTimeRange.ETERNITY, true);
186
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());
203 }
204 }
205
206 }
This page took 0.036985 seconds and 6 git commands to generate.