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