Commit | Line | Data |
---|---|---|
07671572 FC |
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 | ||
9e0640dc | 13 | package org.eclipse.linuxtools.tmf.core.tests.trace; |
07671572 FC |
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; | |
9e0640dc FC |
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; | |
07671572 | 28 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
9e0640dc | 29 | import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange; |
3427112b | 30 | import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp; |
07671572 | 31 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; |
07671572 | 32 | import org.eclipse.linuxtools.tmf.core.tests.TmfCoreTestPlugin; |
3427112b | 33 | import org.eclipse.linuxtools.tmf.core.trace.ITmfCheckpoint; |
07671572 FC |
34 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; |
35 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; | |
07671572 | 36 | import org.eclipse.linuxtools.tmf.core.trace.TmfContext; |
9e0640dc | 37 | import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfExperimentStub; |
07671572 FC |
38 | import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub; |
39 | ||
40 | /** | |
41 | * Test suite for the TmfCheckpointIndexTest class. | |
42 | */ | |
54a7a54c | 43 | @SuppressWarnings({"nls","javadoc"}) |
07671572 FC |
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 | ||
6256d8ad AM |
57 | private static ITmfTrace[] fTestTraces; |
58 | private static TmfExperimentStub fExperiment; | |
07671572 FC |
59 | |
60 | // ------------------------------------------------------------------------ | |
61 | // Housekeeping | |
62 | // ------------------------------------------------------------------------ | |
63 | ||
54a7a54c FC |
64 | /** |
65 | * @param name the test name | |
66 | */ | |
67 | public TmfExperimentCheckpointIndexTest(final String name) { | |
07671572 FC |
68 | super(name); |
69 | } | |
70 | ||
71 | @Override | |
0879b6b9 | 72 | protected synchronized void setUp() throws Exception { |
07671572 | 73 | super.setUp(); |
07671572 | 74 | if (fExperiment == null) { |
3427112b | 75 | setupTrace(DIRECTORY + File.separator + TEST_STREAM1, DIRECTORY + File.separator + TEST_STREAM2); |
6256d8ad | 76 | fExperiment = new TmfExperimentStub(EXPERIMENT, fTestTraces, BLOCK_SIZE); |
9e0640dc | 77 | fExperiment.getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, true); |
07671572 FC |
78 | } |
79 | } | |
80 | ||
81 | @Override | |
82 | protected void tearDown() throws Exception { | |
83 | super.tearDown(); | |
84 | fExperiment.dispose(); | |
85 | fExperiment = null; | |
3427112b | 86 | fTestTraces = null; |
07671572 FC |
87 | } |
88 | ||
6256d8ad | 89 | private synchronized static ITmfTrace[] setupTrace(final String path1, final String path2) { |
c7e1020d FC |
90 | if (fTestTraces == null) { |
91 | fTestTraces = new ITmfTrace[2]; | |
07671572 FC |
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); | |
c7e1020d | 96 | fTestTraces[0] = trace1; |
07671572 FC |
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); | |
c7e1020d | 100 | fTestTraces[1] = trace2; |
07671572 FC |
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 | } | |
c7e1020d | 109 | return fTestTraces; |
07671572 FC |
110 | } |
111 | ||
112 | // ------------------------------------------------------------------------ | |
113 | // Verify checkpoints | |
114 | // ------------------------------------------------------------------------ | |
115 | ||
54a7a54c FC |
116 | @SuppressWarnings("null") |
117 | public void testTmfTraceIndexing() { | |
07671572 FC |
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 | ||
3427112b | 125 | List<ITmfCheckpoint> checkpoints = fExperiment.getIndexer().getCheckpoints(); |
07671572 FC |
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++) { | |
3427112b | 132 | ITmfCheckpoint checkpoint = checkpoints.get(i); |
07671572 | 133 | TmfExperimentLocation expLocation = (TmfExperimentLocation) checkpoint.getLocation(); |
5976d44a | 134 | TmfLocationArray locations = expLocation.getLocationInfo(); |
07671572 | 135 | ITmfContext[] trcContexts = new ITmfContext[2]; |
5cc97265 FC |
136 | trcContexts[0] = new TmfContext(locations.getLocations()[0], (i * pageSize) / 2); |
137 | trcContexts[1] = new TmfContext(locations.getLocations()[1], (i * pageSize) / 2); | |
07671572 | 138 | TmfExperimentContext expContext = new TmfExperimentContext(trcContexts); |
c7e1020d FC |
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)); | |
07671572 FC |
141 | ITmfEvent event = fExperiment.parseEvent(expContext); |
142 | assertTrue(expContext.getRank() == i * pageSize); | |
143 | assertTrue((checkpoint.getTimestamp().compareTo(event.getTimestamp(), false) == 0)); | |
144 | } | |
145 | } | |
146 | ||
3427112b FC |
147 | // ------------------------------------------------------------------------ |
148 | // Streaming | |
149 | // ------------------------------------------------------------------------ | |
150 | ||
54a7a54c FC |
151 | @SuppressWarnings("null") |
152 | public void testGrowingIndex() { | |
3427112b | 153 | |
6256d8ad | 154 | ITmfTrace[] testTraces = new TmfTraceStub[2]; |
3427112b FC |
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 | ||
6256d8ad | 172 | TmfExperimentStub experiment = new TmfExperimentStub(EXPERIMENT, testTraces, BLOCK_SIZE); |
3427112b FC |
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(); | |
5976d44a | 192 | TmfLocationArray locations = expLocation.getLocationInfo(); |
3427112b | 193 | ITmfContext[] trcContexts = new ITmfContext[2]; |
5cc97265 FC |
194 | trcContexts[0] = new TmfContext(locations.getLocations()[0], (i * pageSize) / 2); |
195 | trcContexts[1] = new TmfContext(locations.getLocations()[1], (i * pageSize) / 2); | |
3427112b FC |
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 | ||
07671572 | 206 | } |