changeset 1018:9c1834169bb1

7901060: LaTeX output format.
author shade
date Fri, 10 Oct 2014 18:05:59 +0400
parents 243f511255ef
children 1a4b13657050
files jmh-core/src/main/java/org/openjdk/jmh/results/format/LaTeXResultFormat.java jmh-core/src/main/java/org/openjdk/jmh/results/format/ResultFormatFactory.java jmh-core/src/main/java/org/openjdk/jmh/results/format/ResultFormatType.java
diffstat 3 files changed, 109 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jmh-core/src/main/java/org/openjdk/jmh/results/format/LaTeXResultFormat.java	Fri Oct 10 18:05:59 2014 +0400
@@ -0,0 +1,106 @@
+/*
+ * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package org.openjdk.jmh.results.format;
+
+import org.openjdk.jmh.infra.BenchmarkParams;
+import org.openjdk.jmh.results.Result;
+import org.openjdk.jmh.results.RunResult;
+import org.openjdk.jmh.util.ClassUtils;
+
+import java.io.PrintWriter;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.SortedSet;
+import java.util.TreeSet;
+
+class LaTeXResultFormat implements ResultFormat {
+
+    private final PrintWriter pw;
+
+    public LaTeXResultFormat(PrintWriter pw) {
+        this.pw = pw;
+    }
+
+    @Override
+    public void writeOut(Collection<RunResult> results) {
+
+        SortedSet<String> params = new TreeSet<String>();
+        Set<String> benchNames = new HashSet<String>();
+
+        Set<String> units = new HashSet<String>();
+        for (RunResult rs : results) {
+            params.addAll(rs.getParams().getParamsKeys());
+            benchNames.add(rs.getParams().getBenchmark());
+            units.add(rs.getPrimaryResult().getScoreUnit());
+        }
+
+        boolean singleUnit = (units.size() == 1);
+        String unit = singleUnit ? units.iterator().next() : null;
+
+        Map<String, String> benchPrefixes = ClassUtils.denseClassNames(benchNames);
+
+        pw.write("\\begin{tabular}{r|");
+        for (String p : params) {
+            pw.write("l|");
+        }
+        pw.write("rl" + (singleUnit ? "" : "l") + "}\n");
+        pw.write(" \\multicolumn{1}{c|}{\\texttt{Benchmark}} & ");
+        for (String p : params) {
+            pw.printf("\\texttt{%s}", p);
+            pw.write(" & ");
+        }
+        pw.write(" \\multicolumn{" + (singleUnit ? 2 : 3) + "}{c}{\\texttt{Score" + (singleUnit ? ", " + unit : "") + "}} \\\\\n");
+        pw.write("\\hline\n");
+
+        for (RunResult runResult : results) {
+            BenchmarkParams benchmarkParams = runResult.getParams();
+            Result primaryResult = runResult.getPrimaryResult();
+
+            pw.printf("\\texttt{%s} & ", escape(benchPrefixes.get(benchmarkParams.getBenchmark())));
+            for (String p : params) {
+                pw.printf("\\texttt{%s}", escape(benchmarkParams.getParam(p)));
+                pw.write(" & ");
+            }
+            pw.printf("\\texttt{%5.3f} & ", primaryResult.getScore());
+            pw.printf("\\scriptsize $\\pm$ \\texttt{%5.3f}", primaryResult.getScoreError());
+            if (!singleUnit) {
+                pw.printf(" & \\texttt{%s}", primaryResult.getScoreUnit());
+            }
+            pw.write(" \\\\");
+
+            pw.write("\n");
+        }
+
+        pw.write("\\end{tabular}");
+
+    }
+
+    private static String escape(String s) {
+        return s.replaceAll("_", "\\\\_");
+    }
+
+}
--- a/jmh-core/src/main/java/org/openjdk/jmh/results/format/ResultFormatFactory.java	Wed Oct 01 22:02:39 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/results/format/ResultFormatFactory.java	Fri Oct 10 18:05:59 2014 +0400
@@ -84,6 +84,8 @@
                 return new XSVResultFormat(writer, ";");
             case JSON:
                 return new JSONResultFormat(writer);
+            case LATEX:
+                return new LaTeXResultFormat(writer);
             default:
                 throw new IllegalStateException("Unsupported result format: " + type);
         }
--- a/jmh-core/src/main/java/org/openjdk/jmh/results/format/ResultFormatType.java	Wed Oct 01 22:02:39 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/results/format/ResultFormatType.java	Fri Oct 10 18:05:59 2014 +0400
@@ -30,5 +30,6 @@
     CSV,
     SCSV,
     JSON,
+    LATEX,
 
 }