2010-06-05 fchouinard@gmail.com Contributions for bugs 292965, 292963, 293102,...
[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 protected LTTngTextTrace prepareTextStreamToTest() {
63 if (ftextStream_T1 == null) {
64 try {
65 URL location = FileLocator.find(LTTngCoreTestPlugin.getPlugin()
66 .getBundle(), new Path(fTextTracepath_T1), null);
67 File testfile = new File(FileLocator.toFileURL(location)
68 .toURI());
69 LTTngTextTrace tmpStream = new LTTngTextTrace(testfile
70 .getPath());
71 ftextStream_T1 = tmpStream;
72
73 } catch (Exception e) {
74 System.out.println("ERROR : Could not open " + fTextTracepath_T1);
75 ftextStream_T1 = null;
76 }
77 } else {
78 ftextStream_T1.seekEvent(0);
79 }
80
81 return ftextStream_T1;
82 }
83
84 /**
85 * @return
86 */
87 protected TmfExperiment<LttngEvent> prepareTextExperimentToTest() {
88 if (fTestExperiment == null) {
89 String expId = "testExperiment";
90 int nbTraces = 1;
91
92 // Define traces in experiment
93 ITmfTrace[] traces = new ITmfTrace[nbTraces];
94 ITmfTrace trace = prepareTextStreamToTest();
95 traces[0] = trace;
96
97 // create experiment and associate traces
98 fTestExperiment = new TmfExperiment<LttngEvent>(LttngEvent.class,
99 expId, traces);
100
101 // Set the current selected experiment as the test experiment
102 TmfExperimentSelectedSignal<LttngEvent> signal = new TmfExperimentSelectedSignal<LttngEvent>(
103 this, fTestExperiment);
104 fTestExperiment.experimentSelected(signal);
105
106 }
107
108 return fTestExperiment;
109 }
110
111 protected LTTngTrace prepareStreamToTest() {
112 if (frealStream == null) {
113 try {
114 URL location = FileLocator.find(LTTngCoreTestPlugin.getPlugin()
115 .getBundle(), new Path(ftracepath_T1), null);
116 File testfile = new File(FileLocator.toFileURL(location)
117 .toURI());
118 LTTngTrace tmpStream = new LTTngTrace(testfile.getPath());
119 frealStream = tmpStream;
120 } catch (Exception e) {
121 System.out.println("ERROR : Could not open " + ftracepath_T1);
122 frealStream = null;
123 }
124 } else {
125 frealStream.seekEvent(0L);
126 }
127
128 return frealStream;
129 }
130
131 /**
132 * @return
133 */
134 protected TmfExperiment<LttngEvent> prepareExperimentToTest() {
135 if (fTestExperiment == null) {
136 String expId = "testExperiment";
137 int nbTraces = 1;
138
139 // Define traces in experiment
140 ITmfTrace[] traces = new ITmfTrace[nbTraces];
141 ITmfTrace trace = prepareStreamToTest();
142 traces[0] = trace;
143
144 // create experiment and associate traces
145 fTestExperiment = new TmfExperiment<LttngEvent>(LttngEvent.class,
146 expId, traces);
147 // fTestExperiment.indexExperiment(waitForCompletion);
148
149 // Set the current selected experiment as the test experiment
150 TmfExperimentSelectedSignal<LttngEvent> signal = new TmfExperimentSelectedSignal<LttngEvent>(
151 this, fTestExperiment);
152 fTestExperiment.experimentSelected(signal);
153 }
154
155 return fTestExperiment;
156 }
157
158 protected IStateExperimentManager prepareExperimentContext(
159 boolean waitForRequest) {
160 // Create a new Experiment manager
161 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()
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(Class<T> k, int startIdx, 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 Class<T> k, int startIdx, 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 int DEFAULT_CHUNK = 1;
206
207 // time range
208 TmfTimeRange trange = new TmfTimeRange(new LttngTimestamp(
209 requestIntervals_T1[startIdx]), new LttngTimestamp(
210 requestIntervals_T1[endIdx]));
211
212 // request
213 validSequence = true;
214 TmfEventRequest<T> request = new TmfEventRequest<T>(k,
215 trange, TmfDataRequest.ALL_DATA, DEFAULT_CHUNK) {
216
217 @Override
218 public void handleData() {
219 T[] result = getData();
220
221 T event = (result.length > 0) ? result[0] : null;
222 if (event == null) {
223 System.out
224 .println("Syntheric Event Received is null, after event: "
225 + feventCount);
226 return;
227 }
228
229 // Listen to only one variant of synthetic event to keep
230 // track of
231 if (event instanceof LttngSyntheticEvent) {
232 if (((LttngSyntheticEvent) event).getSynType() != SequenceInd.BEFORE) {
233 return;
234 }
235 }
236
237 // Validating the orders of the first 20 events
238 if (printFirst20 && feventCount < 20) {
239 long timevalue = event.getTimestamp().getValue();
240 if (timevalue != expectedEvents_T1[feventCount]) {
241 validSequence = false;
242 System.out.println("Expected Event: "
243 + expectedEvents_T1[feventCount] + " actual: "
244 + event.getTimestamp().getValue());
245 } else {
246 System.out.println("Synthetic Event: " + feventCount
247 + " matched expected time");
248 }
249 }
250
251 // increment count
252 incrementCount();
253 }
254
255 /**
256 * possibly increased by multiple request threads
257 */
258 private synchronized void incrementCount() {
259 feventCount++;
260 }
261
262 @Override
263 public void handleCompleted() {
264 // if (isCancelled() || isFailed()) {
265 // // No notification to end request handlers
266 // } else {
267 // // notify the associated end request handlers
268 // requestCompleted();
269 // }
270
271 System.out.println("Number of events processed x: "
272 + feventCount);
273 }
274
275 };
276 return request;
277 }
278
279 /**
280 * Validation points
281 */
282 private void fillInExpectedEvents() {
283 expectedEvents_T1[0] = 13589759412128L;
284 expectedEvents_T1[1] = 13589759419903L;
285 expectedEvents_T1[2] = 13589759422785L;
286 expectedEvents_T1[3] = 13589759425598L;
287 expectedEvents_T1[4] = 13589759430979L;
288 expectedEvents_T1[5] = 13589759433694L;
289 expectedEvents_T1[6] = 13589759436212L;
290 expectedEvents_T1[7] = 13589759438797L;
291 expectedEvents_T1[8] = 13589759441253L;
292 expectedEvents_T1[9] = 13589759444795L;
293 expectedEvents_T1[10] = 13589759447800L;
294 expectedEvents_T1[11] = 13589759450836L;
295 expectedEvents_T1[12] = 13589759453835L;
296 expectedEvents_T1[13] = 13589759459351L;
297 expectedEvents_T1[14] = 13589759464411L;
298 expectedEvents_T1[15] = 13589759467021L;
299 expectedEvents_T1[16] = 13589759469668L;
300 expectedEvents_T1[17] = 13589759474938L;
301 expectedEvents_T1[18] = 13589759477536L;
302 expectedEvents_T1[19] = 13589759480485L;
303 }
304
305 /**
306 * Intervals for trace 1, separated %500 + last event
307 */
308 private void fillInRequestIntervals() {
309 requestIntervals_T1[0] = 13589759412128L; /* check point expected */
310 requestIntervals_T1[1] = 13589763490945L; /* between check points */
311 requestIntervals_T1[2] = 13589778265041L; /* check point expected */
312 requestIntervals_T1[3] = 13589783143445L; /* between check points */
313 requestIntervals_T1[4] = 13589786300104L; /* check point expected */
314 requestIntervals_T1[5] = 13589790722564L; /* between check points */
315 requestIntervals_T1[6] = 13589796139823L; /* check point expected */
316 requestIntervals_T1[7] = 13589800400562L; /* between check points */
317 requestIntervals_T1[8] = 13589801594374L; /* check point expected */
318 requestIntervals_T1[9] = 13589802750295L; /* between check points */
319 requestIntervals_T1[10] = 13589804071157L; /* check point expected */
320 requestIntervals_T1[11] = 13589810124488L; /* between check points */
321 requestIntervals_T1[12] = 13589822493183L; /* check point expected */
322 requestIntervals_T1[13] = 13589824131273L; /* between check points */
323 requestIntervals_T1[14] = 13589825398284L; /* check point expected */
324 requestIntervals_T1[15] = 13589826664185L; /* between check points */
325 requestIntervals_T1[16] = 13589827811998L; /* check point expected */
326 requestIntervals_T1[17] = 13589828915763L; /* between check points */
327 requestIntervals_T1[18] = 13589830074220L; /* check point expected */
328 requestIntervals_T1[19] = 13589831232050L; /* between check points */
329 requestIntervals_T1[20] = 13589832394049L; /* check point expected */
330 requestIntervals_T1[21] = 13589833852883L; /* between check points */
331 requestIntervals_T1[22] = 13589839626892L; /* check point expected */
332 requestIntervals_T1[23] = 13589849509798L; /* between check points */
333 requestIntervals_T1[24] = 13589850728538L; /* check point expected */
334 requestIntervals_T1[25] = 13589851889230L; /* between check points */
335 requestIntervals_T1[26] = 13589853294800L; /* check point expected */
336 requestIntervals_T1[27] = 13589859414998L; /* between check points */
337 requestIntervals_T1[28] = 13589878046089L; /* check point expected */
338 requestIntervals_T1[29] = 13589886468603L; /* between check points */
339 requestIntervals_T1[30] = 13589902256918L; /* check point expected */
340 requestIntervals_T1[31] = 13589906758692L; /* last event in T1 */
341 }
342
343 }
This page took 0.037543 seconds and 5 git commands to generate.