| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2012, 2013 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.tmf.core.tests.ctfadaptor.headless; |
| 14 | |
| 15 | import java.util.Vector; |
| 16 | |
| 17 | import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfEvent; |
| 18 | import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfContext; |
| 19 | import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTrace; |
| 20 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; |
| 21 | |
| 22 | /** |
| 23 | * Test and benchmark reading a CTF LTTng kernel trace. |
| 24 | * |
| 25 | * @author Matthew Khouzam |
| 26 | */ |
| 27 | public class Benchmark { |
| 28 | |
| 29 | /** |
| 30 | * Run the benchmark. |
| 31 | * |
| 32 | * @param args The command-line arguments |
| 33 | */ |
| 34 | public static void main(final String[] args) { |
| 35 | final String TRACE_PATH = "testfiles/kernel"; |
| 36 | final int NUM_LOOPS = 100; |
| 37 | |
| 38 | // Change this to enable text output |
| 39 | final boolean USE_TEXT = true; |
| 40 | |
| 41 | // Work variables |
| 42 | Long nbEvent = 0L; |
| 43 | final Vector<Double> benchs = new Vector<>(); |
| 44 | CtfTmfTrace trace = null; |
| 45 | long start, stop; |
| 46 | for (int loops = 0; loops < NUM_LOOPS; loops++) { |
| 47 | nbEvent = 0L; |
| 48 | trace = new CtfTmfTrace(); |
| 49 | try { |
| 50 | trace.initTrace(null, TRACE_PATH, CtfTmfEvent.class); |
| 51 | } catch (final TmfTraceException e) { |
| 52 | loops = NUM_LOOPS +1; |
| 53 | break; |
| 54 | } |
| 55 | |
| 56 | start = System.nanoTime(); |
| 57 | if (nbEvent != -1) { |
| 58 | final CtfTmfContext traceReader = (CtfTmfContext) trace.seekEvent(0); |
| 59 | |
| 60 | start = System.nanoTime(); |
| 61 | CtfTmfEvent current = traceReader.getCurrentEvent(); |
| 62 | while (current != null) { |
| 63 | nbEvent++; |
| 64 | if (USE_TEXT) { |
| 65 | |
| 66 | System.out.println("Event " + nbEvent + " Time " |
| 67 | + current.getTimestamp().toString() + " type " + current.getEventName() |
| 68 | + " on CPU " + current.getSource() + " " + current.getContent().toString()); |
| 69 | } |
| 70 | // advance the trace to the next event. |
| 71 | boolean hasMore = traceReader.advance(); |
| 72 | if( hasMore ){ |
| 73 | // you can know the trace has more events. |
| 74 | } |
| 75 | current = traceReader.getCurrentEvent(); |
| 76 | } |
| 77 | } |
| 78 | stop = System.nanoTime(); |
| 79 | System.out.print('.'); |
| 80 | final double time = (stop - start) / (double) nbEvent; |
| 81 | benchs.add(time); |
| 82 | } |
| 83 | System.out.println(""); |
| 84 | double avg = 0; |
| 85 | for (final Double val : benchs) { |
| 86 | avg += val; |
| 87 | } |
| 88 | avg /= benchs.size(); |
| 89 | System.out.println("Time to read = " + avg + " events/ns"); |
| 90 | for (final Double val : benchs) { |
| 91 | System.out.print(val); |
| 92 | System.out.print(", "); |
| 93 | } |
| 94 | |
| 95 | } |
| 96 | |
| 97 | } |