1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 Ericsson
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
10 * Alexandre Montplaisir - Initial API and implementation
11 ******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.ctf
.core
.tests
.statistics
;
15 import static org
.junit
.Assert
.assertEquals
;
17 import java
.util
.List
;
20 import org
.eclipse
.tracecompass
.tmf
.core
.statistics
.ITmfStatistics
;
21 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.tests
.shared
.CtfTmfTestTrace
;
22 import org
.junit
.Rule
;
23 import org
.junit
.Test
;
24 import org
.junit
.rules
.TestRule
;
25 import org
.junit
.rules
.Timeout
;
28 * Base unit test class for any type of ITmfStatistics. Sub-classes should
29 * implement a "@BeforeClass" method to setup the 'backend' fixture accordingly.
31 * @author Alexandre Montplaisir
33 public abstract class TmfStatisticsTest
{
35 /** Time-out tests after 30 seconds */
36 @Rule public TestRule globalTimeout
= new Timeout(30000);
38 /** Test trace used for these tests */
39 protected static final CtfTmfTestTrace testTrace
= CtfTmfTestTrace
.KERNEL
;
41 /** The statistics back-end object */
42 protected static ITmfStatistics backend
;
44 /* Known values about the trace */
45 private static final int totalNbEvents
= 695319;
46 private static final long tStart
= 1332170682440133097L; /* Timestamp of first event */
47 private static final long tEnd
= 1332170692664579801L; /* Timestamp of last event */
49 /* Timestamps of interest */
50 private static final long t1
= 1332170682490946000L;
51 private static final long t2
= 1332170682490947524L; /* event exactly here */
52 private static final long t3
= 1332170682490948000L;
53 private static final long t4
= 1332170682490949000L;
54 private static final long t5
= 1332170682490949270L; /* following event here */
55 private static final long t6
= 1332170682490949300L;
57 private static final String eventType
= "lttng_statedump_process_state";
60 // ------------------------------------------------------------------------
61 // Tests for histogramQuery()
62 // ------------------------------------------------------------------------
65 * Test the {@link ITmfStatistics#histogramQuery} method for the small known
69 public void testHistogramQuerySmall() {
70 final int NB_REQ
= 10;
71 List
<Long
> results
= backend
.histogramQuery(t1
, t6
, NB_REQ
);
73 /* Make sure the returned array has the right size */
74 assertEquals(NB_REQ
, results
.size());
76 /* Check the contents of each "bucket" */
77 assertEquals(0, results
.get(0).longValue());
78 assertEquals(0, results
.get(1).longValue());
79 assertEquals(0, results
.get(2).longValue());
80 assertEquals(0, results
.get(3).longValue());
81 assertEquals(1, results
.get(4).longValue());
82 assertEquals(0, results
.get(5).longValue());
83 assertEquals(0, results
.get(6).longValue());
84 assertEquals(0, results
.get(7).longValue());
85 assertEquals(0, results
.get(8).longValue());
86 assertEquals(1, results
.get(9).longValue());
91 * Test the {@link ITmfStatistics#histogramQuery} method over the whole
95 public void testHistogramQueryFull() {
96 final int NB_REQ
= 10;
97 List
<Long
> results
= backend
.histogramQuery(tStart
, tEnd
, NB_REQ
);
99 /* Make sure the returned array has the right size */
100 assertEquals(NB_REQ
, results
.size());
102 /* Check the total number of events */
104 for (long val
: results
) {
107 assertEquals(totalNbEvents
, count
);
109 /* Check the contents of each "bucket" */
110 assertEquals(94161, results
.get(0).longValue());
111 assertEquals(87348, results
.get(1).longValue());
112 assertEquals(58941, results
.get(2).longValue());
113 assertEquals(59879, results
.get(3).longValue());
114 assertEquals(66941, results
.get(4).longValue());
115 assertEquals(68939, results
.get(5).longValue());
116 assertEquals(72746, results
.get(6).longValue());
117 assertEquals(60749, results
.get(7).longValue());
118 assertEquals(61208, results
.get(8).longValue());
119 assertEquals(64407, results
.get(9).longValue());
122 // ------------------------------------------------------------------------
123 // Test for getEventsTotal()
124 // ------------------------------------------------------------------------
127 * Basic test for {@link ITmfStatistics#getEventsTotal}
130 public void testGetEventsTotal() {
131 long count
= backend
.getEventsTotal();
132 assertEquals(totalNbEvents
, count
);
135 // ------------------------------------------------------------------------
136 // Test for getEventTypesTotal()
137 // ------------------------------------------------------------------------
140 * Basic test for {@link ITmfStatistics#getEventTypesTotal}
143 public void testEventTypesTotal() {
144 Map
<String
, Long
> res
= backend
.getEventTypesTotal();
145 assertEquals(126, res
.size()); /* Number of different event types in the trace */
147 long count
= sumOfEvents(res
);
148 assertEquals(totalNbEvents
, count
);
151 // ------------------------------------------------------------------------
152 // Tests for getEventsInRange(ITmfTimestamp start, ITmfTimestamp end)
153 // ------------------------------------------------------------------------
156 * Test for {@link ITmfStatistics#getEventsInRange} over the whole trace.
159 public void testGetEventsInRangeWholeRange() {
160 long count
= backend
.getEventsInRange(tStart
, tEnd
);
161 assertEquals(totalNbEvents
, count
);
165 * Test for {@link ITmfStatistics#getEventsInRange} for the whole range,
166 * except the start time (there is only one event at the start time).
169 public void testGetEventsInRangeMinusStart() {
170 long count
= backend
.getEventsInRange(tStart
+ 1, tEnd
);
171 assertEquals(totalNbEvents
- 1, count
);
175 * Test for {@link ITmfStatistics#getEventsInRange} for the whole range,
176 * except the end time (there is only one event at the end time).
179 public void testGetEventsInRangeMinusEnd() {
180 long count
= backend
.getEventsInRange(tStart
, tEnd
- 1);
181 assertEquals(totalNbEvents
- 1, count
);
185 * Test for {@link ITmfStatistics#getEventsInRange} when both the start and
186 * end times don't match an event.
189 public void testGetEventsInRangeNoEventsAtEdges() {
190 long count
= backend
.getEventsInRange(t1
, t6
);
191 assertEquals(2, count
);
195 * Test for {@link ITmfStatistics#getEventsInRange} when the *start* of the
196 * interval is exactly on an event (that event should be included).
199 public void testGetEventsInRangeEventAtStart() {
200 long count
= backend
.getEventsInRange(t2
, t3
);
201 assertEquals(1, count
);
203 count
= backend
.getEventsInRange(t2
, t6
);
204 assertEquals(2, count
);
208 * Test for {@link ITmfStatistics#getEventsInRange} when the *end* of the
209 * interval is exactly on an event (that event should be included).
212 public void testGetEventsInRangeEventAtEnd() {
213 long count
= backend
.getEventsInRange(t4
, t5
);
214 assertEquals(1, count
);
216 count
= backend
.getEventsInRange(t1
, t5
);
217 assertEquals(2, count
);
221 * Test for {@link ITmfStatistics#getEventsInRange} when there are events
222 * matching exactly both the start and end times of the range (both should
226 public void testGetEventsInRangeEventAtBoth() {
227 long count
= backend
.getEventsInRange(t2
, t5
);
228 assertEquals(2, count
);
232 * Test for {@link ITmfStatistics#getEventsInRange} when there are no events
236 public void testGetEventsInRangeNoEvents() {
237 long count
= backend
.getEventsInRange(t3
, t4
);
238 assertEquals(0, count
);
241 // ------------------------------------------------------------------------
242 // Tests for getEventTypesInRange(ITmfTimestamp start, ITmfTimestamp end)
243 // ------------------------------------------------------------------------
246 * Test for {@link ITmfStatistics#getEventTypesInRange} over the whole trace.
249 public void testGetEventTypesInRangeWholeRange() {
250 Map
<String
, Long
> result
= backend
.getEventTypesInRange(tStart
, tEnd
);
251 /* Number of events of that type in the whole trace */
252 assertEquals(new Long(464L), result
.get(eventType
));
254 long count
= sumOfEvents(result
);
255 assertEquals(totalNbEvents
, count
);
259 * Test for {@link ITmfStatistics#getEventTypesInRange} for the whole range,
260 * except the start time (there is only one event at the start time).
263 public void testGetEventTypesInRangeMinusStart() {
264 Map
<String
, Long
> result
= backend
.getEventTypesInRange(tStart
+ 1, tEnd
);
266 long count
= sumOfEvents(result
);
267 assertEquals(totalNbEvents
- 1, count
);
271 * Test for {@link ITmfStatistics#getEventTypesInRange} for the whole range,
272 * except the end time (there is only one event at the end time).
275 public void testGetEventTypesInRangeMinusEnd() {
276 Map
<String
, Long
> result
= backend
.getEventTypesInRange(tStart
, tEnd
- 1);
278 long count
= sumOfEvents(result
);
279 assertEquals(totalNbEvents
- 1, count
);
283 * Test for {@link ITmfStatistics#getEventTypesInRange} when both the start
284 * and end times don't match an event.
287 public void testGetEventTypesInRangeNoEventsAtEdges() {
288 Map
<String
, Long
> result
= backend
.getEventTypesInRange(t1
, t6
);
289 assertEquals(new Long(2L), result
.get(eventType
));
291 long count
= sumOfEvents(result
);
292 assertEquals(2, count
);
296 * Test for {@link ITmfStatistics#getEventTypesInRange} when the *start* of
297 * the interval is exactly on an event (that event should be included).
300 public void testGetEventTypesInRangeEventAtStart() {
301 Map
<String
, Long
> result
= backend
.getEventTypesInRange(t2
, t3
);
302 assertEquals(new Long(1L), result
.get(eventType
));
303 long count
= sumOfEvents(result
);
304 assertEquals(1, count
);
306 result
= backend
.getEventTypesInRange(t2
, t6
);
307 assertEquals(new Long(2L), result
.get(eventType
));
308 count
= sumOfEvents(result
);
309 assertEquals(2, count
);
313 * Test for {@link ITmfStatistics#getEventTypesInRange} when the *end* of
314 * the interval is exactly on an event (that event should be included).
317 public void testGetEventTypesInRangeEventAtEnd() {
318 Map
<String
, Long
> result
= backend
.getEventTypesInRange(t4
, t5
);
319 assertEquals(new Long(1L), result
.get(eventType
));
320 long count
= sumOfEvents(result
);
321 assertEquals(1, count
);
323 result
= backend
.getEventTypesInRange(t1
, t5
);
324 assertEquals(new Long(2L), result
.get(eventType
));
325 count
= sumOfEvents(result
);
326 assertEquals(2, count
);
330 * Test for {@link ITmfStatistics#getEventTypesInRange} when there are
331 * events matching exactly both the start and end times of the range (both
332 * should be included).
335 public void testGetEventTypesInRangeEventAtBoth() {
336 Map
<String
, Long
> result
= backend
.getEventTypesInRange(t2
, t5
);
337 assertEquals(new Long(2L), result
.get(eventType
));
338 long count
= sumOfEvents(result
);
339 assertEquals(2, count
);
343 * Test for {@link ITmfStatistics#getEventTypesInRange} when there are no
344 * events in a given range.
347 public void testGetEventTypesInRangeNoEvents() {
348 Map
<String
, Long
> result
= backend
.getEventTypesInRange(t3
, t4
);
349 long count
= sumOfEvents(result
);
350 assertEquals(0, count
);
353 // ------------------------------------------------------------------------
354 // Convenience methods
355 // ------------------------------------------------------------------------
357 private static long sumOfEvents(Map
<String
, Long
> map
) {
359 for (long val
: map
.values()) {