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