| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2012, 2015 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.event; |
| 14 | |
| 15 | import static org.junit.Assert.assertEquals; |
| 16 | import static org.junit.Assert.assertNotNull; |
| 17 | import static org.junit.Assert.assertTrue; |
| 18 | import static org.junit.Assert.fail; |
| 19 | |
| 20 | import java.util.Map; |
| 21 | import java.util.concurrent.TimeUnit; |
| 22 | |
| 23 | import org.eclipse.jdt.annotation.NonNull; |
| 24 | import org.eclipse.tracecompass.ctf.core.CTFStrings; |
| 25 | import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem; |
| 26 | import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace; |
| 27 | import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException; |
| 28 | import org.eclipse.tracecompass.tmf.core.statistics.ITmfStatistics; |
| 29 | import org.eclipse.tracecompass.tmf.core.statistics.TmfStateStatistics; |
| 30 | import org.eclipse.tracecompass.tmf.core.statistics.TmfStatisticsEventTypesModule; |
| 31 | import org.eclipse.tracecompass.tmf.core.statistics.TmfStatisticsTotalsModule; |
| 32 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
| 33 | import org.eclipse.tracecompass.tmf.ctf.core.tests.shared.CtfTmfTestTraceUtils; |
| 34 | import org.junit.After; |
| 35 | import org.junit.Before; |
| 36 | import org.junit.Rule; |
| 37 | import org.junit.Test; |
| 38 | import org.junit.rules.TestRule; |
| 39 | import org.junit.rules.Timeout; |
| 40 | |
| 41 | /** |
| 42 | * Unit tests for handling of lost events by the statistics backends. |
| 43 | * |
| 44 | * @author Alexandre Montplaisir |
| 45 | */ |
| 46 | public class CtfTmfLostEventStatisticsTest { |
| 47 | |
| 48 | /** Time-out tests after 30 seconds */ |
| 49 | @Rule |
| 50 | public TestRule globalTimeout= new Timeout(30, TimeUnit.SECONDS); |
| 51 | |
| 52 | /**Test trace with lost events */ |
| 53 | private static final @NonNull CtfTestTrace lostEventsTrace = CtfTestTrace.HELLO_LOST; |
| 54 | |
| 55 | private ITmfTrace fTrace; |
| 56 | |
| 57 | /** The statistics back-end object for the trace with lost events */ |
| 58 | private ITmfStatistics fStats; |
| 59 | |
| 60 | /* The two analysis modules needed for fStats */ |
| 61 | private TmfStatisticsTotalsModule fTotalsMod; |
| 62 | private TmfStatisticsEventTypesModule fEventTypesMod; |
| 63 | |
| 64 | // ------------------------------------------------------------------------ |
| 65 | // Maintenance |
| 66 | // ------------------------------------------------------------------------ |
| 67 | |
| 68 | /** |
| 69 | * Test setup |
| 70 | */ |
| 71 | @Before |
| 72 | public void setUp() { |
| 73 | ITmfTrace trace = CtfTmfTestTraceUtils.getTrace(lostEventsTrace); |
| 74 | fTrace = trace; |
| 75 | |
| 76 | /* Prepare the two analysis-backed state systems */ |
| 77 | fTotalsMod = new TmfStatisticsTotalsModule(); |
| 78 | fEventTypesMod = new TmfStatisticsEventTypesModule(); |
| 79 | try { |
| 80 | fTotalsMod.setTrace(trace); |
| 81 | fEventTypesMod.setTrace(trace); |
| 82 | } catch (TmfAnalysisException e) { |
| 83 | fail(); |
| 84 | } |
| 85 | |
| 86 | fTotalsMod.schedule(); |
| 87 | fEventTypesMod.schedule(); |
| 88 | assertTrue(fTotalsMod.waitForCompletion()); |
| 89 | assertTrue(fEventTypesMod.waitForCompletion()); |
| 90 | |
| 91 | ITmfStateSystem totalsSS = fTotalsMod.getStateSystem(); |
| 92 | ITmfStateSystem eventTypesSS = fEventTypesMod.getStateSystem(); |
| 93 | assertNotNull(totalsSS); |
| 94 | assertNotNull(eventTypesSS); |
| 95 | |
| 96 | fStats = new TmfStateStatistics(totalsSS, eventTypesSS); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Test cleanup |
| 101 | */ |
| 102 | @After |
| 103 | public void tearDown() { |
| 104 | fStats.dispose(); |
| 105 | fTotalsMod.dispose(); |
| 106 | fEventTypesMod.dispose(); |
| 107 | fTrace.dispose(); |
| 108 | } |
| 109 | |
| 110 | // ------------------------------------------------------------------------ |
| 111 | // Test methods |
| 112 | // ------------------------------------------------------------------------ |
| 113 | |
| 114 | /* |
| 115 | * Trace start = 1376592664828559410 |
| 116 | * Trace end = 1376592665108210547 |
| 117 | */ |
| 118 | |
| 119 | private static final long rangeStart = 1376592664900000000L; |
| 120 | private static final long rangeEnd = 1376592665000000000L; |
| 121 | |
| 122 | /** |
| 123 | * Test the total number of "real" events. Make sure the lost events aren't |
| 124 | * counted in the total. |
| 125 | */ |
| 126 | @Test |
| 127 | public void testLostEventsTotals() { |
| 128 | long realEvents = fStats.getEventsTotal(); |
| 129 | assertEquals(32300, realEvents); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Test the number of real events in a given range. Lost events shouldn't be |
| 134 | * counted. |
| 135 | */ |
| 136 | @Test |
| 137 | public void testLostEventsTotalInRange() { |
| 138 | long realEventsInRange = fStats.getEventsInRange(rangeStart, rangeEnd); |
| 139 | assertEquals(11209L, realEventsInRange); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Test the total number of lost events reported in the trace. |
| 144 | */ |
| 145 | @Test |
| 146 | public void testLostEventsTypes() { |
| 147 | Map<String, Long> events = fStats.getEventTypesTotal(); |
| 148 | Long lostEvents = events.get(CTFStrings.LOST_EVENT_NAME); |
| 149 | assertEquals(Long.valueOf(967700L), lostEvents); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Test the number of lost events reported in a given range. |
| 154 | */ |
| 155 | @Test |
| 156 | public void testLostEventsTypesInRange() { |
| 157 | Map<String, Long> eventsInRange = fStats.getEventTypesInRange(rangeStart, rangeEnd); |
| 158 | Long lostEventsInRange = eventsInRange.get(CTFStrings.LOST_EVENT_NAME); |
| 159 | assertNotNull(lostEventsInRange); |
| 160 | assertEquals(365752L, lostEventsInRange.longValue()); |
| 161 | } |
| 162 | } |