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