Commit | Line | Data |
---|---|---|
ce2388e0 FC |
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 | * Matthew Khouzam - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.ctf.core.tests.headless; | |
14 | ||
15 | import java.text.DateFormat; | |
16 | import java.text.SimpleDateFormat; | |
17 | import java.util.Date; | |
18 | import java.util.Vector; | |
19 | ||
20 | import org.eclipse.linuxtools.ctf.core.event.EventDefinition; | |
21 | import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; | |
22 | import org.eclipse.linuxtools.ctf.core.trace.CTFTrace; | |
23 | import org.eclipse.linuxtools.ctf.core.trace.CTFTraceReader; | |
24 | ||
25 | public class ReadTrace { | |
26 | ||
27 | /** | |
28 | * @param args | |
29 | */ | |
30 | @SuppressWarnings("nls") | |
31 | public static void main(String[] args) { | |
fd74e6c1 | 32 | final String TRACE_PATH = "/home/ematkho/lttng-traces/lt-hello-4175-20120405-092230"; |
ce2388e0 FC |
33 | |
34 | // Change this to enable text output | |
fd74e6c1 | 35 | final boolean USE_TEXT = true; |
ce2388e0 FC |
36 | |
37 | final int LOOP_COUNT = 1; | |
38 | ||
39 | // Work variables | |
40 | Long nbEvent = 0L; | |
41 | Vector<Double> benchs = new Vector<Double>(); | |
42 | CTFTrace trace = null; | |
43 | long start, stop; | |
44 | for (int loops = 0; loops < LOOP_COUNT; loops++) { | |
45 | try { | |
46 | nbEvent = 0L; | |
47 | trace = new CTFTrace(TRACE_PATH); | |
48 | } catch (CTFReaderException e) { | |
49 | // do nothing | |
50 | } | |
51 | long prev = -1; | |
52 | start = System.nanoTime(); | |
53 | if (USE_TEXT) { | |
54 | System.out.println("Event, " + " Time, " + " type, " + " CPU "); | |
55 | } | |
56 | if (trace != null) { | |
57 | CTFTraceReader traceReader = new CTFTraceReader(trace); | |
58 | ||
59 | start = System.nanoTime(); | |
60 | ||
61 | while (traceReader.hasMoreEvents()) { | |
62 | EventDefinition ed = traceReader.getCurrentEventDef(); | |
63 | nbEvent++; | |
64 | if (prev == traceReader.getIndex()) { | |
65 | System.out.println("Error on events " + prev); | |
66 | } | |
67 | prev = traceReader.getIndex(); | |
68 | if (USE_TEXT) { | |
69 | String output = formatDate(ed.timestamp | |
70 | + trace.getOffset()); | |
71 | System.out.println(traceReader.getIndex() + ", " | |
72 | + output + ", " + ed.getDeclaration().getName() | |
fd74e6c1 | 73 | + ", " + ed.getCPU() + ed.getFields().toString()) ; |
ce2388e0 FC |
74 | } |
75 | ||
76 | traceReader.advance(); | |
77 | } | |
78 | } | |
79 | stop = System.nanoTime(); | |
80 | System.out.print('.'); | |
81 | double time = (stop - start) / (double) nbEvent; | |
82 | benchs.add(time); | |
83 | } | |
84 | System.out.println(""); | |
85 | double avg = 0; | |
86 | for (Double val : benchs) { | |
87 | avg += val; | |
88 | } | |
89 | avg /= benchs.size(); | |
90 | System.out.println("Time to read " + nbEvent + " events = " + avg | |
91 | + " events/ns"); | |
92 | for (Double val : benchs) { | |
93 | System.out.print(val); | |
94 | System.out.print(", "); | |
95 | } | |
79cb3749 AM |
96 | testSeekIndex(trace); |
97 | testSeekIndex(trace); | |
ce2388e0 FC |
98 | } |
99 | ||
100 | /** | |
101 | * @return | |
102 | */ | |
103 | private static long getTimestamp(CTFTraceReader fixture) { | |
104 | if (fixture.getCurrentEventDef() != null) { | |
105 | return fixture.getCurrentEventDef().timestamp; | |
106 | } | |
107 | return Long.MIN_VALUE; | |
108 | } | |
109 | ||
79cb3749 | 110 | public static void testSeekIndex(CTFTrace trace) { |
ce2388e0 FC |
111 | CTFTraceReader fixture = new CTFTraceReader(trace); |
112 | long rank = 300000L; | |
113 | long timeRank = 4281275394331L; | |
114 | long nearEnd = 4287422858132L; | |
115 | long seekTime_0; | |
116 | long seekIndex_0 = 0; | |
117 | long seekNext_300000 = 0; | |
118 | long seekIndex_300000 = 0; | |
119 | long seekTime_300000 = 0; | |
120 | String cr = "\n"; //$NON-NLS-1$ | |
121 | fixture.seek(0); | |
122 | for (int i = 0; i < 100; i++) { | |
123 | fixture.advance(); | |
124 | } | |
125 | ||
126 | fixture.seek(nearEnd); | |
127 | /* | |
128 | * we need to read the trace before seeking | |
129 | */ | |
130 | fixture.seek(0); | |
131 | seekTime_0 = getTimestamp(fixture); | |
132 | for (int i = 0; i < rank; i++) { | |
133 | fixture.advance(); | |
134 | } | |
135 | seekNext_300000 = getTimestamp(fixture); | |
136 | fixture.seek(timeRank); | |
137 | seekTime_300000 = getTimestamp(fixture); | |
138 | fixture.seekIndex(0); | |
139 | seekIndex_0 = getTimestamp(fixture); | |
140 | ||
141 | fixture.seekIndex(rank); | |
142 | seekIndex_300000 = getTimestamp(fixture); | |
143 | System.out.print(cr); | |
144 | System.out.println("seek(0) " + seekTime_0 + cr + //$NON-NLS-1$ | |
145 | "seekIndex(0) " + seekIndex_0 + cr + //$NON-NLS-1$ | |
146 | "Next(300000) " + seekNext_300000 + cr + //$NON-NLS-1$ | |
147 | "seek(time(300000)) " + seekTime_300000 + cr + //$NON-NLS-1$ | |
148 | "seekIndex(300000) " + seekIndex_300000 //$NON-NLS-1$ | |
149 | ); | |
150 | } | |
151 | ||
152 | /** | |
153 | * @param timestamp | |
154 | * the timestamp in UTC to convert to nanoseconds. | |
155 | * @return formatted string. | |
156 | */ | |
157 | private static String formatDate(long timestamp) { | |
158 | Date d = new Date(timestamp / 1000000); | |
159 | DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss."); //$NON-NLS-1$ | |
160 | String output = df.format(d) + (timestamp % 1000000000); | |
161 | return output; | |
162 | } | |
163 | } |