Commit | Line | Data |
---|---|---|
0f2a4641 MK |
1 | /******************************************************************************* |
2 | * Copyright (c) 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 | * Matthew Khouzam - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.tracecompass.ctf.core.tests.trace; | |
14 | ||
15 | import static org.junit.Assert.assertEquals; | |
16 | import static org.junit.Assert.fail; | |
17 | import static org.junit.Assume.assumeTrue; | |
18 | ||
19 | import java.util.ArrayList; | |
20 | import java.util.List; | |
d291a715 | 21 | import java.util.concurrent.TimeUnit; |
0f2a4641 | 22 | |
680f9173 | 23 | import org.eclipse.tracecompass.ctf.core.CTFException; |
0f2a4641 | 24 | import org.eclipse.tracecompass.ctf.core.CTFStrings; |
e8ece272 | 25 | import org.eclipse.tracecompass.ctf.core.event.IEventDefinition; |
0f2a4641 | 26 | import org.eclipse.tracecompass.ctf.core.event.types.IntegerDefinition; |
c4d57ac1 | 27 | import org.eclipse.tracecompass.ctf.core.tests.shared.CtfTestTraceUtils; |
0f2a4641 | 28 | import org.eclipse.tracecompass.ctf.core.trace.CTFTraceReader; |
c4d57ac1 | 29 | import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace; |
0f2a4641 MK |
30 | import org.junit.Rule; |
31 | import org.junit.Test; | |
32 | import org.junit.rules.TestRule; | |
33 | import org.junit.rules.Timeout; | |
34 | import org.junit.runner.RunWith; | |
35 | import org.junit.runners.Parameterized; | |
36 | import org.junit.runners.Parameterized.Parameters; | |
37 | ||
38 | /** | |
39 | * Read all the traces and verify some metrics. Nominally the event count and | |
40 | * the duration of the trace (not the time to parse it). | |
41 | * | |
42 | * @author Matthew Khouzam | |
43 | */ | |
44 | @RunWith(Parameterized.class) | |
45 | public class TraceReadAllTracesTest { | |
46 | ||
47 | /** Time-out tests after 20 seconds. */ | |
48 | @Rule | |
d291a715 | 49 | public TestRule globalTimeout = new Timeout(20, TimeUnit.SECONDS); |
0f2a4641 MK |
50 | |
51 | /** | |
52 | * Get the list of traces | |
53 | * | |
54 | * @return the list of traces | |
55 | */ | |
56 | @Parameters(name = "{index}: {0}") | |
57 | public static Iterable<Object[]> getTracePaths() { | |
58 | CtfTestTrace[] values = CtfTestTrace.values(); | |
59 | List<Object[]> list = new ArrayList<>(); | |
60 | for (CtfTestTrace value : values) { | |
61 | list.add(new Object[] { value.name(), value }); | |
62 | } | |
63 | return list; | |
64 | } | |
65 | ||
66 | private final CtfTestTrace fTraceEnum; | |
67 | ||
68 | /** | |
69 | * Constructor | |
70 | * | |
71 | * @param name | |
72 | * name of the enum | |
73 | * | |
74 | * @param traceEnum | |
75 | * the enum to test | |
76 | */ | |
77 | public TraceReadAllTracesTest(String name, CtfTestTrace traceEnum) { | |
78 | fTraceEnum = traceEnum; | |
79 | } | |
80 | ||
81 | /** | |
82 | * Reads all the traces | |
83 | */ | |
84 | @Test | |
85 | public void readTraces() { | |
86 | if (fTraceEnum.getNbEvents() != -1) { | |
c4d57ac1 | 87 | try (CTFTraceReader reader = new CTFTraceReader(CtfTestTraceUtils.getTrace(fTraceEnum))) { |
e8ece272 | 88 | IEventDefinition currentEventDef = reader.getCurrentEventDef(); |
0f2a4641 MK |
89 | double start = currentEventDef.getTimestamp(); |
90 | long count = 0; | |
91 | double end = start; | |
92 | while (reader.hasMoreEvents()) { | |
93 | reader.advance(); | |
94 | count++; | |
95 | currentEventDef = reader.getCurrentEventDef(); | |
96 | if (currentEventDef != null) { | |
97 | end = currentEventDef.getTimestamp(); | |
98 | if (currentEventDef.getDeclaration().getName().equals(CTFStrings.LOST_EVENT_NAME)) { | |
99 | count += ((IntegerDefinition) currentEventDef.getFields().getDefinition(CTFStrings.LOST_EVENTS_FIELD)).getValue() - 1; | |
100 | } | |
101 | } | |
102 | } | |
103 | assertEquals("Event count", fTraceEnum.getNbEvents(), count); | |
104 | assertEquals("Trace duration", fTraceEnum.getDuration(), (end - start) / 1000000000.0, 1.0); | |
680f9173 | 105 | } catch (CTFException e) { |
c4d57ac1 | 106 | fail(fTraceEnum.name() + " " + e.getMessage()); |
0f2a4641 MK |
107 | } |
108 | } else { | |
109 | assumeTrue("Trace did not specify events count", false); | |
110 | } | |
111 | } | |
112 | } |