Commit | Line | Data |
---|---|---|
ce2388e0 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2012, 2014 Ericsson |
ce2388e0 FC |
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 | ||
a4fa4e36 | 15 | import java.io.FileNotFoundException; |
ce2388e0 FC |
16 | import java.text.DateFormat; |
17 | import java.text.SimpleDateFormat; | |
18 | import java.util.Date; | |
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; | |
25 | ||
be6df2d8 | 26 | @SuppressWarnings("javadoc") |
ce2388e0 FC |
27 | public class ReadTrace { |
28 | ||
29 | /** | |
30 | * @param args | |
a4fa4e36 | 31 | * @throws FileNotFoundException |
ce2388e0 | 32 | */ |
a4fa4e36 | 33 | public static void main(String[] args) throws FileNotFoundException { |
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 | 38 | |
a4fa4e36 | 39 | final int LOOP_COUNT = 10; |
ce2388e0 FC |
40 | |
41 | // Work variables | |
6a5251eb | 42 | long nbEvent = 0L; |
3de23137 | 43 | Vector<Double> benchs = new Vector<>(); |
ce2388e0 FC |
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) { | |
a4fa4e36 | 51 | throw new FileNotFoundException(TRACE_PATH); |
ce2388e0 | 52 | } |
ce2388e0 FC |
53 | start = System.nanoTime(); |
54 | if (USE_TEXT) { | |
55 | System.out.println("Event, " + " Time, " + " type, " + " CPU "); | |
56 | } | |
dd9752d5 | 57 | try (CTFTraceReader traceReader = new CTFTraceReader(trace);) { |
a4fa4e36 | 58 | start = System.nanoTime(); |
ce2388e0 | 59 | |
a4fa4e36 MK |
60 | while (traceReader.hasMoreEvents()) { |
61 | EventDefinition ed = traceReader.getCurrentEventDef(); | |
62 | nbEvent++; | |
63 | if (USE_TEXT) { | |
64 | String output = formatDate(ed.getTimestamp() | |
65 | + trace.getOffset()); | |
66 | System.out.println(nbEvent + ", " | |
67 | + output + ", " + ed.getDeclaration().getName() | |
68 | + ", " + ed.getCPU() + ed.getFields().toString()); | |
ce2388e0 | 69 | } |
a4fa4e36 MK |
70 | |
71 | traceReader.advance(); | |
ce2388e0 | 72 | } |
a4fa4e36 | 73 | |
db8e8f7d | 74 | stop = System.nanoTime(); |
bfe038ff | 75 | |
db8e8f7d AM |
76 | System.out.print('.'); |
77 | double time = (stop - start) / (double) nbEvent; | |
78 | benchs.add(time); | |
79 | } catch (CTFReaderException e) { | |
80 | System.out.println("error"); | |
81 | } | |
ce2388e0 FC |
82 | } |
83 | System.out.println(""); | |
84 | double avg = 0; | |
85 | for (Double val : benchs) { | |
86 | avg += val; | |
87 | } | |
88 | avg /= benchs.size(); | |
89 | System.out.println("Time to read " + nbEvent + " events = " + avg | |
bfe038ff | 90 | + " ns/event"); |
ce2388e0 FC |
91 | for (Double val : benchs) { |
92 | System.out.print(val); | |
93 | System.out.print(", "); | |
94 | } | |
ce2388e0 FC |
95 | } |
96 | ||
97 | /** | |
98 | * @param timestamp | |
99 | * the timestamp in UTC to convert to nanoseconds. | |
100 | * @return formatted string. | |
101 | */ | |
102 | private static String formatDate(long timestamp) { | |
103 | Date d = new Date(timestamp / 1000000); | |
4a9c1f07 | 104 | DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss."); |
ce2388e0 FC |
105 | String output = df.format(d) + (timestamp % 1000000000); |
106 | return output; | |
107 | } | |
108 | } |