1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
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 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.tmf
.ctf
.core
.tests
.trace
.indexer
;
12 import static org
.junit
.Assert
.assertEquals
;
13 import static org
.junit
.Assert
.assertFalse
;
14 import static org
.junit
.Assert
.assertTrue
;
18 import org
.eclipse
.jdt
.annotation
.NonNull
;
19 import org
.eclipse
.tracecompass
.testtraces
.ctf
.CtfTestTrace
;
20 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
21 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
22 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
23 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfContext
;
24 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
25 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.experiment
.TmfExperiment
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.TmfBTreeTraceIndexer
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.checkpoint
.ITmfCheckpoint
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.checkpoint
.ITmfCheckpointIndex
;
30 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.tests
.shared
.CtfTmfTestTraceUtils
;
31 import org
.junit
.After
;
32 import org
.junit
.Before
;
33 import org
.junit
.Test
;
36 * Test suite for indexing CTF experiments.
38 public class CtfExperimentCheckpointIndexTest
{
40 private static final String EXPERIMENT
= "MyExperiment";
41 private static final @NonNull CtfTestTrace TEST_TRACE1
= CtfTestTrace
.TRACE2
;
42 private static final @NonNull CtfTestTrace TEST_TRACE2
= CtfTestTrace
.KERNEL_VM
;
43 private static final int NB_EVENTS
= CtfTestTrace
.TRACE2
.getNbEvents() + CtfTestTrace
.KERNEL_VM
.getNbEvents();
45 private static final long START_TIME
= 1331668247314038062L;
46 private static final long END_TIME
= 1363700770550261288L;
47 private static final int BLOCK_SIZE
= 50000;
48 private static final int LAST_EVENT_RANK
= NB_EVENTS
- 1;
49 private static final int LAST_CHECKPOINT_RANK
= LAST_EVENT_RANK
/ BLOCK_SIZE
;
50 private static final int NB_CHECKPOINTS
= LAST_CHECKPOINT_RANK
+ 1;
52 private static ITmfTrace
[] fTestTraces
;
53 private static TmfExperiment fExperiment
;
54 private static TestIndexer fIndexer
;
61 deleteSupplementaryFiles();
65 private static void setUpTraces() {
66 fTestTraces
= new ITmfTrace
[2];
67 fTestTraces
[0] = CtfTmfTestTraceUtils
.getTrace(TEST_TRACE1
);
68 fTestTraces
[1] = CtfTmfTestTraceUtils
.getTrace(TEST_TRACE2
);
69 fExperiment
= new TmfExperiment(ITmfEvent
.class, EXPERIMENT
, fTestTraces
, BLOCK_SIZE
, null) {
71 protected ITmfTraceIndexer
createIndexer(int interval
) {
72 fIndexer
= new TestIndexer(this, interval
);
76 fExperiment
.indexTrace(true);
83 public void tearDown() {
84 deleteSupplementaryFiles();
88 private static void deleteSupplementaryFiles() {
89 final String TRACE_DIRECTORY
= TmfTraceManager
.getTemporaryDirPath() + File
.separator
+ EXPERIMENT
;
90 File supplementaryFileDir
= new File(TRACE_DIRECTORY
);
91 if (supplementaryFileDir
.exists()) {
92 for (File file
: supplementaryFileDir
.listFiles()) {
98 private static void disposeTraces() {
99 fExperiment
.dispose();
101 for (ITmfTrace trace
: fTestTraces
) {
108 * Test indexer to give access to checkpoints
110 private static class TestIndexer
extends TmfBTreeTraceIndexer
{
112 public TestIndexer(ITmfTrace trace
, int interval
) {
113 super(trace
, interval
);
116 public ITmfCheckpointIndex
getCheckpoints() {
117 return getTraceIndex();
122 * Test the content of the index after building the full index
125 public void testIndexing() {
126 assertTrue(fIndexer
.getCheckpoints().isCreatedFromScratch());
127 verifyIndexContent();
131 * Test that a fully built index has the same content when reloaded from disk
134 public void testReopenIndex() {
135 assertTrue(fIndexer
.getCheckpoints().isCreatedFromScratch());
138 assertFalse(fIndexer
.getCheckpoints().isCreatedFromScratch());
139 verifyIndexContent();
142 private static void verifyIndexContent() {
143 assertEquals("getTraceSize", NB_EVENTS
, fExperiment
.getNbEvents());
144 assertEquals("getRange-start", START_TIME
, fExperiment
.getTimeRange().getStartTime().getValue());
145 assertEquals("getRange-end", END_TIME
, fExperiment
.getTimeRange().getEndTime().getValue());
146 assertEquals("getStartTime", START_TIME
, fExperiment
.getStartTime().getValue());
147 assertEquals("getEndTime", END_TIME
, fExperiment
.getEndTime().getValue());
149 ITmfCheckpointIndex checkpoints
= fIndexer
.getCheckpoints();
150 assertTrue(checkpoints
!= null);
151 assertEquals(NB_EVENTS
, checkpoints
.getNbEvents());
152 assertEquals(NB_CHECKPOINTS
, checkpoints
.size());
154 // Validate that each checkpoint points to the right event
155 for (int i
= 0; i
< checkpoints
.size(); i
++) {
156 ITmfCheckpoint checkpoint
= checkpoints
.get(i
);
157 TmfContext context
= new TmfContext(checkpoint
.getLocation(), i
* BLOCK_SIZE
);
158 ITmfEvent event
= fExperiment
.parseEvent(context
);
159 assertEquals(context
.getRank(), i
* BLOCK_SIZE
);
160 assertEquals(0, (checkpoint
.getTimestamp().compareTo(event
.getTimestamp())));
163 ITmfContext context
= fExperiment
.seekEvent(0);
164 ITmfEvent event
= fExperiment
.getNext(context
);
165 assertEquals(START_TIME
, event
.getTimestamp().getValue());
167 context
= fExperiment
.seekEvent(NB_EVENTS
- 1);
168 event
= fExperiment
.getNext(context
);
169 assertEquals(END_TIME
, event
.getTimestamp().getValue());