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