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