From 2e76cb012388630c6e167e9ea50395d0ef597e76 Mon Sep 17 00:00:00 2001 From: Rajat Goel Date: Mon, 25 Jun 2012 13:41:49 -0700 Subject: [PATCH] Adding support to output benchmarks result in JSON 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 | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/folly/Benchmark.cpp b/folly/Benchmark.cpp index 93aa2efe..aa83804b 100644 --- a/folly/Benchmark.cpp +++ b/folly/Benchmark.cpp @@ -18,6 +18,7 @@ #include "Benchmark.h" #include "Foreach.h" +#include "json.h" #include "String.h" #include #include @@ -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 >& data) { // Width available static const uint columns = 76; @@ -366,6 +368,26 @@ static void printBenchmarkResults( separator('='); } +static void printBenchmarkResultsAsJson( + const vector >& 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 >& data) { + + if (FLAGS_json) { + printBenchmarkResultsAsJson(data); + } else { + printBenchmarkResultsAsTable(data); + } +} + void runBenchmarks() { CHECK(!benchmarks.empty()); -- 2.34.1