| 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; |
| 15 | import java.io.IOException; |
| 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.tmf.core.ctfadaptor.CtfIterator; |
| 22 | import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfEvent; |
| 23 | import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTrace; |
| 24 | |
| 25 | public class Benchmark { |
| 26 | |
| 27 | /** |
| 28 | * @param args |
| 29 | */ |
| 30 | @SuppressWarnings("nls") |
| 31 | public static void main(final String[] args) { |
| 32 | final String TRACE_PATH = "../org.eclipse.linuxtools.ctf.core.tests/Tests/traces/trace20m1"; |
| 33 | final int NUM_LOOPS = 100; |
| 34 | |
| 35 | // Change this to enable text output |
| 36 | final boolean USE_TEXT = false; |
| 37 | |
| 38 | try { |
| 39 | System.in.read(); |
| 40 | } catch (final IOException e1) { |
| 41 | e1.printStackTrace(); |
| 42 | } |
| 43 | // Work variables |
| 44 | Long nbEvent = 0L; |
| 45 | final 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(null, TRACE_PATH, CtfTmfEvent.class); |
| 53 | } catch (final FileNotFoundException e) { |
| 54 | loops = NUM_LOOPS +1; |
| 55 | break; |
| 56 | } |
| 57 | |
| 58 | start = System.nanoTime(); |
| 59 | if (nbEvent != -1) { |
| 60 | final 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 | final 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 | final double time = (stop - start) / (double) nbEvent; |
| 79 | benchs.add(time); |
| 80 | } |
| 81 | System.out.println(""); |
| 82 | double avg = 0; |
| 83 | for (final Double val : benchs) |
| 84 | avg += val; |
| 85 | avg /= benchs.size(); |
| 86 | System.out.println("Time to read = " + avg + " events/ns"); |
| 87 | for (final Double val : benchs) { |
| 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 | */ |
| 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); |
| 103 | return output; |
| 104 | } |
| 105 | |
| 106 | } |