Fix for bug 381411: Implement ranked location in experiment.
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / trace / TmfExperimentCheckpointIndexTest.java
1 /*******************************************************************************
2 * Copyright (c) 2012 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 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.core.tests.trace;
14
15 import java.io.File;
16 import java.io.IOException;
17 import java.net.URISyntaxException;
18 import java.net.URL;
19 import java.util.List;
20
21 import junit.framework.TestCase;
22
23 import org.eclipse.core.runtime.FileLocator;
24 import org.eclipse.core.runtime.Path;
25 import org.eclipse.linuxtools.internal.tmf.core.trace.TmfExperimentContext;
26 import org.eclipse.linuxtools.internal.tmf.core.trace.TmfExperimentLocation;
27 import org.eclipse.linuxtools.internal.tmf.core.trace.TmfLocationArray;
28 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
29 import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
30 import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
31 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
32 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
33 import org.eclipse.linuxtools.tmf.core.tests.TmfCoreTestPlugin;
34 import org.eclipse.linuxtools.tmf.core.trace.ITmfCheckpoint;
35 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
36 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
37 import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
38 import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfExperimentStub;
39 import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub;
40
41 /**
42 * Test suite for the TmfCheckpointIndexTest class.
43 */
44 @SuppressWarnings({ "nls" })
45 public class TmfExperimentCheckpointIndexTest extends TestCase {
46
47 // ------------------------------------------------------------------------
48 // Attributes
49 // ------------------------------------------------------------------------
50
51 private static final String DIRECTORY = "testfiles";
52 private static final String TEST_STREAM1 = "O-Test-10K";
53 private static final String TEST_STREAM2 = "E-Test-10K";
54 private static final String EXPERIMENT = "MyExperiment";
55 private static int NB_EVENTS = 20000;
56 private static int BLOCK_SIZE = 1000;
57
58 private static ITmfTrace<TmfEvent>[] fTestTraces;
59 private static TmfExperimentStub<TmfEvent> fExperiment;
60
61 // ------------------------------------------------------------------------
62 // Housekeeping
63 // ------------------------------------------------------------------------
64
65 public TmfExperimentCheckpointIndexTest(final String name) throws Exception {
66 super(name);
67 }
68
69 @Override
70 protected synchronized void setUp() throws Exception {
71 super.setUp();
72 if (fExperiment == null) {
73 setupTrace(DIRECTORY + File.separator + TEST_STREAM1, DIRECTORY + File.separator + TEST_STREAM2);
74 fExperiment = new TmfExperimentStub<TmfEvent>(EXPERIMENT, fTestTraces, BLOCK_SIZE);
75 fExperiment.getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, true);
76 }
77 }
78
79 @Override
80 protected void tearDown() throws Exception {
81 super.tearDown();
82 fExperiment.dispose();
83 fExperiment = null;
84 fTestTraces = null;
85 }
86
87 @SuppressWarnings("unchecked")
88 private synchronized static ITmfTrace<?>[] setupTrace(final String path1, final String path2) {
89 if (fTestTraces == null) {
90 fTestTraces = new ITmfTrace[2];
91 try {
92 URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(path1), null);
93 File test = new File(FileLocator.toFileURL(location).toURI());
94 final TmfTraceStub trace1 = new TmfTraceStub(test.getPath(), 0, true);
95 fTestTraces[0] = trace1;
96 location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(path2), null);
97 test = new File(FileLocator.toFileURL(location).toURI());
98 final TmfTraceStub trace2 = new TmfTraceStub(test.getPath(), 0, true);
99 fTestTraces[1] = trace2;
100 } catch (final TmfTraceException e) {
101 e.printStackTrace();
102 } catch (final URISyntaxException e) {
103 e.printStackTrace();
104 } catch (final IOException e) {
105 e.printStackTrace();
106 }
107 }
108 return fTestTraces;
109 }
110
111 // ------------------------------------------------------------------------
112 // Verify checkpoints
113 // ------------------------------------------------------------------------
114
115 public void testTmfTraceIndexing() throws Exception {
116 assertEquals("getCacheSize", BLOCK_SIZE, fExperiment.getCacheSize());
117 assertEquals("getTraceSize", NB_EVENTS, fExperiment.getNbEvents());
118 assertEquals("getRange-start", 1, fExperiment.getTimeRange().getStartTime().getValue());
119 assertEquals("getRange-end", NB_EVENTS, fExperiment.getTimeRange().getEndTime().getValue());
120 assertEquals("getStartTime", 1, fExperiment.getStartTime().getValue());
121 assertEquals("getEndTime", NB_EVENTS, fExperiment.getEndTime().getValue());
122
123 List<ITmfCheckpoint> checkpoints = fExperiment.getIndexer().getCheckpoints();
124 int pageSize = fExperiment.getCacheSize();
125 assertTrue("Checkpoints exist", checkpoints != null);
126 assertEquals("Checkpoints size", NB_EVENTS / BLOCK_SIZE, checkpoints.size());
127
128 // Validate that each checkpoint points to the right event
129 for (int i = 0; i < checkpoints.size(); i++) {
130 ITmfCheckpoint checkpoint = checkpoints.get(i);
131 TmfExperimentLocation expLocation = (TmfExperimentLocation) checkpoint.getLocation();
132 TmfLocationArray locations = expLocation.getLocation();
133 ITmfContext[] trcContexts = new ITmfContext[2];
134 trcContexts[0] = new TmfContext(locations.getLocations()[0].getLocation(), (i * pageSize) / 2);
135 trcContexts[1] = new TmfContext(locations.getLocations()[1].getLocation(), (i * pageSize) / 2);
136 TmfExperimentContext expContext = new TmfExperimentContext(trcContexts);
137 expContext.getEvents()[0] = fTestTraces[0].getNext(fTestTraces[0].seekEvent((i * pageSize) / 2));
138 expContext.getEvents()[1] = fTestTraces[1].getNext(fTestTraces[1].seekEvent((i * pageSize) / 2));
139 ITmfEvent event = fExperiment.parseEvent(expContext);
140 assertTrue(expContext.getRank() == i * pageSize);
141 assertTrue((checkpoint.getTimestamp().compareTo(event.getTimestamp(), false) == 0));
142 }
143 }
144
145 // ------------------------------------------------------------------------
146 // Streaming
147 // ------------------------------------------------------------------------
148
149 public void testGrowingIndex() throws Exception {
150
151 ITmfTrace<TmfEvent>[] testTraces = new TmfTraceStub[2];
152 try {
153 URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(DIRECTORY + File.separator + TEST_STREAM1), null);
154 File test = new File(FileLocator.toFileURL(location).toURI());
155 final TmfTraceStub trace1 = new TmfTraceStub(test.getPath(), 0, false);
156 testTraces[0] = trace1;
157 location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(DIRECTORY + File.separator + TEST_STREAM2), null);
158 test = new File(FileLocator.toFileURL(location).toURI());
159 final TmfTraceStub trace2 = new TmfTraceStub(test.getPath(), 0, false);
160 testTraces[1] = trace2;
161 } catch (final TmfTraceException e) {
162 e.printStackTrace();
163 } catch (final URISyntaxException e) {
164 e.printStackTrace();
165 } catch (final IOException e) {
166 e.printStackTrace();
167 }
168
169 TmfExperimentStub<TmfEvent> experiment = new TmfExperimentStub<TmfEvent>(EXPERIMENT, testTraces, BLOCK_SIZE);
170 int pageSize = experiment.getCacheSize();
171
172 // Build the first half of the index
173 TmfTimeRange range = new TmfTimeRange(new TmfTimestamp(1, -3), new TmfTimestamp(NB_EVENTS / 2 - 1, -3));
174 experiment.getIndexer().buildIndex(0, range, true);
175
176 // Validate that each checkpoint points to the right event
177 List<ITmfCheckpoint> checkpoints = experiment.getIndexer().getCheckpoints();
178 assertTrue("Checkpoints exist", checkpoints != null);
179 assertEquals("Checkpoints size", NB_EVENTS / BLOCK_SIZE / 2, checkpoints.size());
180
181 // Build the second half of the index
182 experiment.getIndexer().buildIndex(NB_EVENTS / 2, TmfTimeRange.ETERNITY, true);
183
184 // Validate that each checkpoint points to the right event
185 assertEquals("Checkpoints size", NB_EVENTS / BLOCK_SIZE, checkpoints.size());
186 for (int i = 0; i < checkpoints.size(); i++) {
187 ITmfCheckpoint checkpoint = checkpoints.get(i);
188 TmfExperimentLocation expLocation = (TmfExperimentLocation) checkpoint.getLocation();
189 TmfLocationArray locations = expLocation.getLocation();
190 ITmfContext[] trcContexts = new ITmfContext[2];
191 trcContexts[0] = new TmfContext(locations.getLocations()[0].getLocation(), (i * pageSize) / 2);
192 trcContexts[1] = new TmfContext(locations.getLocations()[1].getLocation(), (i * pageSize) / 2);
193 TmfExperimentContext expContext = new TmfExperimentContext(trcContexts);
194 expContext.getEvents()[0] = testTraces[0].getNext(testTraces[0].seekEvent((i * pageSize) / 2));
195 expContext.getEvents()[1] = testTraces[1].getNext(testTraces[1].seekEvent((i * pageSize) / 2));
196 ITmfEvent event = experiment.parseEvent(expContext);
197 assertTrue(expContext.getRank() == i * pageSize);
198 assertTrue((checkpoint.getTimestamp().compareTo(event.getTimestamp(), false) == 0));
199 assertEquals("Checkpoint value", i * pageSize + 1, checkpoint.getTimestamp().getValue());
200 }
201 }
202
203 }
This page took 0.034649 seconds and 5 git commands to generate.