~ubuntu-branches/ubuntu/trusty/eclipse-linuxtools/trusty

« back to all changes in this revision

Viewing changes to lttng/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/headless/ReadTrace.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2012-06-29 12:07:30 UTC
  • Revision ID: package-import@ubuntu.com-20120629120730-bfri1xys1i71dpn6
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
13
package org.eclipse.linuxtools.ctf.core.tests.headless;
 
14
 
 
15
import java.text.DateFormat;
 
16
import java.text.SimpleDateFormat;
 
17
import java.util.Date;
 
18
import java.util.Map;
 
19
import java.util.Vector;
 
20
 
 
21
import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
 
22
import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
 
23
import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
 
24
import org.eclipse.linuxtools.ctf.core.trace.CTFTraceReader;
 
25
import org.eclipse.linuxtools.internal.ctf.core.trace.Stream;
 
26
 
 
27
@SuppressWarnings("javadoc")
 
28
public class ReadTrace {
 
29
 
 
30
    /**
 
31
     * @param args
 
32
     */
 
33
    @SuppressWarnings("nls")
 
34
    public static void main(String[] args) {
 
35
        final String TRACE_PATH = "traces/kernel";
 
36
 
 
37
        // Change this to enable text output
 
38
        final boolean USE_TEXT = false;
 
39
 
 
40
        final int LOOP_COUNT = 1;
 
41
 
 
42
        // Work variables
 
43
        Long nbEvent = 0L;
 
44
        Vector<Double> benchs = new Vector<Double>();
 
45
        CTFTrace trace = null;
 
46
        long start, stop;
 
47
        for (int loops = 0; loops < LOOP_COUNT; loops++) {
 
48
            try {
 
49
                nbEvent = 0L;
 
50
                trace = new CTFTrace(TRACE_PATH);
 
51
            } catch (CTFReaderException e) {
 
52
                // do nothing
 
53
            }
 
54
            long prev = -1;
 
55
            start = System.nanoTime();
 
56
            if (USE_TEXT) {
 
57
                System.out.println("Event, " + " Time, " + " type, " + " CPU ");
 
58
            }
 
59
            if (trace != null) {
 
60
                CTFTraceReader traceReader = new CTFTraceReader(trace);
 
61
 
 
62
                start = System.nanoTime();
 
63
 
 
64
                while (traceReader.hasMoreEvents()) {
 
65
                    EventDefinition ed = traceReader.getCurrentEventDef();
 
66
                    nbEvent++;
 
67
                    if (USE_TEXT) {
 
68
                        String output = formatDate(ed.getTimestamp()
 
69
                                + trace.getOffset());
 
70
                        System.out.println(nbEvent + ", "
 
71
                                + output + ", " + ed.getDeclaration().getName()
 
72
                                + ", " + ed.getCPU() + ed.getFields().toString()) ;
 
73
                    }
 
74
                    long endTime = traceReader.getEndTime();
 
75
                    long timestamp = traceReader.getCurrentEventDef().getTimestamp();
 
76
                    traceReader.advance();
 
77
                }
 
78
                Map<Long, Stream> streams = traceReader.getTrace().getStreams();
 
79
            }
 
80
            stop = System.nanoTime();
 
81
 
 
82
            System.out.print('.');
 
83
            double time = (stop - start) / (double) nbEvent;
 
84
            benchs.add(time);
 
85
        }
 
86
        System.out.println("");
 
87
        double avg = 0;
 
88
        for (Double val : benchs) {
 
89
            avg += val;
 
90
        }
 
91
        avg /= benchs.size();
 
92
        System.out.println("Time to read " + nbEvent + " events = " + avg
 
93
                + " ns/event");
 
94
        for (Double val : benchs) {
 
95
            System.out.print(val);
 
96
            System.out.print(", ");
 
97
        }
 
98
    }
 
99
 
 
100
    /**
 
101
     * @param timestamp
 
102
     *            the timestamp in UTC to convert to nanoseconds.
 
103
     * @return formatted string.
 
104
     */
 
105
    private static String formatDate(long timestamp) {
 
106
        Date d = new Date(timestamp / 1000000);
 
107
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss."); //$NON-NLS-1$
 
108
        String output = df.format(d) + (timestamp % 1000000000);
 
109
        return output;
 
110
    }
 
111
}