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