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