Commit | Line | Data |
---|---|---|
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 | 13 | package org.eclipse.linuxtools.tmf.core.tests.trace; |
07671572 FC |
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; | |
9e0640dc FC |
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; | |
07671572 | 28 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
9e0640dc FC |
29 | import org.eclipse.linuxtools.tmf.core.event.TmfEvent; |
30 | import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange; | |
07671572 | 31 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; |
07671572 FC |
32 | import org.eclipse.linuxtools.tmf.core.tests.TmfCoreTestPlugin; |
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.TmfCheckpoint; | |
07671572 | 36 | import org.eclipse.linuxtools.tmf.core.trace.TmfContext; |
9e0640dc | 37 | import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfExperimentStub; |
07671572 FC |
38 | import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub; |
39 | ||
40 | /** | |
41 | * Test suite for the TmfCheckpointIndexTest class. | |
42 | */ | |
2d223a34 | 43 | @SuppressWarnings({ "nls" }) |
07671572 FC |
44 | public class TmfExperimentCheckpointIndexTest extends TestCase { |
45 | ||
46 | // ------------------------------------------------------------------------ | |
47 | // Attributes | |
48 | // ------------------------------------------------------------------------ | |
49 | ||
50 | private static final String DIRECTORY = "testfiles"; | |
51 | private static final String TEST_STREAM1 = "O-Test-10K"; | |
52 | private static final String TEST_STREAM2 = "E-Test-10K"; | |
53 | private static final String EXPERIMENT = "MyExperiment"; | |
54 | private static int NB_EVENTS = 20000; | |
55 | private static int BLOCK_SIZE = 1000; | |
56 | ||
9e0640dc | 57 | private static ITmfTrace<TmfEvent>[] fTestTraces; |
0879b6b9 | 58 | private static TmfExperimentStub<TmfEvent> fExperiment; |
07671572 FC |
59 | |
60 | // ------------------------------------------------------------------------ | |
61 | // Housekeeping | |
62 | // ------------------------------------------------------------------------ | |
63 | ||
64 | public TmfExperimentCheckpointIndexTest(final String name) throws Exception { | |
65 | super(name); | |
66 | } | |
67 | ||
68 | @Override | |
0879b6b9 | 69 | protected synchronized void setUp() throws Exception { |
07671572 FC |
70 | super.setUp(); |
71 | setupTrace(DIRECTORY + File.separator + TEST_STREAM1, DIRECTORY + File.separator + TEST_STREAM2); | |
72 | if (fExperiment == null) { | |
0879b6b9 | 73 | fExperiment = new TmfExperimentStub<TmfEvent>(EXPERIMENT, fTestTraces, BLOCK_SIZE); |
9e0640dc | 74 | fExperiment.getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, true); |
07671572 FC |
75 | } |
76 | } | |
77 | ||
78 | @Override | |
79 | protected void tearDown() throws Exception { | |
80 | super.tearDown(); | |
81 | fExperiment.dispose(); | |
82 | fExperiment = null; | |
83 | } | |
84 | ||
9e0640dc | 85 | @SuppressWarnings("unchecked") |
0879b6b9 | 86 | private synchronized static ITmfTrace<?>[] setupTrace(final String path1, final String path2) { |
c7e1020d FC |
87 | if (fTestTraces == null) { |
88 | fTestTraces = new ITmfTrace[2]; | |
07671572 FC |
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); | |
c7e1020d | 93 | fTestTraces[0] = trace1; |
07671572 FC |
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); | |
c7e1020d | 97 | fTestTraces[1] = trace2; |
07671572 FC |
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 | } | |
c7e1020d | 106 | return fTestTraces; |
07671572 FC |
107 | } |
108 | ||
109 | // ------------------------------------------------------------------------ | |
110 | // Verify checkpoints | |
111 | // ------------------------------------------------------------------------ | |
112 | ||
113 | public void testTmfTraceIndexing() throws Exception { | |
114 | assertEquals("getCacheSize", BLOCK_SIZE, fExperiment.getCacheSize()); | |
115 | assertEquals("getTraceSize", NB_EVENTS, fExperiment.getNbEvents()); | |
116 | assertEquals("getRange-start", 1, fExperiment.getTimeRange().getStartTime().getValue()); | |
117 | assertEquals("getRange-end", NB_EVENTS, fExperiment.getTimeRange().getEndTime().getValue()); | |
118 | assertEquals("getStartTime", 1, fExperiment.getStartTime().getValue()); | |
119 | assertEquals("getEndTime", NB_EVENTS, fExperiment.getEndTime().getValue()); | |
120 | ||
121 | List<TmfCheckpoint> checkpoints = fExperiment.getIndexer().getCheckpoints(); | |
122 | int pageSize = fExperiment.getCacheSize(); | |
123 | assertTrue("Checkpoints exist", checkpoints != null); | |
124 | assertEquals("Checkpoints size", NB_EVENTS / BLOCK_SIZE, checkpoints.size()); | |
125 | ||
126 | // Validate that each checkpoint points to the right event | |
127 | for (int i = 0; i < checkpoints.size(); i++) { | |
128 | TmfCheckpoint checkpoint = checkpoints.get(i); | |
129 | TmfExperimentLocation expLocation = (TmfExperimentLocation) checkpoint.getLocation(); | |
130 | TmfLocationArray locations = expLocation.getLocation(); | |
131 | ITmfContext[] trcContexts = new ITmfContext[2]; | |
132 | trcContexts[0] = new TmfContext(locations.getLocations()[0], (i * pageSize) / 2); | |
133 | trcContexts[1] = new TmfContext(locations.getLocations()[1], (i * pageSize) / 2); | |
134 | TmfExperimentContext expContext = new TmfExperimentContext(trcContexts); | |
c7e1020d FC |
135 | expContext.getEvents()[0] = fTestTraces[0].getNext(fTestTraces[0].seekEvent((i * pageSize) / 2)); |
136 | expContext.getEvents()[1] = fTestTraces[1].getNext(fTestTraces[1].seekEvent((i * pageSize) / 2)); | |
07671572 FC |
137 | ITmfEvent event = fExperiment.parseEvent(expContext); |
138 | assertTrue(expContext.getRank() == i * pageSize); | |
139 | assertTrue((checkpoint.getTimestamp().compareTo(event.getTimestamp(), false) == 0)); | |
140 | } | |
141 | } | |
142 | ||
143 | } |