CppBenchmark 1.0.5.0
C++ Benchmark Library
Loading...
Searching...
No Matches
launcher_console.cpp
Go to the documentation of this file.
1
10
11#include "benchmark/console.h"
15#include "benchmark/version.h"
16
17#include <iomanip>
18#include <regex>
19
20#include <OptionParser.h>
21
22namespace CppBenchmark {
23
24void LauncherConsole::Initialize(const int argc, char const* const* const argv)
25{
26 auto parser = optparse::OptionParser().version(version);
27
28 const char* output[] = { "console", "csv", "json" };
29
30 parser.add_option("-f", "--filter").dest("filter").help("Filter benchmarks by the given regexp pattern");
31 parser.add_option("-l", "--list").dest("list").action("store_true").help("List all avaliable benchmarks");
32 parser.add_option("-o", "--output").dest("output").choices(&output[0], &output[3]).set_default(output[0]).help("Output format (console, csv, json). Default: %default");
33 parser.add_option("-q", "--quiet").dest("quiet").action("store_true").help("Launch in quiet mode. No progress will be shown!");
34 parser.add_option("-r", "--histograms").dest("histograms").action("store").type("int").set_default(0).help("Create High Dynamic Range (HDR) Histogram files with a given resolution. Default: %default");
35
36 optparse::Values options = parser.parse_args(argc, argv);
37
38 // Check for double initialization
39 if (_init)
40 {
41 parser.error("Console launcher double initialization!");
42 }
43
44 // Print help
45 if (options.get("help"))
46 {
47 parser.print_help();
48 exit(EXIT_SUCCESS);
49 }
50
51 // Setup console launcher parameters
52 _list = options.get("list");
53 _quiet = options.get("quiet");
54 _histograms = (int32_t)options.get("histograms");
55 if (options.is_set("filter"))
56 _filter = options["filter"];
57 if (options.is_set("output"))
58 _output = options["output"];
59
60 // Update initialization flag
61 _init = true;
62}
63
65{
66 if (_list)
67 {
68 // List all suitable benchmarks
69 std::regex matcher(_filter);
70 for (const auto& benchmark : _benchmarks)
71 {
72 // Match benchmark name with the given pattern
73 if (_filter.empty() || std::regex_match(benchmark->name(), matcher))
74 std::cout << benchmark->name() << std::endl;
75 }
76 }
77 else
78 {
79 // Launch all suitable benchmarks
80 Launcher::Launch(_filter);
81 }
82}
83
85{
86 if (_output == "console")
87 {
88 ReporterConsole reporter(std::cout);
89 Launcher::Report(reporter);
90 }
91 else if (_output == "csv")
92 {
93 ReporterCSV reporter(std::cout);
94 Launcher::Report(reporter);
95 }
96 else if (_output == "json")
97 {
98 ReporterJSON reporter(std::cout);
99 Launcher::Report(reporter);
100 }
101
102 // Report histograms
103 if (_histograms > 0)
104 Launcher::ReportHistograms(_histograms);
105}
106
107void LauncherConsole::onLaunching(int current, int total, const BenchmarkBase& benchmark, const Context& context, int attempt)
108{
109 if (_quiet)
110 return;
111
112 std::cerr << Color::DARKGREY << "[" << std::setw(3) << (100 * current / total) << "%] " << Color::GREY << "Launching " << Color::LIGHTCYAN << benchmark.name() << context.description() << Color::GREY << ". Attempt " << Color::WHITE << attempt << Color::GREY << "...";
113}
114
115void LauncherConsole::onLaunched(int current, int total, const BenchmarkBase& benchmark, const Context& context, int attempt)
116{
117 if (_quiet)
118 return;
119
120 std::cerr << Color::LIGHTGREEN << "Done!" << std::endl;
121}
122
123} // namespace CppBenchmark
Benchmark base class.
const std::string & name() const
Get benchmark name.
Benchmark running context.
Definition context.h:27
virtual std::string description() const
Get description of the current benchmark running context.
Definition context.cpp:13
void onLaunched(int current, int total, const BenchmarkBase &benchmark, const Context &context, int attempt) override
Handle benchmark launched notification.
void Execute()
Execute benchmarks and show progress in console.
void Initialize(const int argc, char const *const *const argv)
Initialized console launcher.
void onLaunching(int current, int total, const BenchmarkBase &benchmark, const Context &context, int attempt) override
Handle benchmark launching notification.
void Report() const
Report benchmarks results in console.
virtual void Launch(const std::string &pattern="")
Launch registered benchmarks.
Definition launcher.cpp:16
void Report(Reporter &reporter) const
Report benchmarks results using the given reporter.
Definition launcher.cpp:43
void ReportHistograms(int32_t resolution) const
Report benchmarks High Dynamic Range (HDR) Histograms.
Definition launcher.cpp:85
std::vector< std::shared_ptr< BenchmarkBase > > _benchmarks
Registered benchmarks collection.
Definition launcher.h:73
Comma-separated values (CSV) reporter.
Console management definition.
Console launcher definition.
C++ Benchmark project definitions.
Definition barrier.h:15
const char version[]
Project version.
Definition version.h:35
@ LIGHTCYAN
Light cyan color.
@ GREY
Grey color.
@ DARKGREY
Dark grey color.
@ LIGHTGREEN
Light green color.
@ WHITE
White color.
Console reporter definition.
Comma-separated values (CSV) reporter definition.
JSON reporter definition.
Version definition.