LTTng: CPU usage analysis from the LTTng kernel trace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / 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.linuxtools.tmf.core.tests.statistics;
14
15 import static org.junit.Assert.assertEquals;
16
17 import java.util.List;
18 import java.util.Map;
19
20 import org.eclipse.linuxtools.tmf.core.statistics.ITmfStatistics;
21 import org.eclipse.linuxtools.tmf.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;
26
27 /**
28 * Base unit test class for any type of ITmfStatistics. Sub-classes should
29 * implement a "@BeforeClass" method to setup the 'backend' fixture accordingly.
30 *
31 * @author Alexandre Montplaisir
32 */
33 public abstract class TmfStatisticsTest {
34
35 /** Time-out tests after 30 seconds */
36 @Rule public TestRule globalTimeout= new Timeout(30000);
37
38 /** Test trace used for these tests */
39 protected static final CtfTmfTestTrace testTrace = CtfTmfTestTrace.KERNEL;
40
41 /** The statistics back-end object */
42 protected static ITmfStatistics backend;
43
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 */
48
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;
56
57 private static final String eventType = "lttng_statedump_process_state";
58
59
60 // ------------------------------------------------------------------------
61 // Tests for histogramQuery()
62 // ------------------------------------------------------------------------
63
64 /**
65 * Test the {@link ITmfStatistics#histogramQuery} method for the small known
66 * interval.
67 */
68 @Test
69 public void testHistogramQuerySmall() {
70 final int NB_REQ = 10;
71 List<Long> results = backend.histogramQuery(t1, t6, NB_REQ);
72
73 /* Make sure the returned array has the right size */
74 assertEquals(NB_REQ, results.size());
75
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());
87
88 }
89
90 /**
91 * Test the {@link ITmfStatistics#histogramQuery} method over the whole
92 * trace.
93 */
94 @Test
95 public void testHistogramQueryFull() {
96 final int NB_REQ = 10;
97 List<Long> results = backend.histogramQuery(tStart, tEnd, NB_REQ);
98
99 /* Make sure the returned array has the right size */
100 assertEquals(NB_REQ, results.size());
101
102 /* Check the total number of events */
103 long count = 0;
104 for (long val : results) {
105 count += val;
106 }
107 assertEquals(totalNbEvents, count);
108
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());
120 }
121
122 // ------------------------------------------------------------------------
123 // Test for getEventsTotal()
124 // ------------------------------------------------------------------------
125
126 /**
127 * Basic test for {@link ITmfStatistics#getEventsTotal}
128 */
129 @Test
130 public void testGetEventsTotal() {
131 long count = backend.getEventsTotal();
132 assertEquals(totalNbEvents, count);
133 }
134
135 // ------------------------------------------------------------------------
136 // Test for getEventTypesTotal()
137 // ------------------------------------------------------------------------
138
139 /**
140 * Basic test for {@link ITmfStatistics#getEventTypesTotal}
141 */
142 @Test
143 public void testEventTypesTotal() {
144 Map<String, Long> res = backend.getEventTypesTotal();
145 assertEquals(126, res.size()); /* Number of different event types in the trace */
146
147 long count = sumOfEvents(res);
148 assertEquals(totalNbEvents, count);
149 }
150
151 // ------------------------------------------------------------------------
152 // Tests for getEventsInRange(ITmfTimestamp start, ITmfTimestamp end)
153 // ------------------------------------------------------------------------
154
155 /**
156 * Test for {@link ITmfStatistics#getEventsInRange} over the whole trace.
157 */
158 @Test
159 public void testGetEventsInRangeWholeRange() {
160 long count = backend.getEventsInRange(tStart, tEnd);
161 assertEquals(totalNbEvents, count);
162 }
163
164 /**
165 * Test for {@link ITmfStatistics#getEventsInRange} for the whole range,
166 * except the start time (there is only one event at the start time).
167 */
168 @Test
169 public void testGetEventsInRangeMinusStart() {
170 long count = backend.getEventsInRange(tStart + 1, tEnd);
171 assertEquals(totalNbEvents - 1, count);
172 }
173
174 /**
175 * Test for {@link ITmfStatistics#getEventsInRange} for the whole range,
176 * except the end time (there is only one event at the end time).
177 */
178 @Test
179 public void testGetEventsInRangeMinusEnd() {
180 long count = backend.getEventsInRange(tStart, tEnd - 1);
181 assertEquals(totalNbEvents - 1, count);
182 }
183
184 /**
185 * Test for {@link ITmfStatistics#getEventsInRange} when both the start and
186 * end times don't match an event.
187 */
188 @Test
189 public void testGetEventsInRangeNoEventsAtEdges() {
190 long count = backend.getEventsInRange(t1, t6);
191 assertEquals(2, count);
192 }
193
194 /**
195 * Test for {@link ITmfStatistics#getEventsInRange} when the *start* of the
196 * interval is exactly on an event (that event should be included).
197 */
198 @Test
199 public void testGetEventsInRangeEventAtStart() {
200 long count = backend.getEventsInRange(t2, t3);
201 assertEquals(1, count);
202
203 count = backend.getEventsInRange(t2, t6);
204 assertEquals(2, count);
205 }
206
207 /**
208 * Test for {@link ITmfStatistics#getEventsInRange} when the *end* of the
209 * interval is exactly on an event (that event should be included).
210 */
211 @Test
212 public void testGetEventsInRangeEventAtEnd() {
213 long count = backend.getEventsInRange(t4, t5);
214 assertEquals(1, count);
215
216 count = backend.getEventsInRange(t1, t5);
217 assertEquals(2, count);
218 }
219
220 /**
221 * Test for {@link ITmfStatistics#getEventsInRange} when there are events
222 * matching exactly both the start and end times of the range (both should
223 * be included).
224 */
225 @Test
226 public void testGetEventsInRangeEventAtBoth() {
227 long count = backend.getEventsInRange(t2, t5);
228 assertEquals(2, count);
229 }
230
231 /**
232 * Test for {@link ITmfStatistics#getEventsInRange} when there are no events
233 * in a given range.
234 */
235 @Test
236 public void testGetEventsInRangeNoEvents() {
237 long count = backend.getEventsInRange(t3, t4);
238 assertEquals(0, count);
239 }
240
241 // ------------------------------------------------------------------------
242 // Tests for getEventTypesInRange(ITmfTimestamp start, ITmfTimestamp end)
243 // ------------------------------------------------------------------------
244
245 /**
246 * Test for {@link ITmfStatistics#getEventTypesInRange} over the whole trace.
247 */
248 @Test
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));
253
254 long count = sumOfEvents(result);
255 assertEquals(totalNbEvents, count);
256 }
257
258 /**
259 * Test for {@link ITmfStatistics#getEventTypesInRange} for the whole range,
260 * except the start time (there is only one event at the start time).
261 */
262 @Test
263 public void testGetEventTypesInRangeMinusStart() {
264 Map<String, Long> result = backend.getEventTypesInRange(tStart + 1, tEnd);
265
266 long count = sumOfEvents(result);
267 assertEquals(totalNbEvents - 1, count);
268 }
269
270 /**
271 * Test for {@link ITmfStatistics#getEventTypesInRange} for the whole range,
272 * except the end time (there is only one event at the end time).
273 */
274 @Test
275 public void testGetEventTypesInRangeMinusEnd() {
276 Map<String, Long> result = backend.getEventTypesInRange(tStart, tEnd - 1);
277
278 long count = sumOfEvents(result);
279 assertEquals(totalNbEvents - 1, count);
280 }
281
282 /**
283 * Test for {@link ITmfStatistics#getEventTypesInRange} when both the start
284 * and end times don't match an event.
285 */
286 @Test
287 public void testGetEventTypesInRangeNoEventsAtEdges() {
288 Map<String, Long> result = backend.getEventTypesInRange(t1, t6);
289 assertEquals(new Long(2L), result.get(eventType));
290
291 long count = sumOfEvents(result);
292 assertEquals(2, count);
293 }
294
295 /**
296 * Test for {@link ITmfStatistics#getEventTypesInRange} when the *start* of
297 * the interval is exactly on an event (that event should be included).
298 */
299 @Test
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);
305
306 result = backend.getEventTypesInRange(t2, t6);
307 assertEquals(new Long(2L), result.get(eventType));
308 count = sumOfEvents(result);
309 assertEquals(2, count);
310 }
311
312 /**
313 * Test for {@link ITmfStatistics#getEventTypesInRange} when the *end* of
314 * the interval is exactly on an event (that event should be included).
315 */
316 @Test
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);
322
323 result = backend.getEventTypesInRange(t1, t5);
324 assertEquals(new Long(2L), result.get(eventType));
325 count = sumOfEvents(result);
326 assertEquals(2, count);
327 }
328
329 /**
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).
333 */
334 @Test
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);
340 }
341
342 /**
343 * Test for {@link ITmfStatistics#getEventTypesInRange} when there are no
344 * events in a given range.
345 */
346 @Test
347 public void testGetEventTypesInRangeNoEvents() {
348 Map<String, Long> result = backend.getEventTypesInRange(t3, t4);
349 long count = sumOfEvents(result);
350 assertEquals(0, count);
351 }
352
353 // ------------------------------------------------------------------------
354 // Convenience methods
355 // ------------------------------------------------------------------------
356
357 private static long sumOfEvents(Map<String, Long> map) {
358 long count = 0;
359 for (long val : map.values()) {
360 count += val;
361 }
362 return count;
363 }
364 }
This page took 0.047647 seconds and 5 git commands to generate.