Commit | Line | Data |
---|---|---|
07671572 | 1 | /******************************************************************************* |
6e1886bc | 2 | * Copyright (c) 2012, 2013 Ericsson |
07671572 FC |
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 | |
6e1886bc | 11 | * Alexandre Montplaisir - Port to JUnit4 |
07671572 FC |
12 | *******************************************************************************/ |
13 | ||
9e0640dc | 14 | package org.eclipse.linuxtools.tmf.core.tests.trace; |
07671572 | 15 | |
6e1886bc AM |
16 | import static org.junit.Assert.assertEquals; |
17 | import static org.junit.Assert.assertTrue; | |
18 | ||
07671572 FC |
19 | import java.io.File; |
20 | import java.io.IOException; | |
21 | import java.net.URISyntaxException; | |
22 | import java.net.URL; | |
23 | import java.util.List; | |
24 | ||
07671572 FC |
25 | import org.eclipse.core.runtime.FileLocator; |
26 | import org.eclipse.core.runtime.Path; | |
9e0640dc FC |
27 | import org.eclipse.linuxtools.internal.tmf.core.trace.TmfExperimentContext; |
28 | import org.eclipse.linuxtools.internal.tmf.core.trace.TmfExperimentLocation; | |
29 | import org.eclipse.linuxtools.internal.tmf.core.trace.TmfLocationArray; | |
07671572 | 30 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
9e0640dc | 31 | import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange; |
3427112b | 32 | import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp; |
07671572 | 33 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; |
07671572 | 34 | import org.eclipse.linuxtools.tmf.core.tests.TmfCoreTestPlugin; |
3427112b | 35 | import org.eclipse.linuxtools.tmf.core.trace.ITmfCheckpoint; |
07671572 FC |
36 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; |
37 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; | |
07671572 | 38 | import org.eclipse.linuxtools.tmf.core.trace.TmfContext; |
9e0640dc | 39 | import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfExperimentStub; |
07671572 | 40 | import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub; |
6e1886bc AM |
41 | import org.junit.After; |
42 | import org.junit.Before; | |
43 | import org.junit.Test; | |
07671572 FC |
44 | |
45 | /** | |
46 | * Test suite for the TmfCheckpointIndexTest class. | |
47 | */ | |
54a7a54c | 48 | @SuppressWarnings({"nls","javadoc"}) |
6e1886bc | 49 | public class TmfExperimentCheckpointIndexTest { |
07671572 FC |
50 | |
51 | // ------------------------------------------------------------------------ | |
52 | // Attributes | |
53 | // ------------------------------------------------------------------------ | |
54 | ||
55 | private static final String DIRECTORY = "testfiles"; | |
56 | private static final String TEST_STREAM1 = "O-Test-10K"; | |
57 | private static final String TEST_STREAM2 = "E-Test-10K"; | |
58 | private static final String EXPERIMENT = "MyExperiment"; | |
59 | private static int NB_EVENTS = 20000; | |
60 | private static int BLOCK_SIZE = 1000; | |
61 | ||
6256d8ad AM |
62 | private static ITmfTrace[] fTestTraces; |
63 | private static TmfExperimentStub fExperiment; | |
07671572 FC |
64 | |
65 | // ------------------------------------------------------------------------ | |
66 | // Housekeeping | |
67 | // ------------------------------------------------------------------------ | |
68 | ||
6e1886bc AM |
69 | @Before |
70 | public void setUp() { | |
71 | setupTraces(); | |
72 | fExperiment = new TmfExperimentStub(EXPERIMENT, fTestTraces, BLOCK_SIZE); | |
73 | fExperiment.getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, true); | |
07671572 FC |
74 | } |
75 | ||
6e1886bc AM |
76 | @After |
77 | public void tearDown() { | |
07671572 FC |
78 | fExperiment.dispose(); |
79 | fExperiment = null; | |
6e1886bc AM |
80 | for (ITmfTrace trace : fTestTraces) { |
81 | trace.dispose(); | |
82 | } | |
3427112b | 83 | fTestTraces = null; |
07671572 FC |
84 | } |
85 | ||
6e1886bc AM |
86 | private static void setupTraces() { |
87 | final String path1 = DIRECTORY + File.separator + TEST_STREAM1; | |
88 | final String path2 = DIRECTORY + File.separator + TEST_STREAM2; | |
89 | ||
90 | fTestTraces = new ITmfTrace[2]; | |
91 | try { | |
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) { | |
101 | e.printStackTrace(); | |
102 | } catch (final URISyntaxException e) { | |
103 | e.printStackTrace(); | |
104 | } catch (final IOException e) { | |
105 | e.printStackTrace(); | |
07671572 | 106 | } |
07671572 FC |
107 | } |
108 | ||
109 | // ------------------------------------------------------------------------ | |
110 | // Verify checkpoints | |
111 | // ------------------------------------------------------------------------ | |
112 | ||
6e1886bc | 113 | @Test |
54a7a54c FC |
114 | @SuppressWarnings("null") |
115 | public void testTmfTraceIndexing() { | |
07671572 FC |
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()); | |
122 | ||
3427112b | 123 | List<ITmfCheckpoint> checkpoints = fExperiment.getIndexer().getCheckpoints(); |
07671572 FC |
124 | int pageSize = fExperiment.getCacheSize(); |
125 | assertTrue("Checkpoints exist", checkpoints != null); | |
126 | assertEquals("Checkpoints size", NB_EVENTS / BLOCK_SIZE, checkpoints.size()); | |
127 | ||
128 | // Validate that each checkpoint points to the right event | |
129 | for (int i = 0; i < checkpoints.size(); i++) { | |
3427112b | 130 | ITmfCheckpoint checkpoint = checkpoints.get(i); |
07671572 | 131 | TmfExperimentLocation expLocation = (TmfExperimentLocation) checkpoint.getLocation(); |
5976d44a | 132 | TmfLocationArray locations = expLocation.getLocationInfo(); |
07671572 | 133 | ITmfContext[] trcContexts = new ITmfContext[2]; |
d62bb185 FC |
134 | trcContexts[0] = new TmfContext(locations.getLocation(0), (i * pageSize) / 2); |
135 | trcContexts[1] = new TmfContext(locations.getLocation(1), (i * pageSize) / 2); | |
07671572 | 136 | TmfExperimentContext expContext = new TmfExperimentContext(trcContexts); |
c7e1020d FC |
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)); | |
07671572 FC |
139 | ITmfEvent event = fExperiment.parseEvent(expContext); |
140 | assertTrue(expContext.getRank() == i * pageSize); | |
141 | assertTrue((checkpoint.getTimestamp().compareTo(event.getTimestamp(), false) == 0)); | |
142 | } | |
143 | } | |
144 | ||
3427112b FC |
145 | // ------------------------------------------------------------------------ |
146 | // Streaming | |
147 | // ------------------------------------------------------------------------ | |
148 | ||
6e1886bc | 149 | @Test |
54a7a54c FC |
150 | @SuppressWarnings("null") |
151 | public void testGrowingIndex() { | |
6256d8ad | 152 | ITmfTrace[] testTraces = new TmfTraceStub[2]; |
3427112b FC |
153 | try { |
154 | URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(DIRECTORY + File.separator + TEST_STREAM1), null); | |
155 | File test = new File(FileLocator.toFileURL(location).toURI()); | |
156 | final TmfTraceStub trace1 = new TmfTraceStub(test.getPath(), 0, false); | |
157 | testTraces[0] = trace1; | |
158 | location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(DIRECTORY + File.separator + TEST_STREAM2), null); | |
159 | test = new File(FileLocator.toFileURL(location).toURI()); | |
160 | final TmfTraceStub trace2 = new TmfTraceStub(test.getPath(), 0, false); | |
161 | testTraces[1] = trace2; | |
162 | } catch (final TmfTraceException e) { | |
163 | e.printStackTrace(); | |
164 | } catch (final URISyntaxException e) { | |
165 | e.printStackTrace(); | |
166 | } catch (final IOException e) { | |
167 | e.printStackTrace(); | |
168 | } | |
169 | ||
6256d8ad | 170 | TmfExperimentStub experiment = new TmfExperimentStub(EXPERIMENT, testTraces, BLOCK_SIZE); |
3427112b FC |
171 | int pageSize = experiment.getCacheSize(); |
172 | ||
173 | // Build the first half of the index | |
174 | TmfTimeRange range = new TmfTimeRange(new TmfTimestamp(1, -3), new TmfTimestamp(NB_EVENTS / 2 - 1, -3)); | |
175 | experiment.getIndexer().buildIndex(0, range, true); | |
176 | ||
177 | // Validate that each checkpoint points to the right event | |
178 | List<ITmfCheckpoint> checkpoints = experiment.getIndexer().getCheckpoints(); | |
179 | assertTrue("Checkpoints exist", checkpoints != null); | |
180 | assertEquals("Checkpoints size", NB_EVENTS / BLOCK_SIZE / 2, checkpoints.size()); | |
181 | ||
182 | // Build the second half of the index | |
183 | experiment.getIndexer().buildIndex(NB_EVENTS / 2, TmfTimeRange.ETERNITY, true); | |
184 | ||
185 | // Validate that each checkpoint points to the right event | |
186 | assertEquals("Checkpoints size", NB_EVENTS / BLOCK_SIZE, checkpoints.size()); | |
187 | for (int i = 0; i < checkpoints.size(); i++) { | |
188 | ITmfCheckpoint checkpoint = checkpoints.get(i); | |
189 | TmfExperimentLocation expLocation = (TmfExperimentLocation) checkpoint.getLocation(); | |
5976d44a | 190 | TmfLocationArray locations = expLocation.getLocationInfo(); |
3427112b | 191 | ITmfContext[] trcContexts = new ITmfContext[2]; |
d62bb185 FC |
192 | trcContexts[0] = new TmfContext(locations.getLocation(0), (i * pageSize) / 2); |
193 | trcContexts[1] = new TmfContext(locations.getLocation(1), (i * pageSize) / 2); | |
3427112b FC |
194 | TmfExperimentContext expContext = new TmfExperimentContext(trcContexts); |
195 | expContext.getEvents()[0] = testTraces[0].getNext(testTraces[0].seekEvent((i * pageSize) / 2)); | |
196 | expContext.getEvents()[1] = testTraces[1].getNext(testTraces[1].seekEvent((i * pageSize) / 2)); | |
197 | ITmfEvent event = experiment.parseEvent(expContext); | |
198 | assertTrue(expContext.getRank() == i * pageSize); | |
199 | assertTrue((checkpoint.getTimestamp().compareTo(event.getTimestamp(), false) == 0)); | |
200 | assertEquals("Checkpoint value", i * pageSize + 1, checkpoint.getTimestamp().getValue()); | |
201 | } | |
6e1886bc AM |
202 | |
203 | /* Clean up (since we didn't use the class-specific fixtures) */ | |
204 | experiment.dispose(); | |
205 | for (ITmfTrace trace : testTraces) { | |
206 | trace.dispose(); | |
207 | } | |
3427112b FC |
208 | } |
209 | ||
07671572 | 210 | } |