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