-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.cpp
More file actions
86 lines (70 loc) · 2.95 KB
/
runner.cpp
File metadata and controls
86 lines (70 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "runner.hpp"
#include <filesystem>
#include <format>
#include <iostream>
#include <ranges>
namespace fs = std::filesystem;
namespace vw = std::views;
namespace gl_bench {
runner::runner() : _parser("cpp-gl-bench") {
auto& glob_args = this->_parser.add_group("Global Benchmark Options");
this->_glob_args = &glob_args;
this->_parser.add_optional_argument<argon::none_type>(glob_args, "help", "h")
.help("Display the help message")
.action<argon::action_type::on_flag>(argon::action::print_help(this->_parser, 0));
this->_parser.add_optional_argument<argon::none_type>(glob_args, "gbench-help")
.help("Display the Google Benchmar help message")
.action<argon::action_type::on_flag>([]() {
benchmark::PrintDefaultHelp();
std::exit(0);
});
this->_parser.add_optional_argument<fs::path>(glob_args, "output", "o")
.help("Path to the output JSON file")
.nargs(argon::nargs::up_to(1uz))
.action<argon::action_type::observe>([](const fs::path& path) {
if (path.extension() != ".json") {
throw std::runtime_error(std::format(
"Invalid output file path (must be a .json file, got: {})", path.string()
));
}
if (path.has_parent_path() and not fs::exists(path.parent_path())) {
throw std::runtime_error(
std::format("Output directory does not exist: {}", path.parent_path().string())
);
}
});
}
void runner::add_suite(const std::string& name, suite suite) {
this->_suites[name] = suite;
if (suite.add_args)
suite.add_args(this->_parser);
}
int runner::run(int argc, char** argv) {
this->_parser.add_optional_argument(*this->_glob_args, "suites", "s")
.choices(this->_suites | vw::keys)
.default_values(this->_suites | vw::keys)
.help("Select the suites to run (all by default)");
std::vector<std::string> gbench_args = this->_parser.try_parse_known_args(argc, argv);
// Register benchmark suites
for (const auto& suite_name : this->_parser.values("suites")) {
const auto& suite = this->_suites.at(suite_name);
suite.register_benchmarks(this->_parser);
}
// Handle JSON export if requested
if (this->_parser.has_value("output")) {
auto out_path = this->_parser.value<fs::path>("output");
gbench_args.push_back("--benchmark_out=" + out_path.string());
gbench_args.emplace_back("--benchmark_out_format=json");
}
// Reconstruct argv for Google Benchmark
std::vector<char*> gbench_argv;
gbench_argv.push_back(argv[0]);
for (auto& arg : gbench_args)
gbench_argv.push_back(arg.data());
int gbench_argc = static_cast<int>(gbench_argv.size());
benchmark::Initialize(&gbench_argc, gbench_argv.data());
benchmark::RunSpecifiedBenchmarks();
benchmark::Shutdown();
return 0;
}
} // namespace gl_bench