June 29th, 2010
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.tests / src / org / eclipse / linuxtools / lttng / LttngTestPreparation.java
1 package org.eclipse.linuxtools.lttng;
2
3 import java.io.File;
4 import java.net.URL;
5
6 import junit.framework.TestCase;
7
8 import org.eclipse.core.runtime.FileLocator;
9 import org.eclipse.core.runtime.Path;
10 import org.eclipse.linuxtools.lttng.control.LttngCoreProviderFactory;
11 import org.eclipse.linuxtools.lttng.event.LttngEvent;
12 import org.eclipse.linuxtools.lttng.event.LttngSyntheticEvent;
13 import org.eclipse.linuxtools.lttng.event.LttngSyntheticEvent.SequenceInd;
14 import org.eclipse.linuxtools.lttng.event.LttngTimestamp;
15 import org.eclipse.linuxtools.lttng.state.experiment.IStateExperimentManager;
16 import org.eclipse.linuxtools.lttng.state.experiment.StateManagerFactory;
17 import org.eclipse.linuxtools.lttng.tests.LTTngCoreTestPlugin;
18 import org.eclipse.linuxtools.lttng.trace.LTTngTextTrace;
19 import org.eclipse.linuxtools.lttng.trace.LTTngTrace;
20 import org.eclipse.linuxtools.tmf.event.TmfTimeRange;
21 import org.eclipse.linuxtools.tmf.experiment.TmfExperiment;
22 import org.eclipse.linuxtools.tmf.request.TmfDataRequest;
23 import org.eclipse.linuxtools.tmf.request.TmfEventRequest;
24 import org.eclipse.linuxtools.tmf.signal.TmfExperimentSelectedSignal;
25 import org.eclipse.linuxtools.tmf.trace.ITmfTrace;
26
27 public abstract class LttngTestPreparation extends TestCase {
28 // ========================================================================
29 // Data
30 // ========================================================================
31 private final static String ftracepath_T1 = "traceset/trace-15316events_nolost_newformat";
32 final static String fTextTracepath_T1 = "traceset/trace-15316events_nolost_newformat.txt";
33
34 private static final Long CHECK_POINT_INTERVAL = 1000L;
35
36 final Long[] expectedEvents_T1 = new Long[20];
37 final Long[] requestIntervals_T1 = new Long[32];
38
39 static LTTngTextTrace ftextStream_T1 = null;
40 private static LTTngTrace frealStream = null;
41
42 private TmfExperiment<LttngEvent> fTestExperiment = null;
43 protected volatile int feventCount = 0;
44 protected boolean validSequence = true;
45
46 public LttngTestPreparation() {
47 super();
48 init();
49 }
50
51 public LttngTestPreparation(String name) {
52 super(name);
53 init();
54 }
55
56 protected void init() {
57 fillInRequestIntervals();
58 fillInExpectedEvents();
59 feventCount = 0;
60 }
61
62 /**
63 * @return
64 */
65 protected TmfExperiment<LttngEvent> prepareExperimentToTest() {
66 if (fTestExperiment == null) {
67 String expId = "testExperiment";
68 int nbTraces = 1;
69
70 // Define traces in experiment
71 ITmfTrace[] traces = new ITmfTrace[nbTraces];
72 ITmfTrace trace = prepareStreamToTest();
73 traces[0] = trace;
74
75 // create experiment and associate traces
76 fTestExperiment = new TmfExperiment<LttngEvent>(LttngEvent.class,
77 expId, traces);
78 // fTestExperiment.indexExperiment(waitForCompletion);
79
80 // Set the current selected experiment as the test experiment
81 TmfExperimentSelectedSignal<LttngEvent> signal = new TmfExperimentSelectedSignal<LttngEvent>(
82 this, fTestExperiment);
83 fTestExperiment.experimentSelected(signal);
84 }
85
86 return fTestExperiment;
87 }
88
89 /**
90 * @return
91 */
92 protected TmfExperiment<LttngEvent> prepareTextExperimentToTest() {
93 if (fTestExperiment == null) {
94 String expId = "testExperiment";
95 int nbTraces = 1;
96
97 // Define traces in experiment
98 ITmfTrace[] traces = new ITmfTrace[nbTraces];
99 ITmfTrace trace = prepareTextStreamToTest();
100 traces[0] = trace;
101
102 // create experiment and associate traces
103 fTestExperiment = new TmfExperiment<LttngEvent>(LttngEvent.class,
104 expId, traces);
105
106 // Set the current selected experiment as the test experiment
107 TmfExperimentSelectedSignal<LttngEvent> signal = new TmfExperimentSelectedSignal<LttngEvent>(
108 this, fTestExperiment);
109 fTestExperiment.experimentSelected(signal);
110
111 }
112
113 return fTestExperiment;
114 }
115
116 protected LTTngTrace prepareStreamToTest() {
117 if (frealStream == null) {
118 try {
119 URL location = FileLocator.find(LTTngCoreTestPlugin.getPlugin().getBundle(), new Path(ftracepath_T1),
120 null);
121 File testfile = new File(FileLocator.toFileURL(location).toURI());
122 LTTngTrace tmpStream = new LTTngTrace(testfile.getPath());
123 frealStream = tmpStream;
124 } catch (Exception e) {
125 System.out.println("ERROR : Could not open " + ftracepath_T1);
126 frealStream = null;
127 }
128 } else {
129 frealStream.seekEvent(0L);
130 }
131
132 return frealStream;
133 }
134
135 protected LTTngTextTrace prepareTextStreamToTest() {
136 if (ftextStream_T1 == null) {
137 try {
138 URL location = FileLocator.find(LTTngCoreTestPlugin.getPlugin().getBundle(),
139 new Path(fTextTracepath_T1), null);
140 File testfile = new File(FileLocator.toFileURL(location).toURI());
141 LTTngTextTrace tmpStream = new LTTngTextTrace(testfile.getPath());
142 ftextStream_T1 = tmpStream;
143
144 } catch (Exception e) {
145 System.out.println("ERROR : Could not open " + fTextTracepath_T1);
146 ftextStream_T1 = null;
147 }
148 } else {
149 ftextStream_T1.seekEvent(0);
150 }
151
152 return ftextStream_T1;
153 }
154
155 protected IStateExperimentManager prepareExperimentContext(
156 boolean waitForRequest) {
157 // Create a new Experiment manager
158 IStateExperimentManager expManager = StateManagerFactory
159 .getExperimentManager();
160 // Configure the interval to create check points so this can be tested
161 // with medium size files i.e. default is 50000 events
162 StateManagerFactory.setTraceCheckPointInterval(CHECK_POINT_INTERVAL);
163
164 // Lets wait for the request completion to analyse the results
165 LttngCoreProviderFactory.getEventProvider()
166 .setWaitForRequest(waitForRequest);
167 return expManager;
168 }
169
170 /**
171 * @param <T>
172 * @param k
173 * @param startIdx
174 * , > 0 and between 0 - 31
175 * @param endIdx
176 * , > startIdx and between 0 - 31
177 * @return
178 */
179 protected <T extends LttngEvent> TmfEventRequest<T> prepareEventRequest(Class<T> k, int startIdx, int endIdx) {
180 return prepareEventRequest(k, startIdx, endIdx, true);
181 }
182
183 /**
184 * @param <T>
185 * @param k
186 * @param startIdx
187 * , > 0 and between 0 - 31
188 * @param endIdx
189 * , > startIdx and between 0 - 31
190 * @param printFirst20
191 * , print the first expected events vs actual events
192 * @return
193 */
194 protected <T extends LttngEvent> TmfEventRequest<T> prepareEventRequest(
195 Class<T> k, int startIdx, int endIdx, final boolean printFirst20) {
196 // verify bounds
197 if (!(endIdx > startIdx && startIdx >= 0 && endIdx <= 31)) {
198 TraceDebug.debug("Event request indexes out of bounds");
199 return null;
200 }
201
202 int DEFAULT_CHUNK = 1;
203
204 // time range
205 TmfTimeRange trange = new TmfTimeRange(new LttngTimestamp(
206 requestIntervals_T1[startIdx]), new LttngTimestamp(
207 requestIntervals_T1[endIdx]));
208
209 // request
210 validSequence = true;
211 TmfEventRequest<T> request = new TmfEventRequest<T>(k,
212 trange, TmfDataRequest.ALL_DATA, DEFAULT_CHUNK) {
213
214 @Override
215 public void handleData() {
216 T[] result = getData();
217
218 T event = (result.length > 0) ? result[0] : null;
219 if (event == null) {
220 System.out
221 .println("Syntheric Event Received is null, after event: "
222 + feventCount);
223 return;
224 }
225
226 // Listen to only one variant of synthetic event to keep
227 // track of
228 if (event instanceof LttngSyntheticEvent) {
229 if (((LttngSyntheticEvent) event).getSynType() != SequenceInd.BEFORE) {
230 return;
231 }
232 }
233
234 // Validating the orders of the first 20 events
235 if (printFirst20 && feventCount < 20) {
236 long timevalue = event.getTimestamp().getValue();
237 if (timevalue != expectedEvents_T1[feventCount]) {
238 validSequence = false;
239 System.out.println("Expected Event: "
240 + expectedEvents_T1[feventCount] + " actual: "
241 + event.getTimestamp().getValue());
242 } else {
243 System.out.println("Synthetic Event: " + feventCount
244 + " matched expected time");
245 }
246 }
247
248 // increment count
249 incrementCount();
250 }
251
252 /**
253 * possibly increased by multiple request threads
254 */
255 private synchronized void incrementCount() {
256 feventCount++;
257 }
258
259 @Override
260 public void handleCompleted() {
261 // if (isCancelled() || isFailed()) {
262 // // No notification to end request handlers
263 // } else {
264 // // notify the associated end request handlers
265 // requestCompleted();
266 // }
267
268 System.out.println("Number of events processed x: "
269 + feventCount);
270 }
271
272 };
273 return request;
274 }
275
276 /**
277 * Validation points
278 */
279 private void fillInExpectedEvents() {
280 expectedEvents_T1[0] = 13589759412128L;
281 expectedEvents_T1[1] = 13589759419903L;
282 expectedEvents_T1[2] = 13589759422785L;
283 expectedEvents_T1[3] = 13589759425598L;
284 expectedEvents_T1[4] = 13589759430979L;
285 expectedEvents_T1[5] = 13589759433694L;
286 expectedEvents_T1[6] = 13589759436212L;
287 expectedEvents_T1[7] = 13589759438797L;
288 expectedEvents_T1[8] = 13589759441253L;
289 expectedEvents_T1[9] = 13589759444795L;
290 expectedEvents_T1[10] = 13589759447800L;
291 expectedEvents_T1[11] = 13589759450836L;
292 expectedEvents_T1[12] = 13589759453835L;
293 expectedEvents_T1[13] = 13589759459351L;
294 expectedEvents_T1[14] = 13589759464411L;
295 expectedEvents_T1[15] = 13589759467021L;
296 expectedEvents_T1[16] = 13589759469668L;
297 expectedEvents_T1[17] = 13589759474938L;
298 expectedEvents_T1[18] = 13589759477536L;
299 expectedEvents_T1[19] = 13589759480485L;
300 }
301
302 /**
303 * Intervals for trace 1, separated %500 + last event
304 */
305 private void fillInRequestIntervals() {
306 requestIntervals_T1[0] = 13589759412128L; /* check point expected */
307 requestIntervals_T1[1] = 13589763490945L; /* between check points */
308 requestIntervals_T1[2] = 13589778265041L; /* check point expected */
309 requestIntervals_T1[3] = 13589783143445L; /* between check points */
310 requestIntervals_T1[4] = 13589786300104L; /* check point expected */
311 requestIntervals_T1[5] = 13589790722564L; /* between check points */
312 requestIntervals_T1[6] = 13589796139823L; /* check point expected */
313 requestIntervals_T1[7] = 13589800400562L; /* between check points */
314 requestIntervals_T1[8] = 13589801594374L; /* check point expected */
315 requestIntervals_T1[9] = 13589802750295L; /* between check points */
316 requestIntervals_T1[10] = 13589804071157L; /* check point expected */
317 requestIntervals_T1[11] = 13589810124488L; /* between check points */
318 requestIntervals_T1[12] = 13589822493183L; /* check point expected */
319 requestIntervals_T1[13] = 13589824131273L; /* between check points */
320 requestIntervals_T1[14] = 13589825398284L; /* check point expected */
321 requestIntervals_T1[15] = 13589826664185L; /* between check points */
322 requestIntervals_T1[16] = 13589827811998L; /* check point expected */
323 requestIntervals_T1[17] = 13589828915763L; /* between check points */
324 requestIntervals_T1[18] = 13589830074220L; /* check point expected */
325 requestIntervals_T1[19] = 13589831232050L; /* between check points */
326 requestIntervals_T1[20] = 13589832394049L; /* check point expected */
327 requestIntervals_T1[21] = 13589833852883L; /* between check points */
328 requestIntervals_T1[22] = 13589839626892L; /* check point expected */
329 requestIntervals_T1[23] = 13589849509798L; /* between check points */
330 requestIntervals_T1[24] = 13589850728538L; /* check point expected */
331 requestIntervals_T1[25] = 13589851889230L; /* between check points */
332 requestIntervals_T1[26] = 13589853294800L; /* check point expected */
333 requestIntervals_T1[27] = 13589859414998L; /* between check points */
334 requestIntervals_T1[28] = 13589878046089L; /* check point expected */
335 requestIntervals_T1[29] = 13589886468603L; /* between check points */
336 requestIntervals_T1[30] = 13589902256918L; /* check point expected */
337 requestIntervals_T1[31] = 13589906758692L; /* last event in T1 */
338 }
339
340 }
This page took 0.038196 seconds and 5 git commands to generate.