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