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
CommitLineData
07671572
FC
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
9e0640dc 13package org.eclipse.linuxtools.tmf.core.tests.trace;
07671572
FC
14
15import java.io.File;
16import java.io.IOException;
17import java.net.URISyntaxException;
18import java.net.URL;
19import java.util.List;
20
21import junit.framework.TestCase;
22
23import org.eclipse.core.runtime.FileLocator;
24import org.eclipse.core.runtime.Path;
9e0640dc
FC
25import org.eclipse.linuxtools.internal.tmf.core.trace.TmfExperimentContext;
26import org.eclipse.linuxtools.internal.tmf.core.trace.TmfExperimentLocation;
27import org.eclipse.linuxtools.internal.tmf.core.trace.TmfLocationArray;
07671572 28import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
9e0640dc
FC
29import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
30import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
3427112b 31import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
07671572 32import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
07671572 33import org.eclipse.linuxtools.tmf.core.tests.TmfCoreTestPlugin;
3427112b 34import org.eclipse.linuxtools.tmf.core.trace.ITmfCheckpoint;
07671572
FC
35import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
36import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
07671572 37import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
9e0640dc 38import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfExperimentStub;
07671572
FC
39import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub;
40
41/**
42 * Test suite for the TmfCheckpointIndexTest class.
43 */
2d223a34 44@SuppressWarnings({ "nls" })
07671572
FC
45public 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
9e0640dc 58 private static ITmfTrace<TmfEvent>[] fTestTraces;
0879b6b9 59 private static TmfExperimentStub<TmfEvent> fExperiment;
07671572
FC
60
61 // ------------------------------------------------------------------------
62 // Housekeeping
63 // ------------------------------------------------------------------------
64
65 public TmfExperimentCheckpointIndexTest(final String name) throws Exception {
66 super(name);
67 }
68
69 @Override
0879b6b9 70 protected synchronized void setUp() throws Exception {
07671572 71 super.setUp();
07671572 72 if (fExperiment == null) {
3427112b 73 setupTrace(DIRECTORY + File.separator + TEST_STREAM1, DIRECTORY + File.separator + TEST_STREAM2);
0879b6b9 74 fExperiment = new TmfExperimentStub<TmfEvent>(EXPERIMENT, fTestTraces, BLOCK_SIZE);
9e0640dc 75 fExperiment.getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, true);
07671572
FC
76 }
77 }
78
79 @Override
80 protected void tearDown() throws Exception {
81 super.tearDown();
82 fExperiment.dispose();
83 fExperiment = null;
3427112b 84 fTestTraces = null;
07671572
FC
85 }
86
9e0640dc 87 @SuppressWarnings("unchecked")
0879b6b9 88 private synchronized static ITmfTrace<?>[] setupTrace(final String path1, final String path2) {
c7e1020d
FC
89 if (fTestTraces == null) {
90 fTestTraces = new ITmfTrace[2];
07671572
FC
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);
c7e1020d 95 fTestTraces[0] = trace1;
07671572
FC
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);
c7e1020d 99 fTestTraces[1] = trace2;
07671572
FC
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 }
c7e1020d 108 return fTestTraces;
07671572
FC
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
3427112b 123 List<ITmfCheckpoint> checkpoints = fExperiment.getIndexer().getCheckpoints();
07671572
FC
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++) {
3427112b 130 ITmfCheckpoint checkpoint = checkpoints.get(i);
07671572
FC
131 TmfExperimentLocation expLocation = (TmfExperimentLocation) checkpoint.getLocation();
132 TmfLocationArray locations = expLocation.getLocation();
133 ITmfContext[] trcContexts = new ITmfContext[2];
9a7f542f
PT
134 trcContexts[0] = new TmfContext(locations.getLocations()[0].getLocation(), (i * pageSize) / 2);
135 trcContexts[1] = new TmfContext(locations.getLocations()[1].getLocation(), (i * pageSize) / 2);
07671572 136 TmfExperimentContext expContext = new TmfExperimentContext(trcContexts);
c7e1020d
FC
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));
07671572
FC
139 ITmfEvent event = fExperiment.parseEvent(expContext);
140 assertTrue(expContext.getRank() == i * pageSize);
141 assertTrue((checkpoint.getTimestamp().compareTo(event.getTimestamp(), false) == 0));
142 }
143 }
144
3427112b
FC
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];
9a7f542f
PT
191 trcContexts[0] = new TmfContext(locations.getLocations()[0].getLocation(), (i * pageSize) / 2);
192 trcContexts[1] = new TmfContext(locations.getLocations()[1].getLocation(), (i * pageSize) / 2);
3427112b
FC
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
07671572 203}
This page took 0.034876 seconds and 5 git commands to generate.