BTree index on disk
[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
25 import org.eclipse.core.runtime.FileLocator;
26 import org.eclipse.core.runtime.Path;
27 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
28 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
29 import org.eclipse.linuxtools.tmf.core.tests.TmfCoreTestPlugin;
30 import org.eclipse.linuxtools.tmf.core.tests.shared.TmfTestTrace;
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.indexer.checkpoint.ITmfCheckpointIndex;
37 import org.eclipse.linuxtools.tmf.core.trace.location.ITmfLocation;
38 import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfExperimentStub;
39 import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub;
40 import org.junit.After;
41 import org.junit.Before;
42 import org.junit.Test;
43
44 /**
45 * Test suite for the TmfCheckpointIndexTest class.
46 */
47 @SuppressWarnings("javadoc")
48 public class TmfExperimentCheckpointIndexTest {
49
50 // ------------------------------------------------------------------------
51 // Attributes
52 // ------------------------------------------------------------------------
53
54 private static final String EXPERIMENT = "MyExperiment";
55 private static final TmfTestTrace TEST_TRACE1 = TmfTestTrace.O_TEST_10K;
56 private static final TmfTestTrace TEST_TRACE2 = TmfTestTrace.E_TEST_10K;
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
86 fTestTraces = new ITmfTrace[2];
87 try {
88 URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(TEST_TRACE1.getFullPath()), null);
89 File test = new File(FileLocator.toFileURL(location).toURI());
90 final TmfTraceStub trace1 = new TmfTraceStub(test.getPath(), 0, true, null, null);
91 fTestTraces[0] = trace1;
92 location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(TEST_TRACE2.getFullPath()), null);
93 test = new File(FileLocator.toFileURL(location).toURI());
94 final TmfTraceStub trace2 = new TmfTraceStub(test.getPath(), 0, true, null, null);
95 fTestTraces[1] = trace2;
96 } catch (final TmfTraceException e) {
97 e.printStackTrace();
98 } catch (final URISyntaxException e) {
99 e.printStackTrace();
100 } catch (final IOException e) {
101 e.printStackTrace();
102 }
103 }
104
105 // ------------------------------------------------------------------------
106 // Verify checkpoints
107 // ------------------------------------------------------------------------
108
109 @Test
110 public void testTmfTraceIndexing() {
111 assertEquals("getCacheSize", BLOCK_SIZE, fExperiment.getCacheSize());
112 assertEquals("getTraceSize", NB_EVENTS, fExperiment.getNbEvents());
113 assertEquals("getRange-start", 1, fExperiment.getTimeRange().getStartTime().getValue());
114 assertEquals("getRange-end", NB_EVENTS, fExperiment.getTimeRange().getEndTime().getValue());
115 assertEquals("getStartTime", 1, fExperiment.getStartTime().getValue());
116 assertEquals("getEndTime", NB_EVENTS, fExperiment.getEndTime().getValue());
117
118 ITmfCheckpointIndex checkpoints = fExperiment.getIndexer().getCheckpoints();
119 int pageSize = fExperiment.getCacheSize();
120 assertTrue("Checkpoints exist", checkpoints != null);
121 assertEquals("Checkpoints size", NB_EVENTS / BLOCK_SIZE, checkpoints.size());
122
123 // Validate that each checkpoint points to the right event
124 for (int i = 0; i < checkpoints.size(); i++) {
125 ITmfCheckpoint checkpoint = checkpoints.get(i);
126 ITmfLocation location = checkpoint.getLocation();
127 ITmfContext context = fExperiment.seekEvent(location);
128 ITmfEvent event = fExperiment.parseEvent(context);
129 assertTrue(context.getRank() == i * pageSize);
130 assertTrue((checkpoint.getTimestamp().compareTo(event.getTimestamp(), false) == 0));
131 }
132 }
133
134 // ------------------------------------------------------------------------
135 // Streaming
136 // ------------------------------------------------------------------------
137
138 @Test
139 public void testGrowingIndex() {
140 ITmfTrace[] testTraces = new TmfTraceStub[2];
141 try {
142 URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(TEST_TRACE1.getFullPath()), null);
143 File test = new File(FileLocator.toFileURL(location).toURI());
144 final TmfTraceStub trace1 = new TmfTraceStub(test.getPath(), 0, false, null, null);
145 testTraces[0] = trace1;
146 location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(TEST_TRACE2.getFullPath()), null);
147 test = new File(FileLocator.toFileURL(location).toURI());
148 final TmfTraceStub trace2 = new TmfTraceStub(test.getPath(), 0, false, null, null);
149 testTraces[1] = trace2;
150 } catch (final TmfTraceException e) {
151 e.printStackTrace();
152 } catch (final URISyntaxException e) {
153 e.printStackTrace();
154 } catch (final IOException e) {
155 e.printStackTrace();
156 }
157
158 TmfExperimentStub experiment = new TmfExperimentStub(EXPERIMENT, testTraces, BLOCK_SIZE);
159 int pageSize = experiment.getCacheSize();
160
161 // Build the first half of the index
162 TmfTimeRange range = new TmfTimeRange(new TmfTimestamp(1, -3), new TmfTimestamp(NB_EVENTS / 2 - 1, -3));
163 experiment.getIndexer().buildIndex(0, range, true);
164
165 // Validate that each checkpoint points to the right event
166 ITmfCheckpointIndex checkpoints = experiment.getIndexer().getCheckpoints();
167 assertTrue("Checkpoints exist", checkpoints != null);
168 assertEquals("Checkpoints size", NB_EVENTS / BLOCK_SIZE / 2, checkpoints.size());
169
170 // Build the second half of the index
171 experiment.getIndexer().buildIndex(NB_EVENTS / 2, TmfTimeRange.ETERNITY, true);
172
173 // Validate that each checkpoint points to the right event
174 assertEquals("Checkpoints size", NB_EVENTS / BLOCK_SIZE, checkpoints.size());
175 for (int i = 0; i < checkpoints.size(); i++) {
176 ITmfCheckpoint checkpoint = checkpoints.get(i);
177 ITmfLocation location = checkpoint.getLocation();
178 ITmfContext context = experiment.seekEvent(location);
179 ITmfEvent event = experiment.parseEvent(context);
180 assertTrue(context.getRank() == i * pageSize);
181 assertTrue((checkpoint.getTimestamp().compareTo(event.getTimestamp(), false) == 0));
182 assertEquals("Checkpoint value", i * pageSize + 1, checkpoint.getTimestamp().getValue());
183 }
184
185 /* Clean up (since we didn't use the class-specific fixtures) */
186 experiment.dispose();
187 for (ITmfTrace trace : testTraces) {
188 trace.dispose();
189 }
190 }
191
192 }
This page took 0.034357 seconds and 5 git commands to generate.