tmf: Clean up tmf.core.trace package
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / trace / indexer / checkpoint / TmfExperimentCheckpointIndexTest.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 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 * Alexandre Montplaisir - Port to JUnit4
12 * Patrick Tasse - Updated for ranks in experiment location
13 *******************************************************************************/
14
15 package org.eclipse.linuxtools.tmf.core.tests.trace.indexer.checkpoint;
16
17 import static org.junit.Assert.assertEquals;
18 import static org.junit.Assert.assertTrue;
19
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
26 import org.eclipse.core.runtime.FileLocator;
27 import org.eclipse.core.runtime.Path;
28 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
29 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
30 import org.eclipse.linuxtools.tmf.core.tests.TmfCoreTestPlugin;
31 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange;
32 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
33 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
34 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
35 import org.eclipse.linuxtools.tmf.core.trace.indexer.checkpoint.ITmfCheckpoint;
36 import org.eclipse.linuxtools.tmf.core.trace.location.ITmfLocation;
37 import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfExperimentStub;
38 import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub;
39 import org.junit.After;
40 import org.junit.Before;
41 import org.junit.Test;
42
43 /**
44 * Test suite for the TmfCheckpointIndexTest class.
45 */
46 @SuppressWarnings("javadoc")
47 public class TmfExperimentCheckpointIndexTest {
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
60 private static ITmfTrace[] fTestTraces;
61 private static TmfExperimentStub fExperiment;
62
63 // ------------------------------------------------------------------------
64 // Housekeeping
65 // ------------------------------------------------------------------------
66
67 @Before
68 public void setUp() {
69 setupTraces();
70 fExperiment = new TmfExperimentStub(EXPERIMENT, fTestTraces, BLOCK_SIZE);
71 fExperiment.getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, true);
72 }
73
74 @After
75 public void tearDown() {
76 fExperiment.dispose();
77 fExperiment = null;
78 for (ITmfTrace trace : fTestTraces) {
79 trace.dispose();
80 }
81 fTestTraces = null;
82 }
83
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();
104 }
105 }
106
107 // ------------------------------------------------------------------------
108 // Verify checkpoints
109 // ------------------------------------------------------------------------
110
111 @Test
112 public void testTmfTraceIndexing() {
113 assertEquals("getCacheSize", BLOCK_SIZE, fExperiment.getCacheSize());
114 assertEquals("getTraceSize", NB_EVENTS, fExperiment.getNbEvents());
115 assertEquals("getRange-start", 1, fExperiment.getTimeRange().getStartTime().getValue());
116 assertEquals("getRange-end", NB_EVENTS, fExperiment.getTimeRange().getEndTime().getValue());
117 assertEquals("getStartTime", 1, fExperiment.getStartTime().getValue());
118 assertEquals("getEndTime", NB_EVENTS, fExperiment.getEndTime().getValue());
119
120 List<ITmfCheckpoint> checkpoints = fExperiment.getIndexer().getCheckpoints();
121 int pageSize = fExperiment.getCacheSize();
122 assertTrue("Checkpoints exist", checkpoints != null);
123 assertEquals("Checkpoints size", NB_EVENTS / BLOCK_SIZE, checkpoints.size());
124
125 // Validate that each checkpoint points to the right event
126 for (int i = 0; i < checkpoints.size(); i++) {
127 ITmfCheckpoint checkpoint = checkpoints.get(i);
128 ITmfLocation location = checkpoint.getLocation();
129 ITmfContext context = fExperiment.seekEvent(location);
130 ITmfEvent event = fExperiment.parseEvent(context);
131 assertTrue(context.getRank() == i * pageSize);
132 assertTrue((checkpoint.getTimestamp().compareTo(event.getTimestamp(), false) == 0));
133 }
134 }
135
136 // ------------------------------------------------------------------------
137 // Streaming
138 // ------------------------------------------------------------------------
139
140 @Test
141 public void testGrowingIndex() {
142 ITmfTrace[] testTraces = new TmfTraceStub[2];
143 try {
144 URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(DIRECTORY + File.separator + TEST_STREAM1), null);
145 File test = new File(FileLocator.toFileURL(location).toURI());
146 final TmfTraceStub trace1 = new TmfTraceStub(test.getPath(), 0, false);
147 testTraces[0] = trace1;
148 location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(DIRECTORY + File.separator + TEST_STREAM2), null);
149 test = new File(FileLocator.toFileURL(location).toURI());
150 final TmfTraceStub trace2 = new TmfTraceStub(test.getPath(), 0, false);
151 testTraces[1] = trace2;
152 } catch (final TmfTraceException e) {
153 e.printStackTrace();
154 } catch (final URISyntaxException e) {
155 e.printStackTrace();
156 } catch (final IOException e) {
157 e.printStackTrace();
158 }
159
160 TmfExperimentStub experiment = new TmfExperimentStub(EXPERIMENT, testTraces, BLOCK_SIZE);
161 int pageSize = experiment.getCacheSize();
162
163 // Build the first half of the index
164 TmfTimeRange range = new TmfTimeRange(new TmfTimestamp(1, -3), new TmfTimestamp(NB_EVENTS / 2 - 1, -3));
165 experiment.getIndexer().buildIndex(0, range, true);
166
167 // Validate that each checkpoint points to the right event
168 List<ITmfCheckpoint> checkpoints = experiment.getIndexer().getCheckpoints();
169 assertTrue("Checkpoints exist", checkpoints != null);
170 assertEquals("Checkpoints size", NB_EVENTS / BLOCK_SIZE / 2, checkpoints.size());
171
172 // Build the second half of the index
173 experiment.getIndexer().buildIndex(NB_EVENTS / 2, TmfTimeRange.ETERNITY, true);
174
175 // Validate that each checkpoint points to the right event
176 assertEquals("Checkpoints size", NB_EVENTS / BLOCK_SIZE, checkpoints.size());
177 for (int i = 0; i < checkpoints.size(); i++) {
178 ITmfCheckpoint checkpoint = checkpoints.get(i);
179 ITmfLocation location = checkpoint.getLocation();
180 ITmfContext context = experiment.seekEvent(location);
181 ITmfEvent event = experiment.parseEvent(context);
182 assertTrue(context.getRank() == i * pageSize);
183 assertTrue((checkpoint.getTimestamp().compareTo(event.getTimestamp(), false) == 0));
184 assertEquals("Checkpoint value", i * pageSize + 1, checkpoint.getTimestamp().getValue());
185 }
186
187 /* Clean up (since we didn't use the class-specific fixtures) */
188 experiment.dispose();
189 for (ITmfTrace trace : testTraces) {
190 trace.dispose();
191 }
192 }
193
194 }
This page took 0.035592 seconds and 5 git commands to generate.