Commit | Line | Data |
---|---|---|
d18dd09b | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2009, 2014 Ericsson |
9b749023 | 3 | * |
d18dd09b ASL |
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 | |
9b749023 | 8 | * |
d18dd09b ASL |
9 | * Contributors: |
10 | * Francois Chouinard - Initial API and implementation | |
0316808c | 11 | * Francois Chouinard - Adjusted for new Trace Model |
6e1886bc | 12 | * Alexandre Montplaisir - Port to JUnit4 |
ea271da6 | 13 | * Patrick Tasse - Updated for rank in experiment location |
d18dd09b ASL |
14 | *******************************************************************************/ |
15 | ||
2bdf0193 | 16 | package org.eclipse.tracecompass.tmf.core.tests.trace; |
d18dd09b | 17 | |
6e1886bc AM |
18 | import static org.junit.Assert.assertEquals; |
19 | import static org.junit.Assert.assertFalse; | |
07ef7847 | 20 | import static org.junit.Assert.assertNotNull; |
6e1886bc AM |
21 | import static org.junit.Assert.assertNull; |
22 | import static org.junit.Assert.assertTrue; | |
23 | ||
d18dd09b ASL |
24 | import java.io.File; |
25 | import java.io.IOException; | |
26 | import java.net.URISyntaxException; | |
27 | import java.net.URL; | |
472ea248 | 28 | import java.util.Map; |
d18dd09b ASL |
29 | import java.util.Vector; |
30 | ||
d18dd09b ASL |
31 | import org.eclipse.core.runtime.FileLocator; |
32 | import org.eclipse.core.runtime.Path; | |
472ea248 | 33 | import org.eclipse.jdt.annotation.NonNull; |
b6f14576 | 34 | import org.eclipse.tracecompass.internal.tmf.core.component.TmfProviderManager; |
472ea248 | 35 | import org.eclipse.tracecompass.internal.tmf.core.synchronization.SyncAlgorithmFullyIncremental; |
5c5fa260 AM |
36 | import org.eclipse.tracecompass.internal.tmf.core.trace.experiment.TmfExperimentContext; |
37 | import org.eclipse.tracecompass.internal.tmf.core.trace.experiment.TmfExperimentLocation; | |
2bdf0193 AM |
38 | import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule; |
39 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; | |
40 | import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException; | |
472ea248 | 41 | import org.eclipse.tracecompass.tmf.core.project.model.ITmfPropertiesProvider; |
2bdf0193 | 42 | import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest; |
2bdf0193 | 43 | import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest.ExecutionType; |
5c5fa260 | 44 | import org.eclipse.tracecompass.tmf.core.request.TmfEventRequest; |
2bdf0193 | 45 | import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal; |
472ea248 AM |
46 | import org.eclipse.tracecompass.tmf.core.synchronization.ITmfTimestampTransform; |
47 | import org.eclipse.tracecompass.tmf.core.synchronization.SynchronizationAlgorithm; | |
48 | import org.eclipse.tracecompass.tmf.core.synchronization.TimestampTransformFactory; | |
2bdf0193 | 49 | import org.eclipse.tracecompass.tmf.core.tests.TmfCoreTestPlugin; |
6fbe12fd | 50 | import org.eclipse.tracecompass.tmf.core.tests.analysis.AnalysisManagerTest; |
2bdf0193 AM |
51 | import org.eclipse.tracecompass.tmf.core.tests.shared.TmfTestTrace; |
52 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange; | |
53 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp; | |
54 | import org.eclipse.tracecompass.tmf.core.trace.ITmfContext; | |
55 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; | |
b8585c7c | 56 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils; |
5c5fa260 | 57 | import org.eclipse.tracecompass.tmf.core.trace.experiment.TmfExperiment; |
2bdf0193 AM |
58 | import org.eclipse.tracecompass.tmf.core.trace.location.ITmfLocation; |
59 | import org.eclipse.tracecompass.tmf.core.trace.location.TmfLongLocation; | |
60 | import org.eclipse.tracecompass.tmf.tests.stubs.analysis.TestExperimentAnalysis; | |
61 | import org.eclipse.tracecompass.tmf.tests.stubs.trace.TmfExperimentStub; | |
62 | import org.eclipse.tracecompass.tmf.tests.stubs.trace.TmfTraceStub; | |
472ea248 | 63 | import org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.TmfXmlTraceStub; |
b6f14576 | 64 | import org.junit.After; |
6e1886bc AM |
65 | import org.junit.Before; |
66 | import org.junit.Test; | |
d18dd09b | 67 | |
472ea248 AM |
68 | import com.google.common.collect.ImmutableMap; |
69 | ||
d18dd09b | 70 | /** |
0316808c | 71 | * Test suite for the TmfExperiment class (single trace). |
d18dd09b | 72 | */ |
cad06250 | 73 | @SuppressWarnings("javadoc") |
6e1886bc | 74 | public class TmfExperimentTest { |
d18dd09b | 75 | |
0316808c FC |
76 | // ------------------------------------------------------------------------ |
77 | // Attributes | |
78 | // ------------------------------------------------------------------------ | |
79 | ||
d18dd09b ASL |
80 | private static final String EXPERIMENT = "MyExperiment"; |
81 | private static int NB_EVENTS = 10000; | |
0316808c | 82 | private static int BLOCK_SIZE = 1000; |
d18dd09b | 83 | |
6e1886bc AM |
84 | private static final double DELTA = 1e-15; |
85 | ||
6256d8ad AM |
86 | private ITmfTrace[] fTestTraces; |
87 | private TmfExperimentStub fExperiment; | |
d18dd09b ASL |
88 | |
89 | private static byte SCALE = (byte) -3; | |
90 | ||
91 | // ------------------------------------------------------------------------ | |
92 | // Housekeeping | |
93 | // ------------------------------------------------------------------------ | |
94 | ||
6256d8ad | 95 | private synchronized ITmfTrace[] setupTrace(final String path) { |
9e0640dc FC |
96 | if (fTestTraces == null) { |
97 | fTestTraces = new ITmfTrace[1]; | |
25e48683 FC |
98 | try { |
99 | final URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(path), null); | |
100 | final File test = new File(FileLocator.toFileURL(location).toURI()); | |
ab186fbb | 101 | final TmfTraceStub trace = new TmfTraceStub(test.getPath(), 0, true, null); |
9e0640dc | 102 | fTestTraces[0] = trace; |
b4f71e4a FC |
103 | } catch (final TmfTraceException e) { |
104 | e.printStackTrace(); | |
25e48683 FC |
105 | } catch (final URISyntaxException e) { |
106 | e.printStackTrace(); | |
107 | } catch (final IOException e) { | |
108 | e.printStackTrace(); | |
109 | } | |
110 | } | |
9e0640dc | 111 | return fTestTraces; |
d18dd09b ASL |
112 | } |
113 | ||
9e0640dc | 114 | private synchronized void setupExperiment() { |
20658947 | 115 | if (fExperiment == null) { |
6256d8ad | 116 | fExperiment = new TmfExperimentStub(EXPERIMENT, fTestTraces, BLOCK_SIZE); |
9e0640dc | 117 | fExperiment.getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, true); |
20658947 | 118 | } |
d18dd09b ASL |
119 | } |
120 | ||
6e1886bc AM |
121 | @Before |
122 | public void setUp() { | |
9c4d52ee | 123 | setupTrace(TmfTestTrace.A_TEST_10K.getFullPath()); |
25e48683 FC |
124 | setupExperiment(); |
125 | } | |
d18dd09b | 126 | |
b6f14576 PT |
127 | @After |
128 | public void tearDown() { | |
129 | if (fExperiment != null) { | |
130 | fExperiment.dispose(); | |
131 | } | |
132 | assertEquals(0, TmfProviderManager.getProviders(ITmfEvent.class).length); | |
133 | } | |
134 | ||
d18dd09b ASL |
135 | // ------------------------------------------------------------------------ |
136 | // Constructor | |
137 | // ------------------------------------------------------------------------ | |
138 | ||
6e1886bc | 139 | @Test |
9e0640dc | 140 | public void testSimpleTmfExperimentConstructor() { |
4178260e AM |
141 | TmfExperiment experiment = new TmfExperiment(ITmfEvent.class, EXPERIMENT, |
142 | fTestTraces, TmfExperiment.DEFAULT_INDEX_PAGE_SIZE, null); | |
9e0640dc FC |
143 | assertEquals("GetId", EXPERIMENT, experiment.getName()); |
144 | assertEquals("GetCacheSize", TmfExperiment.DEFAULT_INDEX_PAGE_SIZE, experiment.getCacheSize()); | |
145 | experiment.dispose(); | |
146 | ||
4178260e AM |
147 | experiment = new TmfExperiment(ITmfEvent.class, EXPERIMENT, null, |
148 | TmfExperiment.DEFAULT_INDEX_PAGE_SIZE, null); | |
9e0640dc FC |
149 | experiment.dispose(); |
150 | } | |
151 | ||
6e1886bc | 152 | @Test |
9e0640dc | 153 | public void testNormalTmfExperimentConstructor() { |
25e48683 | 154 | assertEquals("GetId", EXPERIMENT, fExperiment.getName()); |
d18dd09b ASL |
155 | assertEquals("GetNbEvents", NB_EVENTS, fExperiment.getNbEvents()); |
156 | ||
07671572 FC |
157 | final long nbExperimentEvents = fExperiment.getNbEvents(); |
158 | assertEquals("GetNbEvents", NB_EVENTS, nbExperimentEvents); | |
159 | ||
fa62dc1d | 160 | final long nbTraceEvents = fExperiment.getTraces().get(0).getNbEvents(); |
82e04272 FC |
161 | assertEquals("GetNbEvents", NB_EVENTS, nbTraceEvents); |
162 | ||
25e48683 | 163 | final TmfTimeRange timeRange = fExperiment.getTimeRange(); |
d18dd09b ASL |
164 | assertEquals("getStartTime", 1, timeRange.getStartTime().getValue()); |
165 | assertEquals("getEndTime", NB_EVENTS, timeRange.getEndTime().getValue()); | |
25e48683 | 166 | } |
d18dd09b | 167 | |
b3dd2736 GB |
168 | // ------------------------------------------------------------------------ |
169 | // Experiment setup | |
170 | // ------------------------------------------------------------------------ | |
171 | ||
172 | @Test | |
173 | public void testExperimentInitialization() { | |
174 | /* | |
175 | * Calling default constructor, then init should be equivalent to | |
176 | * calling the full constructor | |
177 | */ | |
178 | ||
4178260e | 179 | TmfExperimentStub experiment = new TmfExperimentStub(EXPERIMENT, fTestTraces, 5000); |
b3dd2736 GB |
180 | experiment.getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, true); |
181 | ||
182 | assertEquals("GetId", EXPERIMENT, fExperiment.getName()); | |
183 | assertEquals("GetNbEvents", NB_EVENTS, fExperiment.getNbEvents()); | |
184 | ||
185 | final long nbExperimentEvents = fExperiment.getNbEvents(); | |
186 | assertEquals("GetNbEvents", NB_EVENTS, nbExperimentEvents); | |
187 | ||
fa62dc1d | 188 | final long nbTraceEvents = fExperiment.getTraces().get(0).getNbEvents(); |
b3dd2736 GB |
189 | assertEquals("GetNbEvents", NB_EVENTS, nbTraceEvents); |
190 | ||
191 | final TmfTimeRange timeRange = fExperiment.getTimeRange(); | |
192 | assertEquals("getStartTime", 1, timeRange.getStartTime().getValue()); | |
193 | assertEquals("getEndTime", NB_EVENTS, timeRange.getEndTime().getValue()); | |
b6f14576 PT |
194 | |
195 | experiment.dispose(); | |
b3dd2736 GB |
196 | } |
197 | ||
9e0640dc FC |
198 | // ------------------------------------------------------------------------ |
199 | // getTimestamp | |
200 | // ------------------------------------------------------------------------ | |
201 | ||
6e1886bc | 202 | @Test |
54a7a54c | 203 | public void testGetTimestamp() { |
b2c971ec MK |
204 | assertEquals("getTimestamp", TmfTimestamp.create( 1, (byte) -3), fExperiment.getTimestamp( 0)); |
205 | assertEquals("getTimestamp", TmfTimestamp.create( 2, (byte) -3), fExperiment.getTimestamp( 1)); | |
206 | assertEquals("getTimestamp", TmfTimestamp.create( 11, (byte) -3), fExperiment.getTimestamp( 10)); | |
207 | assertEquals("getTimestamp", TmfTimestamp.create( 101, (byte) -3), fExperiment.getTimestamp( 100)); | |
208 | assertEquals("getTimestamp", TmfTimestamp.create( 1001, (byte) -3), fExperiment.getTimestamp(1000)); | |
209 | assertEquals("getTimestamp", TmfTimestamp.create( 2001, (byte) -3), fExperiment.getTimestamp(2000)); | |
210 | assertEquals("getTimestamp", TmfTimestamp.create( 2501, (byte) -3), fExperiment.getTimestamp(2500)); | |
211 | assertEquals("getTimestamp", TmfTimestamp.create(10000, (byte) -3), fExperiment.getTimestamp(9999)); | |
9e0640dc FC |
212 | assertNull("getTimestamp", fExperiment.getTimestamp(10000)); |
213 | } | |
214 | ||
a51b2b9f | 215 | // ------------------------------------------------------------------------ |
8a6ff07f | 216 | // State system, statistics and modules methods |
a51b2b9f AM |
217 | // ------------------------------------------------------------------------ |
218 | ||
6e1886bc | 219 | @Test |
8a6ff07f | 220 | public void testGetAnalysisModules() { |
1d83ed07 AM |
221 | TmfExperiment experiment = fExperiment; |
222 | assertNotNull(experiment); | |
223 | ||
8a6ff07f | 224 | /* There should not be any modules at this point */ |
1d83ed07 | 225 | Iterable<IAnalysisModule> modules = experiment.getAnalysisModules(); |
ff3f02c8 | 226 | assertFalse(modules.iterator().hasNext()); |
6a6adab9 GB |
227 | |
228 | /* Open the experiment, the modules should be populated */ | |
1d83ed07 AM |
229 | experiment.traceOpened(new TmfTraceOpenedSignal(this, experiment, null)); |
230 | modules = experiment.getAnalysisModules(); | |
231 | Iterable<TestExperimentAnalysis> testModules = TmfTraceUtils.getAnalysisModulesOfClass(experiment, TestExperimentAnalysis.class); | |
6a6adab9 GB |
232 | assertTrue(modules.iterator().hasNext()); |
233 | assertTrue(testModules.iterator().hasNext()); | |
6fbe12fd GB |
234 | |
235 | /* | |
236 | * Test that a module that applies to one of its trace is present in an | |
237 | * experiment | |
238 | */ | |
239 | ITmfTrace trace1 = TmfTestTrace.A_TEST_10K.getTrace(); | |
240 | ITmfTrace trace2 = TmfTestTrace.A_TEST_10K2.getTrace(); | |
241 | ITmfTrace trace3 = TmfTestTrace.A_TEST_10K2.getTraceAsStub2(); | |
242 | ||
243 | /* | |
244 | * Create an experiment with TmfTraceStub, the module other should not | |
245 | * be there | |
246 | */ | |
247 | ITmfTrace[] tracesExp1 = { trace1, trace2 }; | |
248 | TmfExperiment exp1 = new TmfExperiment(tracesExp1[0].getEventType(), "Experiment 1", tracesExp1, TmfExperiment.DEFAULT_INDEX_PAGE_SIZE, null); | |
249 | ||
250 | /* | |
251 | * Create an experiment containing some TmfTraceStub2, the module other | |
252 | * should be present | |
253 | */ | |
254 | ITmfTrace[] tracesExp2 = { trace1, trace3 }; | |
255 | TmfExperiment exp2 = new TmfExperiment(tracesExp2[0].getEventType(), "Experiment 1", tracesExp2, TmfExperiment.DEFAULT_INDEX_PAGE_SIZE, null); | |
256 | ||
257 | try { | |
258 | /* Open the experiment, the modules should be populated */ | |
259 | exp1.traceOpened(new TmfTraceOpenedSignal(this, exp1, null)); | |
260 | assertNull(exp1.getAnalysisModule(AnalysisManagerTest.MODULE_SECOND)); | |
261 | ||
262 | /* Open the experiment, the modules should be populated */ | |
263 | exp2.traceOpened(new TmfTraceOpenedSignal(this, exp2, null)); | |
264 | assertNotNull(exp2.getAnalysisModule(AnalysisManagerTest.MODULE_SECOND)); | |
265 | } finally { | |
266 | trace1.dispose(); | |
267 | trace2.dispose(); | |
268 | trace3.dispose(); | |
269 | exp1.dispose(); | |
270 | exp2.dispose(); | |
271 | } | |
272 | ||
a51b2b9f AM |
273 | } |
274 | ||
9e0640dc FC |
275 | // ------------------------------------------------------------------------ |
276 | // seekEvent by location | |
277 | // ------------------------------------------------------------------------ | |
278 | ||
6e1886bc | 279 | @Test |
54a7a54c | 280 | public void testSeekBadLocation() { |
cb8c854e | 281 | ITmfContext context = fExperiment.seekEvent(new TmfLongLocation(0L)); |
9e0640dc FC |
282 | assertNull("seekEvent", context); |
283 | } | |
284 | ||
6e1886bc | 285 | @Test |
54a7a54c | 286 | public void testSeekNoTrace() { |
4178260e AM |
287 | TmfExperiment experiment = new TmfExperiment(ITmfEvent.class, EXPERIMENT, |
288 | null, TmfExperiment.DEFAULT_INDEX_PAGE_SIZE, null); | |
9e0640dc | 289 | ITmfContext context = experiment.seekEvent((TmfExperimentLocation) null); |
fa62dc1d | 290 | validateContextRanks(context); |
9e0640dc FC |
291 | experiment.dispose(); |
292 | } | |
293 | ||
294 | // ------------------------------------------------------------------------ | |
295 | // seekEvent on ratio | |
296 | // ------------------------------------------------------------------------ | |
297 | ||
6e1886bc | 298 | @Test |
54a7a54c | 299 | public void testSeekEventOnRatio() { |
9e0640dc FC |
300 | // First event |
301 | ITmfContext context = fExperiment.seekEvent(0.0); | |
302 | assertEquals("Context rank", 0, context.getRank()); | |
303 | ITmfEvent event = fExperiment.parseEvent(context); | |
304 | assertEquals("Event timestamp", 1, event.getTimestamp().getValue()); | |
305 | assertEquals("Context rank", 0, context.getRank()); | |
306 | ||
307 | // Middle event | |
308 | int midTrace = NB_EVENTS / 2; | |
309 | context = fExperiment.seekEvent(0.5); | |
310 | assertEquals("Context rank", midTrace, context.getRank()); | |
311 | event = fExperiment.parseEvent(context); | |
312 | assertEquals("Event timestamp", midTrace + 1, event.getTimestamp().getValue()); | |
313 | assertEquals("Context rank", midTrace, context.getRank()); | |
9b749023 | 314 | |
9e0640dc FC |
315 | // Last event |
316 | context = fExperiment.seekEvent(1.0); | |
317 | assertEquals("Context rank", NB_EVENTS, context.getRank()); | |
318 | event = fExperiment.parseEvent(context); | |
319 | assertNull("Event timestamp", event); | |
320 | assertEquals("Context rank", NB_EVENTS, context.getRank()); | |
9b749023 | 321 | |
9e0640dc FC |
322 | // Beyond last event |
323 | context = fExperiment.seekEvent(1.1); | |
324 | assertEquals("Context rank", NB_EVENTS, context.getRank()); | |
325 | event = fExperiment.parseEvent(context); | |
326 | assertNull("Event timestamp", event); | |
327 | assertEquals("Context rank", NB_EVENTS, context.getRank()); | |
9b749023 | 328 | |
9e0640dc FC |
329 | // Negative ratio |
330 | context = fExperiment.seekEvent(-0.5); | |
331 | assertEquals("Context rank", 0, context.getRank()); | |
332 | event = fExperiment.parseEvent(context); | |
333 | assertEquals("Event timestamp", 1, event.getTimestamp().getValue()); | |
334 | assertEquals("Context rank", 0, context.getRank()); | |
335 | } | |
336 | ||
6e1886bc | 337 | @Test |
54a7a54c | 338 | public void testGetLocationRatio() { |
9e0640dc FC |
339 | // First event |
340 | ITmfContext context = fExperiment.seekEvent((ITmfLocation) null); | |
341 | double ratio = fExperiment.getLocationRatio(context.getLocation()); | |
ea271da6 | 342 | assertEquals("getLocationRatio", 0.0, ratio, DELTA); |
9e0640dc FC |
343 | |
344 | // Middle event | |
345 | context = fExperiment.seekEvent(NB_EVENTS / 2); | |
346 | ratio = fExperiment.getLocationRatio(context.getLocation()); | |
ea271da6 | 347 | assertEquals("getLocationRatio", (double) (NB_EVENTS / 2) / NB_EVENTS, ratio, DELTA); |
9e0640dc FC |
348 | |
349 | // Last event | |
350 | context = fExperiment.seekEvent(NB_EVENTS - 1); | |
351 | ratio = fExperiment.getLocationRatio(context.getLocation()); | |
ea271da6 | 352 | assertEquals("getLocationRatio", (double) (NB_EVENTS - 1) / NB_EVENTS, ratio, DELTA); |
9e0640dc FC |
353 | } |
354 | ||
17324c9a | 355 | // @SuppressWarnings("rawtypes") |
54a7a54c | 356 | // public void testGetCurrentLocation() { |
9e0640dc FC |
357 | // ITmfContext context = fExperiment.seekEvent((ITmfLocation) null); |
358 | // ITmfLocation location = fExperiment.getCurrentLocation(); | |
359 | // assertEquals("getCurrentLocation", location, context.getLocation()); | |
360 | // } | |
361 | ||
d18dd09b | 362 | // ------------------------------------------------------------------------ |
0316808c | 363 | // seekEvent on rank |
d18dd09b ASL |
364 | // ------------------------------------------------------------------------ |
365 | ||
6e1886bc | 366 | @Test |
54a7a54c | 367 | public void testSeekRankOnCacheBoundary() { |
0316808c | 368 | long cacheSize = fExperiment.getCacheSize(); |
9b635e61 | 369 | |
0316808c FC |
370 | // On lower bound, returns the first event (TS = 1) |
371 | ITmfContext context = fExperiment.seekEvent(0); | |
372 | assertEquals("Context rank", 0, context.getRank()); | |
25e48683 | 373 | |
c32744d6 | 374 | ITmfEvent event = fExperiment.getNext(context); |
d18dd09b | 375 | assertEquals("Event timestamp", 1, event.getTimestamp().getValue()); |
0316808c | 376 | assertEquals("Context rank", 1, context.getRank()); |
25e48683 | 377 | |
0316808c FC |
378 | // Position trace at event rank [cacheSize] |
379 | context = fExperiment.seekEvent(cacheSize); | |
380 | assertEquals("Context rank", cacheSize, context.getRank()); | |
25e48683 | 381 | |
c32744d6 | 382 | event = fExperiment.getNext(context); |
0316808c FC |
383 | assertEquals("Event timestamp", cacheSize + 1, event.getTimestamp().getValue()); |
384 | assertEquals("Context rank", cacheSize + 1, context.getRank()); | |
d18dd09b | 385 | |
0316808c FC |
386 | // Position trace at event rank [4 * cacheSize] |
387 | context = fExperiment.seekEvent(4 * cacheSize); | |
388 | assertEquals("Context rank", 4 * cacheSize, context.getRank()); | |
25e48683 | 389 | |
c32744d6 | 390 | event = fExperiment.getNext(context); |
0316808c FC |
391 | assertEquals("Event timestamp", 4 * cacheSize + 1, event.getTimestamp().getValue()); |
392 | assertEquals("Context rank", 4 * cacheSize + 1, context.getRank()); | |
d18dd09b ASL |
393 | } |
394 | ||
6e1886bc | 395 | @Test |
54a7a54c | 396 | public void testSeekRankNotOnCacheBoundary() { |
0316808c | 397 | long cacheSize = fExperiment.getCacheSize(); |
25e48683 | 398 | |
0316808c FC |
399 | // Position trace at event rank 9 |
400 | ITmfContext context = fExperiment.seekEvent(9); | |
401 | assertEquals("Context rank", 9, context.getRank()); | |
25e48683 | 402 | |
c32744d6 | 403 | ITmfEvent event = fExperiment.getNext(context); |
d18dd09b | 404 | assertEquals("Event timestamp", 10, event.getTimestamp().getValue()); |
0316808c | 405 | assertEquals("Context rank", 10, context.getRank()); |
d18dd09b | 406 | |
0316808c FC |
407 | // Position trace at event rank [cacheSize - 1] |
408 | context = fExperiment.seekEvent(cacheSize - 1); | |
409 | assertEquals("Context rank", cacheSize - 1, context.getRank()); | |
25e48683 | 410 | |
c32744d6 | 411 | event = fExperiment.getNext(context); |
0316808c FC |
412 | assertEquals("Event timestamp", cacheSize, event.getTimestamp().getValue()); |
413 | assertEquals("Context rank", cacheSize, context.getRank()); | |
d18dd09b | 414 | |
0316808c FC |
415 | // Position trace at event rank [cacheSize + 1] |
416 | context = fExperiment.seekEvent(cacheSize + 1); | |
417 | assertEquals("Context rank", cacheSize + 1, context.getRank()); | |
25e48683 | 418 | |
c32744d6 | 419 | event = fExperiment.getNext(context); |
0316808c FC |
420 | assertEquals("Event timestamp", cacheSize + 2, event.getTimestamp().getValue()); |
421 | assertEquals("Context rank", cacheSize + 2, context.getRank()); | |
d18dd09b | 422 | |
25e48683 | 423 | // Position trace at event rank 4500 |
0316808c FC |
424 | context = fExperiment.seekEvent(4500); |
425 | assertEquals("Context rank", 4500, context.getRank()); | |
25e48683 | 426 | |
c32744d6 | 427 | event = fExperiment.getNext(context); |
d18dd09b | 428 | assertEquals("Event timestamp", 4501, event.getTimestamp().getValue()); |
0316808c | 429 | assertEquals("Context rank", 4501, context.getRank()); |
d18dd09b ASL |
430 | } |
431 | ||
6e1886bc | 432 | @Test |
54a7a54c | 433 | public void testSeekRankOutOfScope() { |
25e48683 | 434 | // Position trace at beginning |
0316808c | 435 | ITmfContext context = fExperiment.seekEvent(-1); |
9b635e61 | 436 | assertEquals("Event rank", 0, context.getRank()); |
25e48683 | 437 | |
c32744d6 | 438 | ITmfEvent event = fExperiment.getNext(context); |
d18dd09b | 439 | assertEquals("Event timestamp", 1, event.getTimestamp().getValue()); |
0316808c | 440 | assertEquals("Context rank", 1, context.getRank()); |
d18dd09b | 441 | |
25e48683 | 442 | // Position trace at event passed the end |
0316808c FC |
443 | context = fExperiment.seekEvent(NB_EVENTS); |
444 | assertEquals("Context rank", NB_EVENTS, context.getRank()); | |
445 | ||
c32744d6 | 446 | event = fExperiment.getNext(context); |
0316808c FC |
447 | assertNull("Event", event); |
448 | assertEquals("Context rank", NB_EVENTS, context.getRank()); | |
d18dd09b ASL |
449 | } |
450 | ||
451 | // ------------------------------------------------------------------------ | |
452 | // seekEvent on timestamp | |
453 | // ------------------------------------------------------------------------ | |
454 | ||
6e1886bc | 455 | @Test |
54a7a54c | 456 | public void testSeekTimestampOnCacheBoundary() { |
0316808c | 457 | long cacheSize = fExperiment.getCacheSize(); |
25e48683 | 458 | |
0316808c | 459 | // Position trace at event rank 0 |
b2c971ec | 460 | ITmfContext context = fExperiment.seekEvent(TmfTimestamp.create(1, SCALE)); |
0316808c | 461 | assertEquals("Context rank", 0, context.getRank()); |
25e48683 | 462 | |
c32744d6 | 463 | ITmfEvent event = fExperiment.getNext(context); |
d18dd09b | 464 | assertEquals("Event timestamp", 1, event.getTimestamp().getValue()); |
0316808c | 465 | assertEquals("Context rank", 1, context.getRank()); |
25e48683 | 466 | |
0316808c | 467 | // Position trace at event rank [cacheSize] |
b2c971ec | 468 | context = fExperiment.seekEvent(TmfTimestamp.create(cacheSize + 1, SCALE)); |
0316808c | 469 | assertEquals("Event rank", cacheSize, context.getRank()); |
25e48683 | 470 | |
c32744d6 | 471 | event = fExperiment.getNext(context); |
0316808c FC |
472 | assertEquals("Event timestamp", cacheSize + 1, event.getTimestamp().getValue()); |
473 | assertEquals("Context rank", cacheSize + 1, context.getRank()); | |
d18dd09b | 474 | |
0316808c | 475 | // Position trace at event rank [4 * cacheSize] |
b2c971ec | 476 | context = fExperiment.seekEvent(TmfTimestamp.create(4 * cacheSize + 1, SCALE)); |
0316808c | 477 | assertEquals("Context rank", 4 * cacheSize, context.getRank()); |
25e48683 | 478 | |
c32744d6 | 479 | event = fExperiment.getNext(context); |
0316808c FC |
480 | assertEquals("Event timestamp", 4 * cacheSize + 1, event.getTimestamp().getValue()); |
481 | assertEquals("Context rank", 4 * cacheSize + 1, context.getRank()); | |
d18dd09b ASL |
482 | } |
483 | ||
6e1886bc | 484 | @Test |
54a7a54c | 485 | public void testSeekTimestampNotOnCacheBoundary() { |
0316808c | 486 | // Position trace at event rank 1 (TS = 2) |
b2c971ec | 487 | ITmfContext context = fExperiment.seekEvent(TmfTimestamp.create(2, SCALE)); |
0316808c | 488 | assertEquals("Context rank", 1, context.getRank()); |
25e48683 | 489 | |
c32744d6 | 490 | ITmfEvent event = fExperiment.getNext(context); |
d18dd09b | 491 | assertEquals("Event timestamp", 2, event.getTimestamp().getValue()); |
0316808c | 492 | assertEquals("Context rank", 2, context.getRank()); |
d18dd09b | 493 | |
0316808c | 494 | // Position trace at event rank 9 (TS = 10) |
b2c971ec | 495 | context = fExperiment.seekEvent(TmfTimestamp.create(10, SCALE)); |
0316808c | 496 | assertEquals("Context rank", 9, context.getRank()); |
25e48683 | 497 | |
c32744d6 | 498 | event = fExperiment.getNext(context); |
d18dd09b | 499 | assertEquals("Event timestamp", 10, event.getTimestamp().getValue()); |
0316808c | 500 | assertEquals("Context rank", 10, context.getRank()); |
d18dd09b | 501 | |
0316808c | 502 | // Position trace at event rank 999 (TS = 1000) |
b2c971ec | 503 | context = fExperiment.seekEvent(TmfTimestamp.create(1000, SCALE)); |
0316808c | 504 | assertEquals("Context rank", 999, context.getRank()); |
25e48683 | 505 | |
c32744d6 | 506 | event = fExperiment.getNext(context); |
d18dd09b | 507 | assertEquals("Event timestamp", 1000, event.getTimestamp().getValue()); |
0316808c | 508 | assertEquals("Context rank", 1000, context.getRank()); |
d18dd09b | 509 | |
0316808c | 510 | // Position trace at event rank 1001 (TS = 1002) |
b2c971ec | 511 | context = fExperiment.seekEvent(TmfTimestamp.create(1002, SCALE)); |
0316808c | 512 | assertEquals("Context rank", 1001, context.getRank()); |
25e48683 | 513 | |
c32744d6 | 514 | event = fExperiment.getNext(context); |
d18dd09b | 515 | assertEquals("Event timestamp", 1002, event.getTimestamp().getValue()); |
0316808c | 516 | assertEquals("Context rank", 1002, context.getRank()); |
d18dd09b | 517 | |
0316808c | 518 | // Position trace at event rank 4500 (TS = 4501) |
b2c971ec | 519 | context = fExperiment.seekEvent(TmfTimestamp.create(4501, SCALE)); |
0316808c | 520 | assertEquals("Context rank", 4500, context.getRank()); |
25e48683 | 521 | |
c32744d6 | 522 | event = fExperiment.getNext(context); |
d18dd09b | 523 | assertEquals("Event timestamp", 4501, event.getTimestamp().getValue()); |
0316808c | 524 | assertEquals("Context rank", 4501, context.getRank()); |
d18dd09b ASL |
525 | } |
526 | ||
6e1886bc | 527 | @Test |
54a7a54c | 528 | public void testSeekTimestampOutOfScope() { |
25e48683 | 529 | // Position trace at beginning |
b2c971ec | 530 | ITmfContext context = fExperiment.seekEvent(TmfTimestamp.create(-1, SCALE)); |
d18dd09b | 531 | assertEquals("Event rank", 0, context.getRank()); |
25e48683 | 532 | |
c32744d6 | 533 | ITmfEvent event = fExperiment.getNext(context); |
d18dd09b ASL |
534 | assertEquals("Event timestamp", 1, event.getTimestamp().getValue()); |
535 | assertEquals("Event rank", 1, context.getRank()); | |
536 | ||
25e48683 | 537 | // Position trace at event passed the end |
b2c971ec | 538 | context = fExperiment.seekEvent(TmfTimestamp.create(NB_EVENTS + 1, SCALE)); |
c32744d6 | 539 | event = fExperiment.getNext(context); |
0316808c | 540 | assertNull("Event location", event); |
9b635e61 | 541 | assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank()); |
d18dd09b ASL |
542 | } |
543 | ||
544 | // ------------------------------------------------------------------------ | |
0316808c | 545 | // seekEvent by location (context rank is undefined) |
d18dd09b ASL |
546 | // ------------------------------------------------------------------------ |
547 | ||
6e1886bc | 548 | @Test |
54a7a54c | 549 | public void testSeekLocationOnCacheBoundary() { |
0316808c | 550 | long cacheSize = fExperiment.getCacheSize(); |
d18dd09b | 551 | |
0316808c FC |
552 | // Position trace at event rank 0 |
553 | ITmfContext tmpContext = fExperiment.seekEvent(0); | |
554 | ITmfContext context = fExperiment.seekEvent(tmpContext.getLocation()); | |
25e48683 | 555 | |
c32744d6 | 556 | ITmfEvent event = fExperiment.getNext(context); |
d18dd09b | 557 | assertEquals("Event timestamp", 1, event.getTimestamp().getValue()); |
25e48683 | 558 | |
c32744d6 | 559 | event = fExperiment.getNext(context); |
0316808c | 560 | assertEquals("Event timestamp", 2, event.getTimestamp().getValue()); |
d18dd09b | 561 | |
0316808c FC |
562 | // Position trace at event rank 'cacheSize' |
563 | tmpContext = fExperiment.seekEvent(cacheSize); | |
564 | context = fExperiment.seekEvent(tmpContext.getLocation()); | |
25e48683 | 565 | |
c32744d6 | 566 | event = fExperiment.getNext(context); |
0316808c | 567 | assertEquals("Event timestamp", cacheSize + 1, event.getTimestamp().getValue()); |
25e48683 | 568 | |
c32744d6 | 569 | event = fExperiment.getNext(context); |
0316808c | 570 | assertEquals("Event timestamp", cacheSize + 2, event.getTimestamp().getValue()); |
d18dd09b | 571 | |
0316808c FC |
572 | // Position trace at event rank 4 * 'cacheSize' |
573 | tmpContext = fExperiment.seekEvent(4 * cacheSize); | |
574 | context = fExperiment.seekEvent(tmpContext.getLocation()); | |
25e48683 | 575 | |
c32744d6 | 576 | event = fExperiment.getNext(context); |
0316808c | 577 | assertEquals("Event timestamp", 4 * cacheSize + 1, event.getTimestamp().getValue()); |
25e48683 | 578 | |
c32744d6 | 579 | event = fExperiment.getNext(context); |
0316808c | 580 | assertEquals("Event timestamp", 4 * cacheSize + 2, event.getTimestamp().getValue()); |
d18dd09b ASL |
581 | } |
582 | ||
6e1886bc | 583 | @Test |
54a7a54c | 584 | public void testSeekLocationNotOnCacheBoundary() { |
0316808c | 585 | long cacheSize = fExperiment.getCacheSize(); |
25e48683 | 586 | |
0316808c FC |
587 | // Position trace at event 'cacheSize' - 1 |
588 | ITmfContext tmpContext = fExperiment.seekEvent(cacheSize - 1); | |
589 | ITmfContext context = fExperiment.seekEvent(tmpContext.getLocation()); | |
d18dd09b | 590 | |
c32744d6 | 591 | ITmfEvent event = fExperiment.getNext(context); |
0316808c | 592 | assertEquals("Event timestamp", cacheSize, event.getTimestamp().getValue()); |
25e48683 | 593 | |
c32744d6 | 594 | event = fExperiment.getNext(context); |
0316808c | 595 | assertEquals("Event timestamp", cacheSize + 1, event.getTimestamp().getValue()); |
d18dd09b | 596 | |
0316808c FC |
597 | // Position trace at event rank 2 * 'cacheSize' - 1 |
598 | tmpContext = fExperiment.seekEvent(2 * cacheSize - 1); | |
599 | context = fExperiment.seekEvent(tmpContext.getLocation()); | |
600 | context = fExperiment.seekEvent(2 * cacheSize - 1); | |
25e48683 | 601 | |
c32744d6 | 602 | event = fExperiment.getNext(context); |
0316808c | 603 | assertEquals("Event timestamp", 2 * cacheSize, event.getTimestamp().getValue()); |
25e48683 | 604 | |
c32744d6 | 605 | event = fExperiment.getNext(context); |
0316808c | 606 | assertEquals("Event timestamp", 2 * cacheSize + 1, event.getTimestamp().getValue()); |
d18dd09b | 607 | |
25e48683 | 608 | // Position trace at event rank 4500 |
0316808c FC |
609 | tmpContext = fExperiment.seekEvent(4500); |
610 | context = fExperiment.seekEvent(tmpContext.getLocation()); | |
25e48683 | 611 | |
c32744d6 | 612 | event = fExperiment.getNext(context); |
d18dd09b | 613 | assertEquals("Event timestamp", 4501, event.getTimestamp().getValue()); |
25e48683 | 614 | |
c32744d6 | 615 | event = fExperiment.getNext(context); |
0316808c | 616 | assertEquals("Event timestamp", 4502, event.getTimestamp().getValue()); |
d18dd09b ASL |
617 | } |
618 | ||
6e1886bc | 619 | @Test |
54a7a54c | 620 | public void testSeekLocationOutOfScope() { |
25e48683 | 621 | // Position trace at beginning |
1e1bef82 | 622 | ITmfContext context = fExperiment.seekEvent((ITmfLocation) null); |
25e48683 | 623 | |
c32744d6 | 624 | ITmfEvent event = fExperiment.getNext(context); |
d18dd09b | 625 | assertEquals("Event timestamp", 1, event.getTimestamp().getValue()); |
d18dd09b | 626 | } |
25e48683 | 627 | |
9b635e61 | 628 | // ------------------------------------------------------------------------ |
17324c9a | 629 | // getNext - updates the context |
9b635e61 FC |
630 | // ------------------------------------------------------------------------ |
631 | ||
54a7a54c | 632 | private static void validateContextRanks(ITmfContext context) { |
17324c9a FC |
633 | assertTrue("Experiment context type", context instanceof TmfExperimentContext); |
634 | TmfExperimentContext ctx = (TmfExperimentContext) context; | |
9b635e61 | 635 | |
07ef7847 | 636 | int nbTraces = ctx.getNbTraces(); |
17324c9a FC |
637 | |
638 | // expRank = sum(trace ranks) - nbTraces + 1 (if lastTraceRead != NO_TRACE) | |
639 | long expRank = -nbTraces + ((ctx.getLastTrace() != TmfExperimentContext.NO_TRACE) ? 1 : 0); | |
640 | for (int i = 0; i < nbTraces; i++) { | |
07ef7847 AM |
641 | ITmfContext subContext = ctx.getContext(i); |
642 | assertNotNull(subContext); | |
643 | long rank = subContext.getRank(); | |
17324c9a FC |
644 | if (rank == -1) { |
645 | expRank = -1; | |
646 | break; | |
647 | } | |
648 | expRank += rank; | |
649 | } | |
650 | assertEquals("Experiment context rank", expRank, ctx.getRank()); | |
651 | } | |
652 | ||
6e1886bc | 653 | @Test |
54a7a54c | 654 | public void testGetNextAfteSeekingOnTS_1() { |
9b635e61 | 655 | |
17324c9a FC |
656 | final long INITIAL_TS = 1; |
657 | final int NB_READS = 20; | |
658 | ||
659 | // On lower bound, returns the first event (ts = 1) | |
b2c971ec | 660 | final ITmfContext context = fExperiment.seekEvent(TmfTimestamp.create(INITIAL_TS, SCALE)); |
17324c9a FC |
661 | |
662 | validateContextRanks(context); | |
663 | ||
664 | // Read NB_EVENTS | |
665 | ITmfEvent event; | |
666 | for (int i = 0; i < NB_READS; i++) { | |
c32744d6 | 667 | event = fExperiment.getNext(context); |
17324c9a FC |
668 | assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue()); |
669 | assertEquals("Event rank", INITIAL_TS + i, context.getRank()); | |
9b635e61 | 670 | } |
17324c9a FC |
671 | |
672 | // Make sure we stay positioned | |
673 | event = fExperiment.parseEvent(context); | |
674 | assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue()); | |
675 | assertEquals("Event rank", INITIAL_TS + NB_READS - 1, context.getRank()); | |
676 | ||
677 | validateContextRanks(context); | |
9b635e61 FC |
678 | } |
679 | ||
6e1886bc | 680 | @Test |
54a7a54c | 681 | public void testGetNextAfteSeekingOnTS_2() { |
17324c9a FC |
682 | final long INITIAL_TS = 2; |
683 | final int NB_READS = 20; | |
684 | ||
685 | // On lower bound, returns the first event (ts = 2) | |
b2c971ec | 686 | final ITmfContext context = fExperiment.seekEvent(TmfTimestamp.create(INITIAL_TS, SCALE)); |
17324c9a FC |
687 | |
688 | validateContextRanks(context); | |
689 | ||
690 | // Read NB_EVENTS | |
691 | ITmfEvent event; | |
692 | for (int i = 0; i < NB_READS; i++) { | |
693 | event = fExperiment.getNext(context); | |
694 | assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue()); | |
695 | assertEquals("Event rank", INITIAL_TS + i, context.getRank()); | |
696 | } | |
697 | ||
698 | // Make sure we stay positioned | |
699 | event = fExperiment.parseEvent(context); | |
700 | assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue()); | |
701 | assertEquals("Event rank", INITIAL_TS + NB_READS - 1, context.getRank()); | |
702 | ||
703 | validateContextRanks(context); | |
704 | } | |
705 | ||
6e1886bc | 706 | @Test |
54a7a54c | 707 | public void testGetNextAfteSeekingOnTS_3() { |
17324c9a FC |
708 | |
709 | final long INITIAL_TS = 500; | |
710 | final int NB_READS = 20; | |
711 | ||
712 | // On lower bound, returns the first event (ts = 500) | |
b2c971ec | 713 | final ITmfContext context = fExperiment.seekEvent(TmfTimestamp.create(INITIAL_TS, SCALE)); |
17324c9a FC |
714 | |
715 | validateContextRanks(context); | |
716 | ||
717 | // Read NB_EVENTS | |
718 | ITmfEvent event; | |
719 | for (int i = 0; i < NB_READS; i++) { | |
720 | event = fExperiment.getNext(context); | |
721 | assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue()); | |
722 | assertEquals("Event rank", INITIAL_TS + i, context.getRank()); | |
723 | } | |
724 | ||
725 | // Make sure we stay positioned | |
726 | event = fExperiment.parseEvent(context); | |
727 | assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue()); | |
728 | assertEquals("Event rank", INITIAL_TS + NB_READS - 1, context.getRank()); | |
729 | ||
730 | validateContextRanks(context); | |
731 | } | |
732 | ||
6e1886bc | 733 | @Test |
54a7a54c | 734 | public void testGetNextAfterSeekingOnRank_1() { |
17324c9a FC |
735 | final long INITIAL_RANK = 0L; |
736 | final int NB_READS = 20; | |
737 | ||
738 | // On lower bound, returns the first event (rank = 0) | |
739 | final ITmfContext context = fExperiment.seekEvent(INITIAL_RANK); | |
740 | ||
741 | validateContextRanks(context); | |
742 | ||
743 | // Read NB_EVENTS | |
744 | ITmfEvent event; | |
745 | for (int i = 0; i < NB_READS; i++) { | |
746 | event = fExperiment.getNext(context); | |
747 | assertEquals("Event timestamp", INITIAL_RANK + i + 1, event.getTimestamp().getValue()); | |
748 | assertEquals("Event rank", INITIAL_RANK + i + 1, context.getRank()); | |
749 | } | |
750 | ||
751 | // Make sure we stay positioned | |
752 | event = fExperiment.parseEvent(context); | |
753 | assertEquals("Event timestamp", INITIAL_RANK + NB_READS + 1, event.getTimestamp().getValue()); | |
754 | assertEquals("Event rank", INITIAL_RANK + NB_READS, context.getRank()); | |
755 | ||
756 | validateContextRanks(context); | |
757 | } | |
758 | ||
6e1886bc | 759 | @Test |
54a7a54c | 760 | public void testGetNextAfterSeekingOnRank_2() { |
17324c9a FC |
761 | final long INITIAL_RANK = 1L; |
762 | final int NB_READS = 20; | |
763 | ||
764 | // On lower bound, returns the first event (rank = 0) | |
765 | final ITmfContext context = fExperiment.seekEvent(INITIAL_RANK); | |
766 | ||
767 | validateContextRanks(context); | |
768 | ||
769 | // Read NB_EVENTS | |
770 | ITmfEvent event; | |
771 | for (int i = 0; i < NB_READS; i++) { | |
772 | event = fExperiment.getNext(context); | |
773 | assertEquals("Event timestamp", INITIAL_RANK + i + 1, event.getTimestamp().getValue()); | |
774 | assertEquals("Event rank", INITIAL_RANK + i + 1, context.getRank()); | |
775 | } | |
776 | ||
777 | // Make sure we stay positioned | |
778 | event = fExperiment.parseEvent(context); | |
779 | assertEquals("Event timestamp", INITIAL_RANK + NB_READS + 1, event.getTimestamp().getValue()); | |
780 | assertEquals("Event rank", INITIAL_RANK + NB_READS, context.getRank()); | |
781 | ||
782 | validateContextRanks(context); | |
783 | } | |
784 | ||
6e1886bc | 785 | @Test |
54a7a54c | 786 | public void testGetNextAfterSeekingOnRank_3() { |
17324c9a FC |
787 | final long INITIAL_RANK = 500L; |
788 | final int NB_READS = 20; | |
789 | ||
790 | // On lower bound, returns the first event (rank = 0) | |
791 | final ITmfContext context = fExperiment.seekEvent(INITIAL_RANK); | |
792 | ||
793 | validateContextRanks(context); | |
794 | ||
795 | // Read NB_EVENTS | |
796 | ITmfEvent event; | |
797 | for (int i = 0; i < NB_READS; i++) { | |
798 | event = fExperiment.getNext(context); | |
799 | assertEquals("Event timestamp", INITIAL_RANK + i + 1, event.getTimestamp().getValue()); | |
800 | assertEquals("Event rank", INITIAL_RANK + i + 1, context.getRank()); | |
801 | } | |
802 | ||
803 | // Make sure we stay positioned | |
804 | event = fExperiment.parseEvent(context); | |
805 | assertEquals("Event timestamp", INITIAL_RANK + NB_READS + 1, event.getTimestamp().getValue()); | |
806 | assertEquals("Event rank", INITIAL_RANK + NB_READS, context.getRank()); | |
807 | ||
808 | validateContextRanks(context); | |
809 | } | |
810 | ||
6e1886bc | 811 | @Test |
54a7a54c | 812 | public void testGetNextAfterSeekingOnLocation_1() { |
1e1bef82 | 813 | final ITmfLocation INITIAL_LOC = null; |
17324c9a FC |
814 | final long INITIAL_TS = 1; |
815 | final int NB_READS = 20; | |
816 | ||
817 | // On lower bound, returns the first event (ts = 1) | |
818 | final ITmfContext context = fExperiment.seekEvent(INITIAL_LOC); | |
819 | ||
820 | validateContextRanks(context); | |
821 | ||
822 | // Read NB_EVENTS | |
823 | ITmfEvent event; | |
824 | for (int i = 0; i < NB_READS; i++) { | |
825 | event = fExperiment.getNext(context); | |
826 | assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue()); | |
827 | assertEquals("Event rank", INITIAL_TS + i, context.getRank()); | |
828 | } | |
829 | ||
830 | // Make sure we stay positioned | |
831 | event = fExperiment.parseEvent(context); | |
832 | assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue()); | |
833 | assertEquals("Event rank", INITIAL_TS + NB_READS - 1, context.getRank()); | |
834 | ||
835 | validateContextRanks(context); | |
836 | } | |
837 | ||
6e1886bc | 838 | @Test |
54a7a54c | 839 | public void testGetNextAfterSeekingOnLocation_2() { |
1e1bef82 | 840 | final ITmfLocation INITIAL_LOC = fExperiment.seekEvent(1L).getLocation(); |
17324c9a FC |
841 | final long INITIAL_TS = 2; |
842 | final int NB_READS = 20; | |
843 | ||
844 | // On lower bound, returns the first event (ts = 2) | |
845 | final ITmfContext context = fExperiment.seekEvent(INITIAL_LOC); | |
846 | ||
847 | validateContextRanks(context); | |
848 | ||
849 | // Read NB_EVENTS | |
850 | ITmfEvent event; | |
851 | for (int i = 0; i < NB_READS; i++) { | |
852 | event = fExperiment.getNext(context); | |
853 | assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue()); | |
854 | } | |
855 | ||
856 | // Make sure we stay positioned | |
857 | event = fExperiment.parseEvent(context); | |
858 | assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue()); | |
859 | ||
860 | validateContextRanks(context); | |
861 | } | |
862 | ||
6e1886bc | 863 | @Test |
54a7a54c | 864 | public void testGetNextAfterSeekingOnLocation_3() { |
1e1bef82 | 865 | final ITmfLocation INITIAL_LOC = fExperiment.seekEvent(500L).getLocation(); |
17324c9a FC |
866 | final long INITIAL_TS = 501; |
867 | final int NB_READS = 20; | |
868 | ||
869 | // On lower bound, returns the first event (ts = 501) | |
870 | final ITmfContext context = fExperiment.seekEvent(INITIAL_LOC); | |
871 | ||
872 | validateContextRanks(context); | |
873 | ||
874 | // Read NB_EVENTS | |
875 | ITmfEvent event; | |
876 | for (int i = 0; i < NB_READS; i++) { | |
877 | event = fExperiment.getNext(context); | |
878 | assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue()); | |
879 | } | |
880 | ||
881 | // Make sure we stay positioned | |
882 | event = fExperiment.parseEvent(context); | |
883 | assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue()); | |
884 | ||
885 | validateContextRanks(context); | |
886 | } | |
887 | ||
6e1886bc | 888 | @Test |
54a7a54c | 889 | public void testGetNextLocation() { |
17324c9a FC |
890 | ITmfContext context1 = fExperiment.seekEvent(0); |
891 | fExperiment.getNext(context1); | |
d62bb185 | 892 | ITmfLocation location = context1.getLocation(); |
17324c9a FC |
893 | ITmfEvent event1 = fExperiment.getNext(context1); |
894 | ITmfContext context2 = fExperiment.seekEvent(location); | |
895 | ITmfEvent event2 = fExperiment.getNext(context2); | |
896 | assertEquals("Event timestamp", event1.getTimestamp().getValue(), event2.getTimestamp().getValue()); | |
897 | } | |
898 | ||
6e1886bc | 899 | @Test |
54a7a54c | 900 | public void testGetNextEndLocation() { |
17324c9a FC |
901 | ITmfContext context1 = fExperiment.seekEvent(fExperiment.getNbEvents() - 1); |
902 | fExperiment.getNext(context1); | |
d62bb185 | 903 | ITmfLocation location = context1.getLocation(); |
17324c9a FC |
904 | ITmfContext context2 = fExperiment.seekEvent(location); |
905 | ITmfEvent event = fExperiment.getNext(context2); | |
906 | assertNull("Event", event); | |
907 | } | |
54a7a54c | 908 | |
d18dd09b ASL |
909 | // ------------------------------------------------------------------------ |
910 | // processRequest | |
911 | // ------------------------------------------------------------------------ | |
912 | ||
6e1886bc | 913 | @Test |
54a7a54c | 914 | public void testProcessRequestForNbEvents() throws InterruptedException { |
d18dd09b | 915 | final int nbEvents = 1000; |
ccf2bbb4 | 916 | final Vector<ITmfEvent> requestedEvents = new Vector<>(); |
d18dd09b | 917 | |
25e48683 | 918 | final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH); |
7184fc40 | 919 | final TmfEventRequest request = new TmfEventRequest(ITmfEvent.class, |
672a642a | 920 | range, 0, nbEvents, ExecutionType.FOREGROUND) { |
25e48683 | 921 | @Override |
6256d8ad | 922 | public void handleData(final ITmfEvent event) { |
25e48683 FC |
923 | super.handleData(event); |
924 | requestedEvents.add(event); | |
925 | } | |
d18dd09b ASL |
926 | }; |
927 | fExperiment.sendRequest(request); | |
928 | request.waitForCompletion(); | |
929 | ||
930 | assertEquals("nbEvents", nbEvents, requestedEvents.size()); | |
931 | assertTrue("isCompleted", request.isCompleted()); | |
932 | assertFalse("isCancelled", request.isCancelled()); | |
933 | ||
934 | // Ensure that we have distinct events. | |
25e48683 | 935 | // Don't go overboard: we are not validating the stub! |
20658947 | 936 | for (int i = 0; i < nbEvents; i++) { |
d18dd09b | 937 | assertEquals("Distinct events", i+1, requestedEvents.get(i).getTimestamp().getValue()); |
20658947 | 938 | } |
d18dd09b | 939 | } |
25e48683 | 940 | |
6e1886bc | 941 | @Test |
54a7a54c | 942 | public void testProcessRequestForAllEvents() throws InterruptedException { |
2740e05c | 943 | final int nbEvents = ITmfEventRequest.ALL_DATA; |
ccf2bbb4 | 944 | final Vector<ITmfEvent> requestedEvents = new Vector<>(); |
25e48683 | 945 | final long nbExpectedEvents = NB_EVENTS; |
d18dd09b | 946 | |
25e48683 | 947 | final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH); |
7184fc40 | 948 | final TmfEventRequest request = new TmfEventRequest(ITmfEvent.class, |
672a642a | 949 | range, 0, nbEvents, ExecutionType.FOREGROUND) { |
25e48683 | 950 | @Override |
6256d8ad | 951 | public void handleData(final ITmfEvent event) { |
25e48683 FC |
952 | super.handleData(event); |
953 | requestedEvents.add(event); | |
954 | } | |
d18dd09b ASL |
955 | }; |
956 | fExperiment.sendRequest(request); | |
957 | request.waitForCompletion(); | |
958 | ||
959 | assertEquals("nbEvents", nbExpectedEvents, requestedEvents.size()); | |
960 | assertTrue("isCompleted", request.isCompleted()); | |
961 | assertFalse("isCancelled", request.isCancelled()); | |
962 | ||
963 | // Ensure that we have distinct events. | |
25e48683 | 964 | // Don't go overboard: we are not validating the stub! |
20658947 | 965 | for (int i = 0; i < nbExpectedEvents; i++) { |
d18dd09b | 966 | assertEquals("Distinct events", i+1, requestedEvents.get(i).getTimestamp().getValue()); |
20658947 | 967 | } |
d18dd09b | 968 | } |
25e48683 | 969 | |
d18dd09b ASL |
970 | // ------------------------------------------------------------------------ |
971 | // cancel | |
972 | // ------------------------------------------------------------------------ | |
973 | ||
6e1886bc | 974 | @Test |
54a7a54c | 975 | public void testCancel() throws InterruptedException { |
672a642a AM |
976 | final int nbEvents = NB_EVENTS; |
977 | final int limit = BLOCK_SIZE; | |
ccf2bbb4 | 978 | final Vector<ITmfEvent> requestedEvents = new Vector<>(); |
d18dd09b | 979 | |
25e48683 | 980 | final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH); |
7184fc40 | 981 | final TmfEventRequest request = new TmfEventRequest(ITmfEvent.class, |
672a642a | 982 | range, 0, nbEvents, ExecutionType.FOREGROUND) { |
25e48683 | 983 | int nbRead = 0; |
672a642a | 984 | |
25e48683 | 985 | @Override |
6256d8ad | 986 | public void handleData(final ITmfEvent event) { |
25e48683 FC |
987 | super.handleData(event); |
988 | requestedEvents.add(event); | |
672a642a | 989 | if (++nbRead == limit) { |
25e48683 | 990 | cancel(); |
20658947 | 991 | } |
25e48683 | 992 | } |
672a642a | 993 | |
cb866e08 FC |
994 | @Override |
995 | public void handleCancel() { | |
672a642a | 996 | if (requestedEvents.size() < limit) { |
25e48683 | 997 | System.out.println("aie"); |
20658947 | 998 | } |
cb866e08 | 999 | } |
d18dd09b ASL |
1000 | }; |
1001 | fExperiment.sendRequest(request); | |
1002 | request.waitForCompletion(); | |
1003 | ||
672a642a | 1004 | assertEquals("nbEvents", limit, requestedEvents.size()); |
d18dd09b ASL |
1005 | assertTrue("isCompleted", request.isCompleted()); |
1006 | assertTrue("isCancelled", request.isCancelled()); | |
1007 | } | |
25e48683 | 1008 | |
472ea248 AM |
1009 | private static abstract class TestTrace extends TmfXmlTraceStub implements ITmfPropertiesProvider { |
1010 | ||
1011 | } | |
1012 | ||
1013 | /** | |
1014 | * Tests that experiment with traces from the same host and a clock offset | |
1015 | * are well synchronized | |
1016 | */ | |
1017 | @Test | |
1018 | public void testWithSingleHostClockOffset() { | |
1019 | // Data for this specific test | |
1020 | String hostId = "Test Host 1"; | |
1021 | long minOffset = 2000; | |
1022 | long offset = 1000; | |
1023 | String clockOffset = "clock_offset"; | |
1024 | ||
1025 | ITmfTrace t1 = new TestTrace() { | |
1026 | @Override | |
1027 | public @NonNull String getHostId() { | |
1028 | return hostId; | |
1029 | } | |
1030 | @Override | |
1031 | public @NonNull Map<@NonNull String, @NonNull String> getProperties() { | |
1032 | return ImmutableMap.of(clockOffset, String.valueOf(minOffset)); | |
1033 | } | |
1034 | }; | |
1035 | ||
1036 | ITmfTrace t2 = new TestTrace() { | |
1037 | @Override | |
1038 | public @NonNull String getHostId() { | |
1039 | return hostId; | |
1040 | } | |
1041 | @Override | |
1042 | public @NonNull Map<@NonNull String, @NonNull String> getProperties() { | |
1043 | return ImmutableMap.of(clockOffset, String.valueOf(minOffset + offset)); | |
1044 | } | |
1045 | }; | |
1046 | ||
1047 | TmfExperiment exp = new TmfExperimentStub(EXPERIMENT, new ITmfTrace[] { t1, t2 }, BLOCK_SIZE); | |
1048 | ||
1049 | try { | |
1050 | assertEquals(TimestampTransformFactory.createWithOffset(offset / 2), t1.getTimestampTransform()); | |
1051 | assertEquals(TimestampTransformFactory.createWithOffset(-offset / 2), t2.getTimestampTransform()); | |
1052 | ||
1053 | } finally { | |
1054 | exp.dispose(); | |
1055 | } | |
1056 | } | |
1057 | ||
1058 | /** | |
1059 | * Tests that opening an experiment whose traces already have a | |
1060 | * synchronization formula will not eliminate that formula. This test makes | |
1061 | * the supposition that the experiment was synchronized and the | |
1062 | * synchronization added the clock offset correction to the total formula. | |
1063 | */ | |
1064 | @Test | |
1065 | public void testWithMultiHostClockOffset() { | |
1066 | // Data for this specific test | |
1067 | String hostId = "Test Host 1"; | |
1068 | String hostId2 = "Test Host 2"; | |
1069 | long minOffset = 2000; | |
1070 | long offset = 1000; | |
1071 | String clockOffset = "clock_offset"; | |
1072 | ||
1073 | ITmfTimestampTransform tt1 = TimestampTransformFactory.createLinear(2.0, offset / 2); | |
1074 | ITmfTimestampTransform tt2 = TimestampTransformFactory.createLinear(2.0, -offset / 2); | |
1075 | ITmfTimestampTransform tt3 = TimestampTransformFactory.createWithOffset(offset); | |
1076 | ||
1077 | ITmfTrace t1 = new TestTrace() { | |
1078 | @Override | |
1079 | public @NonNull String getHostId() { | |
1080 | return hostId; | |
1081 | } | |
1082 | @Override | |
1083 | public @NonNull Map<@NonNull String, @NonNull String> getProperties() { | |
1084 | return ImmutableMap.of(clockOffset, String.valueOf(minOffset)); | |
1085 | } | |
1086 | ||
1087 | }; | |
1088 | t1.setTimestampTransform(tt1); | |
1089 | ||
1090 | ITmfTrace t2 = new TestTrace() { | |
1091 | @Override | |
1092 | public @NonNull String getHostId() { | |
1093 | return hostId; | |
1094 | } | |
1095 | @Override | |
1096 | public @NonNull Map<@NonNull String, @NonNull String> getProperties() { | |
1097 | return ImmutableMap.of(clockOffset, String.valueOf(minOffset + offset)); | |
1098 | } | |
1099 | }; | |
1100 | t2.setTimestampTransform(tt2); | |
1101 | ||
1102 | ITmfTrace t3 = new TmfXmlTraceStub() { | |
1103 | @Override | |
1104 | public @NonNull String getHostId() { | |
1105 | return hostId2; | |
1106 | } | |
1107 | }; | |
1108 | t3.setTimestampTransform(tt3); | |
1109 | ||
1110 | TmfExperiment exp = new TmfExperimentStub(EXPERIMENT, new ITmfTrace[] { t1, t2, t3 }, BLOCK_SIZE) { | |
1111 | ||
1112 | @Override | |
1113 | public SynchronizationAlgorithm synchronizeTraces() { | |
1114 | return new SyncAlgorithmFullyIncremental() { | |
1115 | ||
1116 | private static final long serialVersionUID = 4206172498287480153L; | |
1117 | ||
1118 | @Override | |
1119 | public ITmfTimestampTransform getTimestampTransform(String h) { | |
1120 | if (hostId.equals(h)) { | |
1121 | return TimestampTransformFactory.createLinear(2.0, 0); | |
1122 | } | |
1123 | return tt3; | |
1124 | } | |
1125 | }; | |
1126 | } | |
1127 | }; | |
1128 | ||
1129 | try { | |
1130 | assertEquals(tt1, t1.getTimestampTransform()); | |
1131 | assertEquals(tt2, t2.getTimestampTransform()); | |
1132 | assertEquals(tt3, t3.getTimestampTransform()); | |
1133 | ||
1134 | } finally { | |
1135 | exp.dispose(); | |
1136 | } | |
1137 | } | |
1138 | ||
17324c9a | 1139 | } |