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
.temp
.statistics
;
15 import static org
.junit
.Assert
.assertEquals
;
17 import java
.util
.List
;
19 import java
.util
.concurrent
.TimeUnit
;
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
;
29 * Base unit test class for any type of ITmfStatistics. Sub-classes should
30 * implement a "@BeforeClass" method to setup the 'backend' fixture accordingly.
32 * @author Alexandre Montplaisir
34 public abstract class TmfStatisticsTest
{
36 /** Time-out tests after 30 seconds */
37 @Rule public TestRule globalTimeout
= new Timeout(30, TimeUnit
.SECONDS
);
39 /** Test trace used for these tests */
40 protected static final CtfTmfTestTrace testTrace
= CtfTmfTestTrace
.KERNEL
;
42 /** The statistics back-end object */
43 protected static ITmfStatistics backend
;
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 */
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;
58 private static final String eventType
= "lttng_statedump_process_state";
61 // ------------------------------------------------------------------------
62 // Tests for histogramQuery()
63 // ------------------------------------------------------------------------
66 * Test the {@link ITmfStatistics#histogramQuery} method for the small known
70 public void testHistogramQuerySmall() {
71 final int NB_REQ
= 10;
72 List
<Long
> results
= backend
.histogramQuery(t1
, t6
, NB_REQ
);
74 /* Make sure the returned array has the right size */
75 assertEquals(NB_REQ
, results
.size());
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());
92 * Test the {@link ITmfStatistics#histogramQuery} method over the whole
96 public void testHistogramQueryFull() {
97 final int NB_REQ
= 10;
98 List
<Long
> results
= backend
.histogramQuery(tStart
, tEnd
, NB_REQ
);
100 /* Make sure the returned array has the right size */
101 assertEquals(NB_REQ
, results
.size());
103 /* Check the total number of events */
105 for (long val
: results
) {
108 assertEquals(totalNbEvents
, count
);
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());
123 // ------------------------------------------------------------------------
124 // Test for getEventsTotal()
125 // ------------------------------------------------------------------------
128 * Basic test for {@link ITmfStatistics#getEventsTotal}
131 public void testGetEventsTotal() {
132 long count
= backend
.getEventsTotal();
133 assertEquals(totalNbEvents
, count
);
136 // ------------------------------------------------------------------------
137 // Test for getEventTypesTotal()
138 // ------------------------------------------------------------------------
141 * Basic test for {@link ITmfStatistics#getEventTypesTotal}
144 public void testEventTypesTotal() {
145 Map
<String
, Long
> res
= backend
.getEventTypesTotal();
146 assertEquals(126, res
.size()); /* Number of different event types in the trace */
148 long count
= sumOfEvents(res
);
149 assertEquals(totalNbEvents
, count
);
152 // ------------------------------------------------------------------------
153 // Tests for getEventsInRange(ITmfTimestamp start, ITmfTimestamp end)
154 // ------------------------------------------------------------------------
157 * Test for {@link ITmfStatistics#getEventsInRange} over the whole trace.
160 public void testGetEventsInRangeWholeRange() {
161 long count
= backend
.getEventsInRange(tStart
, tEnd
);
162 assertEquals(totalNbEvents
, count
);
166 * Test for {@link ITmfStatistics#getEventsInRange} for the whole range,
167 * except the start time (there is only one event at the start time).
170 public void testGetEventsInRangeMinusStart() {
171 long count
= backend
.getEventsInRange(tStart
+ 1, tEnd
);
172 assertEquals(totalNbEvents
- 1, count
);
176 * Test for {@link ITmfStatistics#getEventsInRange} for the whole range,
177 * except the end time (there is only one event at the end time).
180 public void testGetEventsInRangeMinusEnd() {
181 long count
= backend
.getEventsInRange(tStart
, tEnd
- 1);
182 assertEquals(totalNbEvents
- 1, count
);
186 * Test for {@link ITmfStatistics#getEventsInRange} when both the start and
187 * end times don't match an event.
190 public void testGetEventsInRangeNoEventsAtEdges() {
191 long count
= backend
.getEventsInRange(t1
, t6
);
192 assertEquals(2, count
);
196 * Test for {@link ITmfStatistics#getEventsInRange} when the *start* of the
197 * interval is exactly on an event (that event should be included).
200 public void testGetEventsInRangeEventAtStart() {
201 long count
= backend
.getEventsInRange(t2
, t3
);
202 assertEquals(1, count
);
204 count
= backend
.getEventsInRange(t2
, t6
);
205 assertEquals(2, count
);
209 * Test for {@link ITmfStatistics#getEventsInRange} when the *end* of the
210 * interval is exactly on an event (that event should be included).
213 public void testGetEventsInRangeEventAtEnd() {
214 long count
= backend
.getEventsInRange(t4
, t5
);
215 assertEquals(1, count
);
217 count
= backend
.getEventsInRange(t1
, t5
);
218 assertEquals(2, count
);
222 * Test for {@link ITmfStatistics#getEventsInRange} when there are events
223 * matching exactly both the start and end times of the range (both should
227 public void testGetEventsInRangeEventAtBoth() {
228 long count
= backend
.getEventsInRange(t2
, t5
);
229 assertEquals(2, count
);
233 * Test for {@link ITmfStatistics#getEventsInRange} when there are no events
237 public void testGetEventsInRangeNoEvents() {
238 long count
= backend
.getEventsInRange(t3
, t4
);
239 assertEquals(0, count
);
242 // ------------------------------------------------------------------------
243 // Tests for getEventTypesInRange(ITmfTimestamp start, ITmfTimestamp end)
244 // ------------------------------------------------------------------------
247 * Test for {@link ITmfStatistics#getEventTypesInRange} over the whole trace.
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
));
255 long count
= sumOfEvents(result
);
256 assertEquals(totalNbEvents
, count
);
260 * Test for {@link ITmfStatistics#getEventTypesInRange} for the whole range,
261 * except the start time (there is only one event at the start time).
264 public void testGetEventTypesInRangeMinusStart() {
265 Map
<String
, Long
> result
= backend
.getEventTypesInRange(tStart
+ 1, tEnd
);
267 long count
= sumOfEvents(result
);
268 assertEquals(totalNbEvents
- 1, count
);
272 * Test for {@link ITmfStatistics#getEventTypesInRange} for the whole range,
273 * except the end time (there is only one event at the end time).
276 public void testGetEventTypesInRangeMinusEnd() {
277 Map
<String
, Long
> result
= backend
.getEventTypesInRange(tStart
, tEnd
- 1);
279 long count
= sumOfEvents(result
);
280 assertEquals(totalNbEvents
- 1, count
);
284 * Test for {@link ITmfStatistics#getEventTypesInRange} when both the start
285 * and end times don't match an event.
288 public void testGetEventTypesInRangeNoEventsAtEdges() {
289 Map
<String
, Long
> result
= backend
.getEventTypesInRange(t1
, t6
);
290 assertEquals(new Long(2L), result
.get(eventType
));
292 long count
= sumOfEvents(result
);
293 assertEquals(2, count
);
297 * Test for {@link ITmfStatistics#getEventTypesInRange} when the *start* of
298 * the interval is exactly on an event (that event should be included).
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
);
307 result
= backend
.getEventTypesInRange(t2
, t6
);
308 assertEquals(new Long(2L), result
.get(eventType
));
309 count
= sumOfEvents(result
);
310 assertEquals(2, count
);
314 * Test for {@link ITmfStatistics#getEventTypesInRange} when the *end* of
315 * the interval is exactly on an event (that event should be included).
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
);
324 result
= backend
.getEventTypesInRange(t1
, t5
);
325 assertEquals(new Long(2L), result
.get(eventType
));
326 count
= sumOfEvents(result
);
327 assertEquals(2, count
);
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).
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
);
344 * Test for {@link ITmfStatistics#getEventTypesInRange} when there are no
345 * events in a given range.
348 public void testGetEventTypesInRangeNoEvents() {
349 Map
<String
, Long
> result
= backend
.getEventTypesInRange(t3
, t4
);
350 long count
= sumOfEvents(result
);
351 assertEquals(0, count
);
354 // ------------------------------------------------------------------------
355 // Convenience methods
356 // ------------------------------------------------------------------------
358 private static long sumOfEvents(Map
<String
, Long
> map
) {
360 for (long val
: map
.values()) {