changeset 213:d31f1f5e996c

Very basic JSON result formatter.
author shade
date Thu, 24 Oct 2013 17:45:12 +0400
parents c1c8be353c1c
children 9ae9f86bd55a
files jmh-core/src/main/java/org/openjdk/jmh/output/results/JSONResultFormat.java jmh-core/src/main/java/org/openjdk/jmh/output/results/ResultFormatFactory.java jmh-core/src/main/java/org/openjdk/jmh/output/results/ResultFormatType.java
diffstat 3 files changed, 92 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/output/results/JSONResultFormat.java	Thu Oct 24 17:45:12 2013 +0400
@@ -0,0 +1,89 @@
+/*
+ * Copyright (c) 2005, 2013, 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.output.results;
+
+import org.openjdk.jmh.logic.results.Result;
+import org.openjdk.jmh.logic.results.RunResult;
+import org.openjdk.jmh.runner.BenchmarkRecord;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.Map;
+
+public class JSONResultFormat implements ResultFormat {
+
+    private final String output;
+
+    public JSONResultFormat(String output) {
+        this.output = output;
+    }
+
+    @Override
+    public void writeOut(Map<BenchmarkRecord, RunResult> results) {
+        PrintWriter pw = null;
+        try  {
+            pw = new PrintWriter(output);
+
+            boolean first = true;
+
+            pw.println("[");
+            for (BenchmarkRecord br : results.keySet()) {
+                if (first) {
+                    first = false;
+                } else {
+                    pw.println(",");
+                }
+
+                RunResult runResult = results.get(br);
+
+                pw.println("{");
+                pw.println("  'benchmark' : '" + br.getUsername() + "',");
+                pw.println("  'mode' : '" + br.getMode().shortLabel() + "',");
+                pw.println("  'threads' : " + runResult.getThreads() + ",");
+                pw.println("  'iterations' : " + runResult.getIterationCount() + ",");
+                pw.println("  'iterationTime' : '" + runResult.getTime() + "',");
+                pw.println("  'primaryMetric' : {");
+                pw.println("    'score' : " + runResult.getPrimaryResult().getScore() + ",");
+                pw.println("    'scoreUnit' : '" + runResult.getPrimaryResult().getScoreUnit() + "',");
+                pw.println("    'raw' : [");
+                for (Result r : runResult.getRawPrimaryResults()) {
+                    pw.println("      " + r.getScore());
+                }
+                pw.println("    ]");
+                pw.println("}");
+            }
+            pw.println("]");
+
+        } catch (IOException e) {
+            throw new IllegalStateException(e);
+        } finally {
+            if (pw != null) {
+                pw.close();
+            }
+        }
+
+    }
+
+}
--- a/jmh-core/src/main/java/org/openjdk/jmh/output/results/ResultFormatFactory.java	Thu Oct 24 15:08:08 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/output/results/ResultFormatFactory.java	Thu Oct 24 17:45:12 2013 +0400
@@ -34,6 +34,8 @@
                 return new NoneResultFormat();
             case CSV:
                 return new CSVResultFormat(output);
+            case JSON:
+                return new JSONResultFormat(output);
             default:
                 throw new IllegalStateException("Unsupported result format: " + type);
         }
--- a/jmh-core/src/main/java/org/openjdk/jmh/output/results/ResultFormatType.java	Thu Oct 24 15:08:08 2013 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/output/results/ResultFormatType.java	Thu Oct 24 17:45:12 2013 +0400
@@ -28,6 +28,7 @@
 
     NONE,
     CSV,
+    JSON,
     ;
 
     public static ResultFormatType defaultType() {