Adding support to output benchmarks result in JSON
authorRajat Goel <rajatgoel2010@fb.com>
Mon, 25 Jun 2012 20:41:49 +0000 (13:41 -0700)
committerTudor Bosman <tudorb@fb.com>
Wed, 27 Jun 2012 20:50:20 +0000 (13:50 -0700)
Summary: The main reason being so that they can be fed to some automated systems, if required.

Test Plan: Just ran few benchmarks locally

Reviewed By: andrewjcg@fb.com

FB internal diff: D501025

folly/Benchmark.cpp

index 93aa2efe3275572b290b0de3e994f8eefa1f8e44..aa83804b98e63960950230c1636764bfc6171f7e 100644 (file)
@@ -18,6 +18,7 @@
 
 #include "Benchmark.h"
 #include "Foreach.h"
+#include "json.h"
 #include "String.h"
 #include <algorithm>
 #include <cmath>
@@ -29,6 +30,7 @@
 using namespace std;
 
 DEFINE_bool(benchmark, false, "Run benchmarks.");
+DEFINE_bool(json, false, "Output in JSON format.");
 
 namespace folly {
 
@@ -296,7 +298,7 @@ static string humanReadable(double n, unsigned int decimals) {
   return stringPrintf("%*.*f%c", decimals + 3 + 1, decimals, n, suffix);
 }
 
-static void printBenchmarkResults(
+static void printBenchmarkResultsAsTable(
   const vector<tuple<const char*, const char*, double> >& data) {
   // Width available
   static const uint columns = 76;
@@ -366,6 +368,26 @@ static void printBenchmarkResults(
   separator('=');
 }
 
+static void printBenchmarkResultsAsJson(
+  const vector<tuple<const char*, const char*, double> >& data) {
+  dynamic d = dynamic::object;
+  for (auto& datum: data) {
+    d[std::get<1>(datum)] = std::get<2>(datum) * 1000.;
+  }
+
+  printf("%s\n", toPrettyJson(d).c_str());
+}
+
+static void printBenchmarkResults(
+  const vector<tuple<const char*, const char*, double> >& data) {
+
+  if (FLAGS_json) {
+    printBenchmarkResultsAsJson(data);
+  } else {
+    printBenchmarkResultsAsTable(data);
+  }
+}
+
 void runBenchmarks() {
   CHECK(!benchmarks.empty());