Use o.e.test and jdt.annotation from Eclipse 4.5
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.tmf.ctf.core.tests / src / org / eclipse / tracecompass / tmf / ctf / core / tests / temp / statistics / TmfStatisticsTest.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 Ericsson
3 *
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 *
9 * Contributors:
10 * Alexandre Montplaisir - Initial API and implementation
11 ******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.ctf.core.tests.temp.statistics;
14
15 import static org.junit.Assert.assertEquals;
16
17 import java.util.List;
18 import java.util.Map;
19 import java.util.concurrent.TimeUnit;
20
21 import org.eclipse.tracecompass.tmf.core.statistics.ITmfStatistics;
22 import org.eclipse.tracecompass.tmf.ctf.core.tests.shared.CtfTmfTestTrace;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.TestRule;
26 import org.junit.rules.Timeout;
27
28 /**
29 * Base unit test class for any type of ITmfStatistics. Sub-classes should
30 * implement a "@BeforeClass" method to setup the 'backend' fixture accordingly.
31 *
32 * @author Alexandre Montplaisir
33 */
34 public abstract class TmfStatisticsTest {
35
36 /** Time-out tests after 30 seconds */
37 @Rule public TestRule globalTimeout= new Timeout(30, TimeUnit.SECONDS);
38
39 /** Test trace used for these tests */
40 protected static final CtfTmfTestTrace testTrace = CtfTmfTestTrace.KERNEL;
41
42 /** The statistics back-end object */
43 protected static ITmfStatistics backend;
44
45 /* Known values about the trace */
46 private static final int totalNbEvents = 695319;
47 private static final long tStart = 1332170682440133097L; /* Timestamp of first event */
48 private static final long tEnd = 1332170692664579801L; /* Timestamp of last event */
49
50 /* Timestamps of interest */
51 private static final long t1 = 1332170682490946000L;
52 private static final long t2 = 1332170682490947524L; /* event exactly here */
53 private static final long t3 = 1332170682490948000L;
54 private static final long t4 = 1332170682490949000L;
55 private static final long t5 = 1332170682490949270L; /* following event here */
56 private static final long t6 = 1332170682490949300L;
57
58 private static final String eventType = "lttng_statedump_process_state";
59
60
61 // ------------------------------------------------------------------------
62 // Tests for histogramQuery()
63 // ------------------------------------------------------------------------
64
65 /**
66 * Test the {@link ITmfStatistics#histogramQuery} method for the small known
67 * interval.
68 */
69 @Test
70 public void testHistogramQuerySmall() {
71 final int NB_REQ = 10;
72 List<Long> results = backend.histogramQuery(t1, t6, NB_REQ);
73
74 /* Make sure the returned array has the right size */
75 assertEquals(NB_REQ, results.size());
76
77 /* Check the contents of each "bucket" */
78 assertEquals(0, results.get(0).longValue());
79 assertEquals(0, results.get(1).longValue());
80 assertEquals(0, results.get(2).longValue());
81 assertEquals(0, results.get(3).longValue());
82 assertEquals(1, results.get(4).longValue());
83 assertEquals(0, results.get(5).longValue());
84 assertEquals(0, results.get(6).longValue());
85 assertEquals(0, results.get(7).longValue());
86 assertEquals(0, results.get(8).longValue());
87 assertEquals(1, results.get(9).longValue());
88
89 }
90
91 /**
92 * Test the {@link ITmfStatistics#histogramQuery} method over the whole
93 * trace.
94 */
95 @Test
96 public void testHistogramQueryFull() {
97 final int NB_REQ = 10;
98 List<Long> results = backend.histogramQuery(tStart, tEnd, NB_REQ);
99
100 /* Make sure the returned array has the right size */
101 assertEquals(NB_REQ, results.size());
102
103 /* Check the total number of events */
104 long count = 0;
105 for (long val : results) {
106 count += val;
107 }
108 assertEquals(totalNbEvents, count);
109
110 /* Check the contents of each "bucket" */
111 assertEquals(94161, results.get(0).longValue());
112 assertEquals(87348, results.get(1).longValue());
113 assertEquals(58941, results.get(2).longValue());
114 assertEquals(59879, results.get(3).longValue());
115 assertEquals(66941, results.get(4).longValue());
116 assertEquals(68939, results.get(5).longValue());
117 assertEquals(72746, results.get(6).longValue());
118 assertEquals(60749, results.get(7).longValue());
119 assertEquals(61208, results.get(8).longValue());
120 assertEquals(64407, results.get(9).longValue());
121 }
122
123 // ------------------------------------------------------------------------
124 // Test for getEventsTotal()
125 // ------------------------------------------------------------------------
126
127 /**
128 * Basic test for {@link ITmfStatistics#getEventsTotal}
129 */
130 @Test
131 public void testGetEventsTotal() {
132 long count = backend.getEventsTotal();
133 assertEquals(totalNbEvents, count);
134 }
135
136 // ------------------------------------------------------------------------
137 // Test for getEventTypesTotal()
138 // ------------------------------------------------------------------------
139
140 /**
141 * Basic test for {@link ITmfStatistics#getEventTypesTotal}
142 */
143 @Test
144 public void testEventTypesTotal() {
145 Map<String, Long> res = backend.getEventTypesTotal();
146 assertEquals(126, res.size()); /* Number of different event types in the trace */
147
148 long count = sumOfEvents(res);
149 assertEquals(totalNbEvents, count);
150 }
151
152 // ------------------------------------------------------------------------
153 // Tests for getEventsInRange(ITmfTimestamp start, ITmfTimestamp end)
154 // ------------------------------------------------------------------------
155
156 /**
157 * Test for {@link ITmfStatistics#getEventsInRange} over the whole trace.
158 */
159 @Test
160 public void testGetEventsInRangeWholeRange() {
161 long count = backend.getEventsInRange(tStart, tEnd);
162 assertEquals(totalNbEvents, count);
163 }
164
165 /**
166 * Test for {@link ITmfStatistics#getEventsInRange} for the whole range,
167 * except the start time (there is only one event at the start time).
168 */
169 @Test
170 public void testGetEventsInRangeMinusStart() {
171 long count = backend.getEventsInRange(tStart + 1, tEnd);
172 assertEquals(totalNbEvents - 1, count);
173 }
174
175 /**
176 * Test for {@link ITmfStatistics#getEventsInRange} for the whole range,
177 * except the end time (there is only one event at the end time).
178 */
179 @Test
180 public void testGetEventsInRangeMinusEnd() {
181 long count = backend.getEventsInRange(tStart, tEnd - 1);
182 assertEquals(totalNbEvents - 1, count);
183 }
184
185 /**
186 * Test for {@link ITmfStatistics#getEventsInRange} when both the start and
187 * end times don't match an event.
188 */
189 @Test
190 public void testGetEventsInRangeNoEventsAtEdges() {
191 long count = backend.getEventsInRange(t1, t6);
192 assertEquals(2, count);
193 }
194
195 /**
196 * Test for {@link ITmfStatistics#getEventsInRange} when the *start* of the
197 * interval is exactly on an event (that event should be included).
198 */
199 @Test
200 public void testGetEventsInRangeEventAtStart() {
201 long count = backend.getEventsInRange(t2, t3);
202 assertEquals(1, count);
203
204 count = backend.getEventsInRange(t2, t6);
205 assertEquals(2, count);
206 }
207
208 /**
209 * Test for {@link ITmfStatistics#getEventsInRange} when the *end* of the
210 * interval is exactly on an event (that event should be included).
211 */
212 @Test
213 public void testGetEventsInRangeEventAtEnd() {
214 long count = backend.getEventsInRange(t4, t5);
215 assertEquals(1, count);
216
217 count = backend.getEventsInRange(t1, t5);
218 assertEquals(2, count);
219 }
220
221 /**
222 * Test for {@link ITmfStatistics#getEventsInRange} when there are events
223 * matching exactly both the start and end times of the range (both should
224 * be included).
225 */
226 @Test
227 public void testGetEventsInRangeEventAtBoth() {
228 long count = backend.getEventsInRange(t2, t5);
229 assertEquals(2, count);
230 }
231
232 /**
233 * Test for {@link ITmfStatistics#getEventsInRange} when there are no events
234 * in a given range.
235 */
236 @Test
237 public void testGetEventsInRangeNoEvents() {
238 long count = backend.getEventsInRange(t3, t4);
239 assertEquals(0, count);
240 }
241
242 // ------------------------------------------------------------------------
243 // Tests for getEventTypesInRange(ITmfTimestamp start, ITmfTimestamp end)
244 // ------------------------------------------------------------------------
245
246 /**
247 * Test for {@link ITmfStatistics#getEventTypesInRange} over the whole trace.
248 */
249 @Test
250 public void testGetEventTypesInRangeWholeRange() {
251 Map<String, Long> result = backend.getEventTypesInRange(tStart, tEnd);
252 /* Number of events of that type in the whole trace */
253 assertEquals(new Long(464L), result.get(eventType));
254
255 long count = sumOfEvents(result);
256 assertEquals(totalNbEvents, count);
257 }
258
259 /**
260 * Test for {@link ITmfStatistics#getEventTypesInRange} for the whole range,
261 * except the start time (there is only one event at the start time).
262 */
263 @Test
264 public void testGetEventTypesInRangeMinusStart() {
265 Map<String, Long> result = backend.getEventTypesInRange(tStart + 1, tEnd);
266
267 long count = sumOfEvents(result);
268 assertEquals(totalNbEvents - 1, count);
269 }
270
271 /**
272 * Test for {@link ITmfStatistics#getEventTypesInRange} for the whole range,
273 * except the end time (there is only one event at the end time).
274 */
275 @Test
276 public void testGetEventTypesInRangeMinusEnd() {
277 Map<String, Long> result = backend.getEventTypesInRange(tStart, tEnd - 1);
278
279 long count = sumOfEvents(result);
280 assertEquals(totalNbEvents - 1, count);
281 }
282
283 /**
284 * Test for {@link ITmfStatistics#getEventTypesInRange} when both the start
285 * and end times don't match an event.
286 */
287 @Test
288 public void testGetEventTypesInRangeNoEventsAtEdges() {
289 Map<String, Long> result = backend.getEventTypesInRange(t1, t6);
290 assertEquals(new Long(2L), result.get(eventType));
291
292 long count = sumOfEvents(result);
293 assertEquals(2, count);
294 }
295
296 /**
297 * Test for {@link ITmfStatistics#getEventTypesInRange} when the *start* of
298 * the interval is exactly on an event (that event should be included).
299 */
300 @Test
301 public void testGetEventTypesInRangeEventAtStart() {
302 Map<String, Long> result = backend.getEventTypesInRange(t2, t3);
303 assertEquals(new Long(1L), result.get(eventType));
304 long count = sumOfEvents(result);
305 assertEquals(1, count);
306
307 result = backend.getEventTypesInRange(t2, t6);
308 assertEquals(new Long(2L), result.get(eventType));
309 count = sumOfEvents(result);
310 assertEquals(2, count);
311 }
312
313 /**
314 * Test for {@link ITmfStatistics#getEventTypesInRange} when the *end* of
315 * the interval is exactly on an event (that event should be included).
316 */
317 @Test
318 public void testGetEventTypesInRangeEventAtEnd() {
319 Map<String, Long> result = backend.getEventTypesInRange(t4, t5);
320 assertEquals(new Long(1L), result.get(eventType));
321 long count = sumOfEvents(result);
322 assertEquals(1, count);
323
324 result = backend.getEventTypesInRange(t1, t5);
325 assertEquals(new Long(2L), result.get(eventType));
326 count = sumOfEvents(result);
327 assertEquals(2, count);
328 }
329
330 /**
331 * Test for {@link ITmfStatistics#getEventTypesInRange} when there are
332 * events matching exactly both the start and end times of the range (both
333 * should be included).
334 */
335 @Test
336 public void testGetEventTypesInRangeEventAtBoth() {
337 Map<String, Long> result = backend.getEventTypesInRange(t2, t5);
338 assertEquals(new Long(2L), result.get(eventType));
339 long count = sumOfEvents(result);
340 assertEquals(2, count);
341 }
342
343 /**
344 * Test for {@link ITmfStatistics#getEventTypesInRange} when there are no
345 * events in a given range.
346 */
347 @Test
348 public void testGetEventTypesInRangeNoEvents() {
349 Map<String, Long> result = backend.getEventTypesInRange(t3, t4);
350 long count = sumOfEvents(result);
351 assertEquals(0, count);
352 }
353
354 // ------------------------------------------------------------------------
355 // Convenience methods
356 // ------------------------------------------------------------------------
357
358 private static long sumOfEvents(Map<String, Long> map) {
359 long count = 0;
360 for (long val : map.values()) {
361 count += val;
362 }
363 return count;
364 }
365 }
This page took 0.041939 seconds and 5 git commands to generate.