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; | |
aa3b05ef | 18 | import java.util.Map; |
ce2388e0 FC |
19 | import java.util.Vector; |
20 | ||
21 | import org.eclipse.linuxtools.ctf.core.event.EventDefinition; | |
22 | import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; | |
23 | import org.eclipse.linuxtools.ctf.core.trace.CTFTrace; | |
24 | import org.eclipse.linuxtools.ctf.core.trace.CTFTraceReader; | |
aa3b05ef | 25 | import org.eclipse.linuxtools.internal.ctf.core.trace.Stream; |
ce2388e0 | 26 | |
be6df2d8 | 27 | @SuppressWarnings("javadoc") |
ce2388e0 FC |
28 | public class ReadTrace { |
29 | ||
30 | /** | |
31 | * @param args | |
32 | */ | |
33 | @SuppressWarnings("nls") | |
34 | public static void main(String[] args) { | |
aa572e22 | 35 | final String TRACE_PATH = "traces/kernel"; |
ce2388e0 FC |
36 | |
37 | // Change this to enable text output | |
bfe038ff | 38 | final boolean USE_TEXT = false; |
ce2388e0 FC |
39 | |
40 | final int LOOP_COUNT = 1; | |
41 | ||
42 | // Work variables | |
43 | Long nbEvent = 0L; | |
44 | Vector<Double> benchs = new Vector<Double>(); | |
45 | CTFTrace trace = null; | |
46 | long start, stop; | |
47 | for (int loops = 0; loops < LOOP_COUNT; loops++) { | |
48 | try { | |
49 | nbEvent = 0L; | |
50 | trace = new CTFTrace(TRACE_PATH); | |
51 | } catch (CTFReaderException e) { | |
52 | // do nothing | |
53 | } | |
aa3b05ef FC |
54 | @SuppressWarnings("unused") |
55 | long prev = -1; | |
ce2388e0 FC |
56 | start = System.nanoTime(); |
57 | if (USE_TEXT) { | |
58 | System.out.println("Event, " + " Time, " + " type, " + " CPU "); | |
59 | } | |
60 | if (trace != null) { | |
61 | CTFTraceReader traceReader = new CTFTraceReader(trace); | |
62 | ||
63 | start = System.nanoTime(); | |
64 | ||
65 | while (traceReader.hasMoreEvents()) { | |
66 | EventDefinition ed = traceReader.getCurrentEventDef(); | |
67 | nbEvent++; | |
ce2388e0 | 68 | if (USE_TEXT) { |
aa572e22 | 69 | String output = formatDate(ed.getTimestamp() |
ce2388e0 | 70 | + trace.getOffset()); |
bfe038ff | 71 | System.out.println(nbEvent + ", " |
ce2388e0 | 72 | + output + ", " + ed.getDeclaration().getName() |
fd74e6c1 | 73 | + ", " + ed.getCPU() + ed.getFields().toString()) ; |
ce2388e0 | 74 | } |
aa3b05ef FC |
75 | @SuppressWarnings("unused") |
76 | long endTime = traceReader.getEndTime(); | |
77 | @SuppressWarnings("unused") | |
78 | long timestamp = traceReader.getCurrentEventDef().getTimestamp(); | |
ce2388e0 FC |
79 | traceReader.advance(); |
80 | } | |
aa3b05ef FC |
81 | @SuppressWarnings("unused") |
82 | Map<Long, Stream> streams = traceReader.getTrace().getStreams(); | |
ce2388e0 FC |
83 | } |
84 | stop = System.nanoTime(); | |
bfe038ff | 85 | |
ce2388e0 FC |
86 | System.out.print('.'); |
87 | double time = (stop - start) / (double) nbEvent; | |
88 | benchs.add(time); | |
89 | } | |
90 | System.out.println(""); | |
91 | double avg = 0; | |
92 | for (Double val : benchs) { | |
93 | avg += val; | |
94 | } | |
95 | avg /= benchs.size(); | |
96 | System.out.println("Time to read " + nbEvent + " events = " + avg | |
bfe038ff | 97 | + " ns/event"); |
ce2388e0 FC |
98 | for (Double val : benchs) { |
99 | System.out.print(val); | |
100 | System.out.print(", "); | |
101 | } | |
ce2388e0 FC |
102 | } |
103 | ||
104 | /** | |
105 | * @param timestamp | |
106 | * the timestamp in UTC to convert to nanoseconds. | |
107 | * @return formatted string. | |
108 | */ | |
109 | private static String formatDate(long timestamp) { | |
110 | Date d = new Date(timestamp / 1000000); | |
111 | DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss."); //$NON-NLS-1$ | |
112 | String output = df.format(d) + (timestamp % 1000000000); | |
113 | return output; | |
114 | } | |
115 | } |