Commit | Line | Data |
---|---|---|
032ecd45 | 1 | /******************************************************************************* |
13c60668 | 2 | * Copyright (c) 2013, 2015 Ericsson |
032ecd45 MAL |
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 | * Marc-Andre Laperle - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
2bdf0193 | 13 | package org.eclipse.tracecompass.tmf.ui.tests.trace; |
032ecd45 MAL |
14 | |
15 | import static org.junit.Assert.assertEquals; | |
16 | import static org.junit.Assert.assertFalse; | |
17 | import static org.junit.Assert.assertTrue; | |
18 | ||
19 | import java.io.File; | |
20 | ||
2bdf0193 AM |
21 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; |
22 | import org.eclipse.tracecompass.tmf.core.trace.ITmfEventParser; | |
23 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; | |
24 | import org.eclipse.tracecompass.tmf.core.trace.TmfContext; | |
25 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager; | |
26 | import org.eclipse.tracecompass.tmf.core.trace.indexer.TmfBTreeTraceIndexer; | |
27 | import org.eclipse.tracecompass.tmf.core.trace.indexer.checkpoint.ITmfCheckpoint; | |
28 | import org.eclipse.tracecompass.tmf.core.trace.indexer.checkpoint.ITmfCheckpointIndex; | |
032ecd45 MAL |
29 | import org.junit.After; |
30 | import org.junit.Before; | |
31 | import org.junit.Test; | |
32 | ||
33 | /** | |
34 | * Common test code for custom trace indexes | |
35 | * | |
36 | * @author Marc-Andre Laperle | |
37 | */ | |
38 | public abstract class AbstractCustomTraceIndexTest { | |
39 | ||
40 | /** | |
41 | * Time format use for event creation | |
42 | */ | |
43 | protected static final String TIMESTAMP_FORMAT = "dd/MM/yyyy HH:mm:ss:SSS"; | |
44 | /** | |
45 | * Block size used for the indexer | |
46 | */ | |
47 | protected static final int BLOCK_SIZE = 100; | |
48 | /** | |
49 | * The total number of events in the generated trace | |
50 | */ | |
51 | protected static final int NB_EVENTS = 10000; | |
13c60668 | 52 | private static final long MILLISECOND_TO_NANOSECOND = 1000000; |
032ecd45 MAL |
53 | private TestTrace fTrace = null; |
54 | ||
55 | /** | |
56 | * A common test indexer for custom trace index tests | |
57 | */ | |
58 | protected static class TestIndexer extends TmfBTreeTraceIndexer { | |
59 | ||
60 | /** | |
61 | * Constructs a new test indexer | |
62 | * | |
63 | * @param trace the trace | |
64 | * @param interval the checkpoint interval | |
65 | */ | |
66 | public TestIndexer(ITmfTrace trace, int interval) { | |
67 | super(trace, interval); | |
68 | } | |
69 | ||
70 | /** | |
71 | * Get the index | |
72 | * | |
73 | * @return the index | |
74 | */ | |
75 | public ITmfCheckpointIndex getCheckpoints() { | |
76 | return getTraceIndex(); | |
77 | } | |
78 | } | |
79 | ||
80 | interface TestTrace extends ITmfTrace { | |
81 | TestIndexer getIndexer(); | |
82 | } | |
83 | ||
84 | /** | |
85 | * Setup the test | |
86 | * | |
87 | * @throws Exception when error occurs | |
88 | */ | |
89 | @Before | |
90 | public void setUp() throws Exception { | |
91 | setupTrace(); | |
92 | } | |
93 | ||
94 | private synchronized void setupTrace() throws Exception { | |
95 | File traceDirectory = new File(getTraceDirectory()); | |
96 | if (traceDirectory.exists()) { | |
97 | traceDirectory.delete(); | |
98 | } | |
99 | traceDirectory.mkdir(); | |
100 | if (fTrace == null) { | |
101 | fTrace = createTrace(); | |
102 | fTrace.indexTrace(true); | |
103 | } | |
104 | } | |
105 | ||
106 | /** | |
107 | * Create a test trace, varies between tests | |
108 | * | |
109 | * @return the test trace | |
110 | * @throws Exception when error occurs | |
111 | */ | |
112 | abstract protected TestTrace createTrace() throws Exception; | |
113 | /** | |
114 | * Return the trace directory for the generated trace | |
115 | * | |
116 | * @return the trace directory for the generated trace | |
117 | */ | |
118 | abstract protected String getTraceDirectory(); | |
119 | ||
120 | /** | |
121 | * Tear down the test | |
122 | */ | |
123 | @After | |
124 | public void tearDown() { | |
125 | String directory = TmfTraceManager.getSupplementaryFileDir(fTrace); | |
126 | try { | |
127 | fTrace.dispose(); | |
128 | fTrace = null; | |
129 | } finally { | |
130 | File dir = new File(directory); | |
131 | if (dir.exists()) { | |
132 | File[] files = dir.listFiles(); | |
133 | for (File file : files) { | |
134 | file.delete(); | |
135 | } | |
136 | dir.delete(); | |
137 | } | |
138 | ||
139 | File trace = new File(getTraceDirectory()); | |
140 | if (trace.exists()) { | |
141 | trace.delete(); | |
142 | } | |
143 | } | |
144 | ||
145 | } | |
146 | ||
147 | /** | |
148 | * Test the content of the index after building the full index | |
149 | */ | |
150 | @Test | |
151 | public void testTmfTraceIndexing() { | |
152 | verifyIndexContent(); | |
153 | } | |
154 | ||
155 | private void verifyIndexContent() { | |
13c60668 | 156 | long endTime = (NB_EVENTS - 1) * MILLISECOND_TO_NANOSECOND; |
032ecd45 MAL |
157 | assertEquals("getCacheSize", BLOCK_SIZE, fTrace.getCacheSize()); |
158 | assertEquals("getTraceSize", NB_EVENTS, fTrace.getNbEvents()); | |
159 | assertEquals("getRange-start", 0, fTrace.getTimeRange().getStartTime().getValue()); | |
13c60668 | 160 | assertEquals("getRange-end", endTime, fTrace.getTimeRange().getEndTime().getValue()); |
032ecd45 | 161 | assertEquals("getStartTime", 0, fTrace.getStartTime().getValue()); |
13c60668 | 162 | assertEquals("getEndTime", endTime, fTrace.getEndTime().getValue()); |
032ecd45 MAL |
163 | |
164 | ITmfCheckpointIndex checkpoints = fTrace.getIndexer().getCheckpoints(); | |
165 | int pageSize = fTrace.getCacheSize(); | |
166 | assertTrue("Checkpoints exist", checkpoints != null); | |
167 | assertEquals("Checkpoints size", NB_EVENTS / BLOCK_SIZE, checkpoints.size()); | |
168 | ||
169 | // Validate that each checkpoint points to the right event | |
170 | for (int i = 0; i < checkpoints.size(); i++) { | |
171 | ITmfCheckpoint checkpoint = checkpoints.get(i); | |
172 | TmfContext context = new TmfContext(checkpoint.getLocation(), i * pageSize); | |
173 | ITmfEvent event = ((ITmfEventParser)fTrace).parseEvent(context); | |
174 | assertTrue(context.getRank() == i * pageSize); | |
065cc19b | 175 | assertTrue((checkpoint.getTimestamp().compareTo(event.getTimestamp()) == 0)); |
032ecd45 MAL |
176 | } |
177 | } | |
178 | ||
179 | /** | |
180 | * Test that a fully built index has the same content when reloaded from disk | |
181 | * | |
182 | * @throws Exception when error occurs | |
183 | */ | |
184 | @Test | |
185 | public void testReopenIndex() throws Exception { | |
186 | fTrace.dispose(); | |
187 | fTrace = createTrace(); | |
188 | assertFalse(fTrace.getIndexer().getCheckpoints().isCreatedFromScratch()); | |
189 | fTrace.indexTrace(true); | |
190 | ||
191 | verifyIndexContent(); | |
192 | } | |
193 | } |