Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6c83b91bfa3ea0491e090bbc24d36527af966fab (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*******************************************************************************
 * Copyright (c) 2009, 2013 Ericsson
 *
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Francois Chouinard - Initial API and implementation
 *   Francois Chouinard - Adapted for TMF Trace Model 1.0
 *   Alexandre Montplaisir - Port to JUnit4
 *   Marc-Andre Laperle - Extracted to a common class from TmfCheckpointIndexTest
 *******************************************************************************/

package org.eclipse.linuxtools.tmf.core.tests.trace.indexer.checkpoint;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
import org.eclipse.linuxtools.tmf.core.tests.TmfCoreTestPlugin;
import org.eclipse.linuxtools.tmf.core.tests.shared.TmfTestTrace;
import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
import org.eclipse.linuxtools.tmf.core.trace.indexer.ITmfTraceIndexer;
import org.eclipse.linuxtools.tmf.core.trace.indexer.checkpoint.ITmfCheckpoint;
import org.eclipse.linuxtools.tmf.core.trace.indexer.checkpoint.ITmfCheckpointIndex;
import org.eclipse.linuxtools.tmf.core.trace.indexer.checkpoint.TmfCheckpointIndexer;
import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfEmptyTraceStub;
import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * Common code for index testing
 *
 * @author Marc-Andre Laperle
 */
public abstract class AbstractIndexTest {

    // ------------------------------------------------------------------------
    // Variables
    // ------------------------------------------------------------------------

    /**
     *
     */
    protected static final int BLOCK_SIZE = 100;
    private static final int NB_EVENTS = 10000;
    /**
     * The trace being tested
     */
    protected static TestTrace fTrace = null;
    private static EmptyTestTrace fEmptyTrace = null;

    // ------------------------------------------------------------------------
    // Housekeeping
    // ------------------------------------------------------------------------

    /**
     * Setup the test
     */
    @Before
    public void setUp() {
        setupTrace(getTracePath());
    }

    /**
     * Get the trace path
     *
     * @return the trace path
     */
    protected String getTracePath() {
        return TmfTestTrace.A_TEST_10K.getFullPath();
    }

    /**
     * Tear down the test
     */
    @After
    public void tearDown() {
        fTrace.dispose();
        fTrace = null;
        fEmptyTrace.dispose();
        fEmptyTrace = null;
    }

    interface TestIndexerInterface extends ITmfTraceIndexer {
        ITmfCheckpointIndex getCheckpoints();
    }

    // ------------------------------------------------------------------------
    // Helper classes
    // ------------------------------------------------------------------------

    /**
     * A test indexer
     */
    protected static class TestIndexer extends TmfCheckpointIndexer implements TestIndexerInterface {
        /**
         * Constructs the test indexer for a normal test trace
         *
         * @param testTrace
         *            the test trace
         */
        public TestIndexer(ITmfTrace testTrace) {
            super(testTrace, BLOCK_SIZE);
        }

        @Override
        public ITmfCheckpointIndex getCheckpoints() {
            return getTraceIndex();
        }
    }

    /**
     * Create the indexer for testing
     *
     * @param trace
     *            the trace
     * @return the indexer for testing
     */
    protected TestIndexerInterface createTestIndexer(TestTrace trace) {
        return new TestIndexer(trace);
    }

    /**
     * A test trace
     */
    protected class TestTrace extends TmfTraceStub {
        /**
         *
         * @param path
         *            the path
         * @param blockSize
         *            the block size
         * @throws TmfTraceException
         *             when error occurs
         */
        public TestTrace(String path, int blockSize) throws TmfTraceException {
            super(path, blockSize, false, null, null);
            setIndexer(createTestIndexer(this));
        }

        @Override
        public TestIndexerInterface getIndexer() {
            return (TestIndexerInterface) super.getIndexer();
        }
    }

    private class EmptyTestTrace extends TmfEmptyTraceStub {
        public EmptyTestTrace() {
            super();
            setIndexer(new TestIndexer(this));
        }

        @Override
        public TestIndexer getIndexer() {
            return (TestIndexer) super.getIndexer();
        }
    }

    // ------------------------------------------------------------------------
    // Helper functions
    // ------------------------------------------------------------------------

    /**
     * Creates the trace for the specified path
     *
     * @param path
     *            the path
     * @return the created trace
     * @throws URISyntaxException
     *             when error occurs
     * @throws IOException
     *             when error occurs
     * @throws TmfTraceException
     *             when error occurs
     */
    protected TestTrace createTrace(final String path) throws URISyntaxException, IOException, TmfTraceException {
        final URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(path), null);
        final File test = new File(FileLocator.toFileURL(location).toURI());
        TestTrace trace = new TestTrace(test.toURI().getPath(), BLOCK_SIZE);
        trace.indexTrace(true);
        return trace;
    }

    private synchronized void setupTrace(final String path) {
        if (fTrace == null) {
            try {
                fTrace = createTrace(path);
            } catch (final TmfTraceException e) {
                fail(e.getMessage());
            } catch (final URISyntaxException e) {
                fail(e.getMessage());
            } catch (final IOException e) {
                fail(e.getMessage());
            }
        }

        if (fEmptyTrace == null) {
            fEmptyTrace = new EmptyTestTrace();
            fEmptyTrace.indexTrace(true);
        }
    }

    // ------------------------------------------------------------------------
    // Verify checkpoints
    // ------------------------------------------------------------------------

    /**
     * Test the content of the index after building the full index
     */
    @Test
    public void testTmfTraceIndexing() {
        verifyIndexContent();
    }

    /**
     * Verify the content of the index
     */
    protected static void verifyIndexContent() {
        assertEquals(BLOCK_SIZE, fTrace.getCacheSize());
        assertEquals(NB_EVENTS, fTrace.getNbEvents());
        assertEquals(1, fTrace.getTimeRange().getStartTime().getValue());
        assertEquals(NB_EVENTS, fTrace.getTimeRange().getEndTime().getValue());
        assertEquals(1, fTrace.getStartTime().getValue());
        assertEquals(NB_EVENTS, fTrace.getEndTime().getValue());

        ITmfCheckpointIndex checkpoints = fTrace.getIndexer().getCheckpoints();
        int pageSize = fTrace.getCacheSize();
        assertTrue(checkpoints != null);
        assertEquals(NB_EVENTS / BLOCK_SIZE, checkpoints.size());

        // Validate that each checkpoint points to the right event
        for (int i = 0; i < checkpoints.size(); i++) {
            ITmfCheckpoint checkpoint = checkpoints.get(i);
            TmfContext context = new TmfContext(checkpoint.getLocation(), i * pageSize);
            ITmfEvent event = fTrace.parseEvent(context);
            assertEquals(context.getRank(), i * pageSize);
            assertTrue((checkpoint.getTimestamp().compareTo(event.getTimestamp(), false) == 0));
        }
    }

    /**
     * Test that a empty trace has the correct content
     */
    @Test
    public void testEmptyTmfTraceIndexing() {
        assertEquals(ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, fEmptyTrace.getCacheSize());
        assertEquals(0, fEmptyTrace.getNbEvents());
        assertEquals(TmfTimestamp.BIG_BANG, fEmptyTrace.getTimeRange().getStartTime());
        assertEquals(TmfTimestamp.BIG_BANG, fEmptyTrace.getTimeRange().getEndTime());
        assertEquals(TmfTimestamp.BIG_BANG, fEmptyTrace.getStartTime());
        assertEquals(TmfTimestamp.BIG_BANG, fEmptyTrace.getEndTime());

        ITmfCheckpointIndex checkpoints = fEmptyTrace.getIndexer().getCheckpoints();
        assertTrue(checkpoints != null);
        assertEquals(0, checkpoints.size());
    }
}

Back to the top