1
0
mirror of https://github.com/google/benchmark.git synced 2025-04-29 06:20:32 +08:00

Add flag to control output format

This commit is contained in:
Dominic Hamon 2015-03-12 21:23:33 -07:00
parent 1183c04884
commit 2cb4428f3e
3 changed files with 31 additions and 8 deletions

View File

@ -482,17 +482,16 @@ class Benchmark {
// default reporter used by RunSpecifiedBenchmarks().
class ConsoleReporter : public BenchmarkReporter {
public:
ConsoleReporter();
virtual bool ReportContext(const Context& context) const;
virtual void ReportRuns(const std::vector<Run>& reports) const;
private:
enum Format {
FORMAT_TABLE,
FORMAT_CSV,
};
explicit ConsoleReporter(Format format)
: format_(format) {}
virtual bool ReportContext(const Context& context) const;
virtual void ReportRuns(const std::vector<Run>& reports) const;
private:
virtual void PrintRunData(const Run& report) const;
// TODO(ericwf): Find a better way to share this information.
mutable size_t name_field_width_;

View File

@ -61,6 +61,10 @@ DEFINE_int32(benchmark_repetitions, 1,
"The number of runs of each benchmark. If greater than 1, the "
"mean and standard deviation of the runs will be reported.");
DEFINE_string(benchmark_format, "tabular",
"The format to use for console output. Valid values are "
"'tabular' or 'csv'.");
DEFINE_bool(color_print, true, "Enables colorized logging.");
DEFINE_int32(v, 0, "The level of verbose logging to output");
@ -751,6 +755,17 @@ void State::SetLabel(const char* label) {
BenchmarkReporter::~BenchmarkReporter() {}
ConsoleReporter::ConsoleReporter() {
if (FLAGS_benchmark_format == "tabular") {
format_ = FORMAT_TABLE;
} else if (FLAGS_benchmark_format == "csv") {
format_ = FORMAT_CSV;
} else {
std::cerr << "Unexpected format: '" << FLAGS_benchmark_format << "'\n";
std::exit(1);
}
}
bool ConsoleReporter::ReportContext(const Context& context) const {
name_field_width_ = context.name_field_width;
@ -926,7 +941,7 @@ void RunSpecifiedBenchmarks(const BenchmarkReporter* reporter) {
std::string spec = FLAGS_benchmark_filter;
if (spec.empty() || spec == "all")
spec = "."; // Regexp that matches all benchmarks
ConsoleReporter default_reporter(ConsoleReporter::FORMAT_TABLE);
ConsoleReporter default_reporter;
RunMatchingBenchmarks(spec, reporter ? reporter : &default_reporter);
}
@ -939,6 +954,7 @@ void PrintUsageAndExit() {
" [--benchmark_iterations=<iterations>]\n"
" [--benchmark_min_time=<min_time>]\n"
" [--benchmark_repetitions=<num_repetitions>]\n"
" [--benchmark_format=<tabular|csv>\n"
" [--color_print={true|false}]\n"
" [--v=<verbosity>]\n");
exit(0);
@ -956,6 +972,8 @@ void ParseCommandLineFlags(int* argc, const char** argv) {
&FLAGS_benchmark_min_time) ||
ParseInt32Flag(argv[i], "benchmark_repetitions",
&FLAGS_benchmark_repetitions) ||
ParseStringFlag(argv[i], "benchmark_format",
&FLAGS_benchmark_format) ||
ParseBoolFlag(argv[i], "color_print",
&FLAGS_color_print) ||
ParseInt32Flag(argv[i], "v", &FLAGS_v)) {
@ -967,6 +985,12 @@ void ParseCommandLineFlags(int* argc, const char** argv) {
PrintUsageAndExit();
}
}
// Check valid values.
if (FLAGS_benchmark_format != "tabular" &&
FLAGS_benchmark_format != "csv") {
PrintUsageAndExit();
}
}
} // end namespace internal

View File

@ -31,7 +31,7 @@ class TestReporter : public benchmark::ConsoleReporter {
ConsoleReporter::ReportRuns(report);
};
TestReporter() : ConsoleReporter(ConsoleReporter::FORMAT_CSV), count_(0) {}
TestReporter() : count_(0) {}
virtual ~TestReporter() {}