Improve API.
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / ctfadaptor / headless / Benchmark.java
CommitLineData
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 *******************************************************************************/
12package org.eclipse.linuxtools.tmf.core.tests.ctfadaptor.headless;
13
ce2388e0
FC
14import java.text.DateFormat;
15import java.text.SimpleDateFormat;
16import java.util.Date;
17import java.util.Vector;
18
19import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfIterator;
20import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfEvent;
21import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTrace;
b4f71e4a 22import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
ce2388e0
FC
23
24public class Benchmark {
25
26 /**
27 * @param args
28 */
29 @SuppressWarnings("nls")
25e48683 30 public static void main(final String[] args) {
aa572e22 31 final String TRACE_PATH = "testfiles/kernel";
ce2388e0
FC
32 final int NUM_LOOPS = 100;
33
34 // Change this to enable text output
aa572e22 35 final boolean USE_TEXT = true;
ce2388e0 36
aa572e22
MK
37// try {
38// System.in.read();
39// } catch (final IOException e1) {
40// e1.printStackTrace();
41// }
ce2388e0
FC
42 // Work variables
43 Long nbEvent = 0L;
25e48683 44 final Vector<Double> benchs = new Vector<Double>();
ce2388e0
FC
45 CtfTmfTrace trace = null;
46 long start, stop;
47 for (int loops = 0; loops < NUM_LOOPS; loops++) {
48 nbEvent = 0L;
49 trace = new CtfTmfTrace();
50 try {
25e48683 51 trace.initTrace(null, TRACE_PATH, CtfTmfEvent.class);
b4f71e4a 52 } catch (final TmfTraceException e) {
ce2388e0
FC
53 loops = NUM_LOOPS +1;
54 break;
55 }
56
57 start = System.nanoTime();
58 if (nbEvent != -1) {
25e48683 59 final CtfIterator traceReader = (CtfIterator) trace.seekEvent(0);
ce2388e0
FC
60
61 start = System.nanoTime();
62 CtfTmfEvent current = traceReader.getCurrentEvent();
63 while (current != null) {
64 nbEvent++;
65 if (USE_TEXT) {
aa572e22 66
ce2388e0 67 System.out.println("Event " + traceReader.getRank() + " Time "
aa572e22
MK
68 + current.getTimestamp().toString() + " type " + current.getEventName()
69 + " on CPU " + current.getSource() + " " + current.getContent().toString()) ;
ce2388e0
FC
70 }
71 traceReader.advance();
72 current = traceReader.getCurrentEvent();
73 }
74 }
75 stop = System.nanoTime();
76 System.out.print('.');
25e48683 77 final double time = (stop - start) / (double) nbEvent;
ce2388e0
FC
78 benchs.add(time);
79 }
80 System.out.println("");
81 double avg = 0;
aa572e22 82 for (final Double val : benchs) {
ce2388e0 83 avg += val;
aa572e22 84 }
ce2388e0
FC
85 avg /= benchs.size();
86 System.out.println("Time to read = " + avg + " events/ns");
25e48683 87 for (final Double val : benchs) {
ce2388e0
FC
88 System.out.print(val);
89 System.out.print(", ");
90 }
91
92 }
93
94 /**
95 * @param timestamp
96 * the timestamp in UTC to convert to nanoseconds.
97 * @return formatted string.
98 */
25e48683
FC
99 private static String formatDate(final long timestamp) {
100 final Date d = new Date(timestamp / 1000000);
101 final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss."); //$NON-NLS-1$
102 final String output = df.format(d) + (timestamp % 1000000000);
ce2388e0
FC
103 return output;
104 }
105
106}
This page took 0.032353 seconds and 5 git commands to generate.