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
CommitLineData
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
a3db8436 15package org.eclipse.linuxtools.tmf.core.tests.trace.indexer.checkpoint;
07671572 16
6e1886bc
AM
17import static org.junit.Assert.assertEquals;
18import static org.junit.Assert.assertTrue;
19
07671572
FC
20import java.io.File;
21import java.io.IOException;
22import java.net.URISyntaxException;
23import java.net.URL;
07671572 24
07671572
FC
25import org.eclipse.core.runtime.FileLocator;
26import org.eclipse.core.runtime.Path;
27import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
07671572 28import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
07671572 29import org.eclipse.linuxtools.tmf.core.tests.TmfCoreTestPlugin;
9c4d52ee 30import org.eclipse.linuxtools.tmf.core.tests.shared.TmfTestTrace;
3bd46eef
AM
31import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange;
32import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
07671572
FC
33import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
34import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
a3db8436 35import org.eclipse.linuxtools.tmf.core.trace.indexer.checkpoint.ITmfCheckpoint;
032ecd45 36import org.eclipse.linuxtools.tmf.core.trace.indexer.checkpoint.ITmfCheckpointIndex;
a3db8436 37import org.eclipse.linuxtools.tmf.core.trace.location.ITmfLocation;
9e0640dc 38import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfExperimentStub;
07671572 39import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub;
6e1886bc
AM
40import org.junit.After;
41import org.junit.Before;
42import org.junit.Test;
07671572
FC
43
44/**
45 * Test suite for the TmfCheckpointIndexTest class.
46 */
cad06250 47@SuppressWarnings("javadoc")
6e1886bc 48public class TmfExperimentCheckpointIndexTest {
07671572
FC
49
50 // ------------------------------------------------------------------------
51 // Attributes
52 // ------------------------------------------------------------------------
53
07671572 54 private static final String EXPERIMENT = "MyExperiment";
9c4d52ee
GB
55 private static final TmfTestTrace TEST_TRACE1 = TmfTestTrace.O_TEST_10K;
56 private static final TmfTestTrace TEST_TRACE2 = TmfTestTrace.E_TEST_10K;
07671572
FC
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 84 private static void setupTraces() {
6e1886bc
AM
85
86 fTestTraces = new ITmfTrace[2];
87 try {
9c4d52ee 88 URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(TEST_TRACE1.getFullPath()), null);
6e1886bc 89 File test = new File(FileLocator.toFileURL(location).toURI());
ff0960ac 90 final TmfTraceStub trace1 = new TmfTraceStub(test.getPath(), 0, true, null, null);
6e1886bc 91 fTestTraces[0] = trace1;
9c4d52ee 92 location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(TEST_TRACE2.getFullPath()), null);
6e1886bc 93 test = new File(FileLocator.toFileURL(location).toURI());
ff0960ac 94 final TmfTraceStub trace2 = new TmfTraceStub(test.getPath(), 0, true, null, null);
6e1886bc
AM
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();
07671572 102 }
07671572
FC
103 }
104
105 // ------------------------------------------------------------------------
106 // Verify checkpoints
107 // ------------------------------------------------------------------------
108
6e1886bc 109 @Test
54a7a54c 110 public void testTmfTraceIndexing() {
07671572
FC
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
032ecd45 118 ITmfCheckpointIndex checkpoints = fExperiment.getIndexer().getCheckpoints();
07671572
FC
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++) {
3427112b 125 ITmfCheckpoint checkpoint = checkpoints.get(i);
ea271da6
PT
126 ITmfLocation location = checkpoint.getLocation();
127 ITmfContext context = fExperiment.seekEvent(location);
128 ITmfEvent event = fExperiment.parseEvent(context);
129 assertTrue(context.getRank() == i * pageSize);
07671572
FC
130 assertTrue((checkpoint.getTimestamp().compareTo(event.getTimestamp(), false) == 0));
131 }
132 }
133
3427112b
FC
134 // ------------------------------------------------------------------------
135 // Streaming
136 // ------------------------------------------------------------------------
137
6e1886bc 138 @Test
54a7a54c 139 public void testGrowingIndex() {
6256d8ad 140 ITmfTrace[] testTraces = new TmfTraceStub[2];
3427112b 141 try {
9c4d52ee 142 URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(TEST_TRACE1.getFullPath()), null);
3427112b 143 File test = new File(FileLocator.toFileURL(location).toURI());
ff0960ac 144 final TmfTraceStub trace1 = new TmfTraceStub(test.getPath(), 0, false, null, null);
3427112b 145 testTraces[0] = trace1;
9c4d52ee 146 location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(TEST_TRACE2.getFullPath()), null);
3427112b 147 test = new File(FileLocator.toFileURL(location).toURI());
ff0960ac 148 final TmfTraceStub trace2 = new TmfTraceStub(test.getPath(), 0, false, null, null);
3427112b
FC
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
6256d8ad 158 TmfExperimentStub experiment = new TmfExperimentStub(EXPERIMENT, testTraces, BLOCK_SIZE);
3427112b
FC
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
032ecd45 166 ITmfCheckpointIndex checkpoints = experiment.getIndexer().getCheckpoints();
3427112b
FC
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);
ea271da6
PT
177 ITmfLocation location = checkpoint.getLocation();
178 ITmfContext context = experiment.seekEvent(location);
179 ITmfEvent event = experiment.parseEvent(context);
180 assertTrue(context.getRank() == i * pageSize);
3427112b
FC
181 assertTrue((checkpoint.getTimestamp().compareTo(event.getTimestamp(), false) == 0));
182 assertEquals("Checkpoint value", i * pageSize + 1, checkpoint.getTimestamp().getValue());
183 }
6e1886bc
AM
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 }
3427112b
FC
190 }
191
07671572 192}
This page took 0.045728 seconds and 5 git commands to generate.