ctf: Make events immutable
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core.tests / src / org / eclipse / linuxtools / ctf / core / tests / headless / ReadTrace.java
index 07cdfbfe2596a4cabb72e08e0ebc3758cc6a9c28..2541d9da60231ea6d02ec2d132201e60f87648b8 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2012 Ericsson
+ * Copyright (c) 2012, 2014 Ericsson
  *
  * All rights reserved. This program and the accompanying materials are
  * made available under the terms of the Eclipse Public License v1.0 which
 
 package org.eclipse.linuxtools.ctf.core.tests.headless;
 
+import java.io.FileNotFoundException;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.util.Date;
+import java.util.Vector;
 
 import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
 import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
 import org.eclipse.linuxtools.ctf.core.trace.CTFTraceReader;
 
+@SuppressWarnings("javadoc")
 public class ReadTrace {
 
     /**
      * @param args
+     * @throws FileNotFoundException
      */
-    @SuppressWarnings("nls")
-    public static void main(String[] args) {
-        final String TRACE_PATH = "Tests/traces/trace20m1";
+    public static void main(String[] args) throws FileNotFoundException {
+        final String TRACE_PATH = "traces/kernel";
 
         // Change this to enable text output
-        final boolean USE_TEXT = true;
+        final boolean USE_TEXT = false;
+
+        final int LOOP_COUNT = 10;
 
         // Work variables
-        Long nbEvent = 0L;
+        long nbEvent = 0L;
+        Vector<Double> benchs = new Vector<>();
         CTFTrace trace = null;
-        try {
-            trace = new CTFTrace(TRACE_PATH);
-        } catch (CTFReaderException e) {
-
-            nbEvent = (long) -1;
-        }
         long start, stop;
-        start = System.nanoTime();
-        if (nbEvent != -1) {
-            CTFTraceReader traceReader = new CTFTraceReader(trace);
-
+        for (int loops = 0; loops < LOOP_COUNT; loops++) {
+            try {
+                nbEvent = 0L;
+                trace = new CTFTrace(TRACE_PATH);
+            } catch (CTFReaderException e) {
+                throw new FileNotFoundException(TRACE_PATH);
+            }
             start = System.nanoTime();
-            while (traceReader.hasMoreEvents()) {
-                EventDefinition ed = traceReader.getCurrentEventDef();
-                nbEvent++;
-                if (USE_TEXT) {
-                    String output = formatDate(ed.timestamp + trace.getOffset());
-                    System.out.println("Event " + nbEvent + " Time " + output
-                            + " type " + ed.getDeclaration().getName()
-                            + " on CPU " + ed.getCPU());
+            if (USE_TEXT) {
+                System.out.println("Event, " + " Time, " + " type, " + " CPU ");
+            }
+            try (CTFTraceReader traceReader = new CTFTraceReader(trace);) {
+                start = System.nanoTime();
+
+                while (traceReader.hasMoreEvents()) {
+                    EventDefinition ed = traceReader.getCurrentEventDef();
+                    nbEvent++;
+                    if (USE_TEXT) {
+                        String output = formatDate(ed.getTimestamp()
+                                + trace.getOffset());
+                        System.out.println(nbEvent + ", "
+                                + output + ", " + ed.getDeclaration().getName()
+                                + ", " + ed.getCPU() + ed.getFields().toString());
+                    }
+
+                    traceReader.advance();
                 }
-                traceReader.advance();
+
+                stop = System.nanoTime();
+
+                System.out.print('.');
+                double time = (stop - start) / (double) nbEvent;
+                benchs.add(time);
+            } catch (CTFReaderException e) {
+                System.out.println("error");
             }
         }
-        stop = System.nanoTime();
-        System.out.println("Time taken for " + nbEvent + " events " + (stop - start)
-                + "ns ");
-        System.out.println(((stop - start) / nbEvent) + "ns/event ");
+        System.out.println("");
+        double avg = 0;
+        for (Double val : benchs) {
+            avg += val;
+        }
+        avg /= benchs.size();
+        System.out.println("Time to read " + nbEvent + " events = " + avg
+                + " ns/event");
+        for (Double val : benchs) {
+            System.out.print(val);
+            System.out.print(", ");
+        }
     }
 
     /**
-     * @param timestamp the timestamp in UTC to convert to nanoseconds.
+     * @param timestamp
+     *            the timestamp in UTC to convert to nanoseconds.
      * @return formatted string.
      */
     private static String formatDate(long timestamp) {
         Date d = new Date(timestamp / 1000000);
-        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss."); //$NON-NLS-1$
+        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.");
         String output = df.format(d) + (timestamp % 1000000000);
         return output;
     }
This page took 0.025175 seconds and 5 git commands to generate.