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