diff --git a/include/benchmark/reporter.h b/include/benchmark/reporter.h index 3d573d83..6aebf0c0 100644 --- a/include/benchmark/reporter.h +++ b/include/benchmark/reporter.h @@ -80,6 +80,8 @@ class BenchmarkReporter { virtual void Finalize(); virtual ~BenchmarkReporter(); +protected: + static void ComputeStats(std::vector const& reports, Run* mean, Run* stddev); }; // Simple reporter that outputs benchmark data to the console. This is the diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9b26f0a3..17fb42ad 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,8 +3,8 @@ include_directories(${PROJECT_SOURCE_DIR}/src) # Define the source files set(SOURCE_FILES "benchmark.cc" "colorprint.cc" "commandlineflags.cc" "log.cc" - "reporter.cc" "sleep.cc" "string_util.cc" "sysinfo.cc" - "walltime.cc") + "json_reporter.cc" "reporter.cc" "sleep.cc" "string_util.cc" + "sysinfo.cc" "walltime.cc") # Determine the correct regular expression engine to use if(HAVE_STD_REGEX) set(RE_FILES "re_std.cc") diff --git a/src/json_reporter.cc b/src/json_reporter.cc new file mode 100644 index 00000000..2ce5cd22 --- /dev/null +++ b/src/json_reporter.cc @@ -0,0 +1,168 @@ +// Copyright 2015 Google Inc. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "benchmark/reporter.h" + +#include +#include +#include +#include + +#include "string_util.h" +#include "walltime.h" + +namespace benchmark { + +namespace { + +std::string FormatKV(std::string const& key, std::string const& value) { + return StringPrintF("\"%s\": \"%s\"", key.c_str(), value.c_str()); +} + +std::string FormatKV(std::string const& key, const char* value) { + return StringPrintF("\"%s\": \"%s\"", key.c_str(), value); +} + +std::string FormatKV(std::string const& key, bool value) { + return StringPrintF("\"%s\": %s", key.c_str(), value ? "true" : "false"); +} + +std::string FormatKV(std::string const& key, int64_t value) { + std::stringstream ss; + ss << '"' << key << "\": " << value; + return ss.str(); +} + +std::string FormatKV(std::string const& key, std::size_t value) { + std::stringstream ss; + ss << '"' << key << "\": " << value; + return ss.str(); +} + +int64_t RoundDouble(double v) { + return static_cast(v + 0.5); +} + +} // end namespace + +bool JSONReporter::ReportContext(const Context& context) { + std::ostream& out = std::cout; + + out << "{\n"; + std::string inner_indent(2, ' '); + + // Open context block and print context information. + out << inner_indent << "\"context\": {\n"; + std::string indent(4, ' '); + int remainder_us; + std::string walltime_value = walltime::Print( + walltime::Now(), "%Y/%m/%d-%H:%M:%S", + true, // use local timezone + &remainder_us); + out << indent << FormatKV("date", walltime_value) << ",\n"; + + out << indent + << FormatKV("num_cpus", static_cast(context.num_cpus)) + << ",\n"; + out << indent + << FormatKV("mhz_per_cpu", RoundDouble(context.mhz_per_cpu)) + << ",\n"; + out << indent + << FormatKV("cpu_scaling_enabled", context.cpu_scaling_enabled) + << ",\n"; + +#if defined(NDEBUG) + const char build_type[] = "release"; +#else + const char build_type[] = "debug"; +#endif + out << indent << FormatKV("build_type", build_type) << "\n"; + // Close context block and open the list of benchmarks. + out << inner_indent << "},\n"; + out << inner_indent << "\"benchmarks\": [\n"; + return true; +} + +void JSONReporter::ReportRuns(std::vector const& reports) { + if (reports.empty()) { + return; + } + std::string indent(4, ' '); + std::ostream& out = std::cout; + if (!first_report_) { + out << ",\n"; + } + first_report_ = false; + std::vector reports_cp = reports; + if (reports.size() >= 2) { + Run mean_data; + Run stddev_data; + BenchmarkReporter::ComputeStats(reports, &mean_data, &stddev_data); + reports_cp.push_back(mean_data); + reports_cp.push_back(stddev_data); + } + for (auto it = reports_cp.begin(); it != reports_cp.end(); ++it) { + out << indent << "{\n"; + PrintRunData(*it); + out << indent << '}'; + auto it_cp = it; + if (++it_cp != reports_cp.end()) { + out << ','; + } + } +} + +void JSONReporter::Finalize() { + // Close the list of benchmarks and the top level object. + std::cout << "\n ]\n}\n"; +} + +void JSONReporter::PrintRunData(Run const& run) { + double const multiplier = 1e9; // nano second multiplier + double cpu_time = run.cpu_accumulated_time * multiplier; + double real_time = run.real_accumulated_time * multiplier; + if (run.iterations != 0) { + real_time = real_time / static_cast(run.iterations); + cpu_time = cpu_time / static_cast(run.iterations); + } + + std::string indent(6, ' '); + std::ostream& out = std::cout; + out << indent + << FormatKV("name", run.benchmark_name) + << ",\n"; + out << indent + << FormatKV("iterations", run.iterations) + << ",\n"; + out << indent + << FormatKV("real_time", RoundDouble(real_time)) + << ",\n"; + out << indent + << FormatKV("cpu_time", RoundDouble(cpu_time)); + if (run.bytes_per_second > 0.0) { + out << ",\n" << indent + << FormatKV("bytes_per_second", RoundDouble(run.bytes_per_second)); + } + if (run.items_per_second > 0.0) { + out << ",\n" << indent + << FormatKV("items_per_second", RoundDouble(run.items_per_second)); + } + if (!run.report_label.empty()) { + out << ",\n" << indent + << FormatKV("label", run.report_label); + } + out << '\n'; +} + +} // end namespace benchmark \ No newline at end of file diff --git a/src/reporter.cc b/src/reporter.cc index b2deea47..233fd9e3 100644 --- a/src/reporter.cc +++ b/src/reporter.cc @@ -27,11 +27,10 @@ #include "walltime.h" namespace benchmark { -namespace { -void ComputeStats(const std::vector& reports, - BenchmarkReporter::Run* mean_data, - BenchmarkReporter::Run* stddev_data) { +void BenchmarkReporter::ComputeStats( + const std::vector& reports, + Run* mean_data, Run* stddev_data) { CHECK(reports.size() >= 2) << "Cannot compute stats for less than 2 reports"; // Accumulators. Stat1_d real_accumulated_time_stat; @@ -43,7 +42,7 @@ void ComputeStats(const std::vector& reports, std::size_t const run_iterations = reports.front().iterations; // Populate the accumulators. - for (BenchmarkReporter::Run const& run : reports) { + for (Run const& run : reports) { CHECK_EQ(reports[0].benchmark_name, run.benchmark_name); CHECK_EQ(run_iterations, run.iterations); real_accumulated_time_stat += @@ -84,8 +83,6 @@ void ComputeStats(const std::vector& reports, stddev_data->items_per_second = items_per_second_stat.StdDev(); } -} // end namespace - void BenchmarkReporter::Finalize() { } @@ -146,7 +143,7 @@ void ConsoleReporter::ReportRuns(const std::vector& reports) { Run mean_data; Run stddev_data; - ComputeStats(reports, &mean_data, &stddev_data); + BenchmarkReporter::ComputeStats(reports, &mean_data, &stddev_data); // Output using PrintRun. PrintRunData(mean_data); @@ -189,145 +186,4 @@ void ConsoleReporter::PrintRunData(const Run& result) { result.report_label.c_str()); } -namespace { - -std::string FormatKV(std::string const& key, std::string const& value) { - return StringPrintF("\"%s\": \"%s\"", key.c_str(), value.c_str()); -} - -std::string FormatKV(std::string const& key, const char* value) { - return StringPrintF("\"%s\": \"%s\"", key.c_str(), value); -} - -std::string FormatKV(std::string const& key, bool value) { - return StringPrintF("\"%s\": %s", key.c_str(), value ? "true" : "false"); -} - -std::string FormatKV(std::string const& key, int64_t value) { - std::stringstream ss; - ss << '"' << key << "\": " << value; - return ss.str(); -} - -std::string FormatKV(std::string const& key, std::size_t value) { - std::stringstream ss; - ss << '"' << key << "\": " << value; - return ss.str(); -} - -int64_t RoundDouble(double v) { - return static_cast(v + 0.5); -} - -} // end namespace - -bool JSONReporter::ReportContext(const Context& context) { - std::ostream& out = std::cout; - - out << "{\n"; - std::string inner_indent(2, ' '); - - // Open context block and print context information. - out << inner_indent << "\"context\": {\n"; - std::string indent(4, ' '); - int remainder_us; - std::string walltime_value = walltime::Print( - walltime::Now(), "%Y/%m/%d-%H:%M:%S", - true, // use local timezone - &remainder_us); - out << indent << FormatKV("date", walltime_value) << ",\n"; - - out << indent - << FormatKV("num_cpus", static_cast(context.num_cpus)) - << ",\n"; - out << indent - << FormatKV("mhz_per_cpu", RoundDouble(context.mhz_per_cpu)) - << ",\n"; - out << indent - << FormatKV("cpu_scaling_enabled", context.cpu_scaling_enabled) - << ",\n"; - -#if defined(NDEBUG) - const char[] build_type = "release"; -#else - const char[] build_type = "debug"; -#endif - out << indent << FormatKV("build_type", build_type) << "\n"; - // Close context block and open the list of benchmarks. - out << inner_indent << "},\n"; - out << inner_indent << "\"benchmarks\": [\n"; - return true; -} - -void JSONReporter::ReportRuns(std::vector const& reports) { - if (reports.empty()) { - return; - } - std::string indent(4, ' '); - std::ostream& out = std::cout; - if (!first_report_) { - out << ",\n"; - } - first_report_ = false; - std::vector reports_cp = reports; - if (reports.size() >= 2) { - Run mean_data; - Run stddev_data; - ComputeStats(reports, &mean_data, &stddev_data); - reports_cp.push_back(mean_data); - reports_cp.push_back(stddev_data); - } - for (auto it = reports_cp.begin(); it != reports_cp.end(); ++it) { - out << indent << "{\n"; - PrintRunData(*it); - out << indent << '}'; - auto it_cp = it; - if (++it_cp != reports_cp.end()) { - out << ','; - } - } -} - -void JSONReporter::Finalize() { - // Close the list of benchmarks and the top level object. - std::cout << "\n ]\n}\n"; -} - -void JSONReporter::PrintRunData(Run const& run) { - double const multiplier = 1e9; // nano second multiplier - double cpu_time = run.cpu_accumulated_time * multiplier; - double real_time = run.real_accumulated_time * multiplier; - if (run.iterations != 0) { - real_time = real_time / static_cast(run.iterations); - cpu_time = cpu_time / static_cast(run.iterations); - } - - std::string indent(6, ' '); - std::ostream& out = std::cout; - out << indent - << FormatKV("name", run.benchmark_name) - << ",\n"; - out << indent - << FormatKV("iterations", run.iterations) - << ",\n"; - out << indent - << FormatKV("real_time", RoundDouble(real_time)) - << ",\n"; - out << indent - << FormatKV("cpu_time", RoundDouble(cpu_time)); - if (run.bytes_per_second > 0.0) { - out << ",\n" << indent - << FormatKV("bytes_per_second", RoundDouble(run.bytes_per_second)); - } - if (run.items_per_second > 0.0) { - out << ",\n" << indent - << FormatKV("items_per_second", RoundDouble(run.items_per_second)); - } - if (!run.report_label.empty()) { - out << ",\n" << indent - << FormatKV("label", run.report_label); - } - out << '\n'; -} - } // end namespace benchmark