66
77#include < fs.h>
88#include < test/util/setup_common.h>
9+ #include < util/string.h>
910
1011#include < algorithm>
1112#include < chrono>
@@ -42,18 +43,42 @@ void GenerateTemplateResults(const std::vector<ankerl::nanobench::Result>& bench
4243
4344} // namespace
4445
45- benchmark::BenchRunner::BenchmarkMap& benchmark::BenchRunner::benchmarks ()
46+ namespace benchmark {
47+
48+ // map a label to one or multiple priority levels
49+ std::map<std::string, uint8_t > map_label_priority = {
50+ {" high" , PriorityLevel::HIGH},
51+ {" low" , PriorityLevel::LOW},
52+ {" all" , 0xff }
53+ };
54+
55+ std::string ListPriorities ()
4656{
47- static std::map<std::string, BenchFunction> benchmarks_map;
57+ using item_t = std::pair<std::string, uint8_t >;
58+ auto sort_by_priority = [](item_t a, item_t b){ return a.second < b.second ; };
59+ std::set<item_t , decltype (sort_by_priority)> sorted_priorities (map_label_priority.begin (), map_label_priority.end (), sort_by_priority);
60+ return Join (sorted_priorities, ' ,' , [](const auto & entry){ return entry.first ; });
61+ }
62+
63+ uint8_t StringToPriority (const std::string& str)
64+ {
65+ auto it = map_label_priority.find (str);
66+ if (it == map_label_priority.end ()) throw std::runtime_error (strprintf (" Unknown priority level %s" , str));
67+ return it->second ;
68+ }
69+
70+ BenchRunner::BenchmarkMap& BenchRunner::benchmarks ()
71+ {
72+ static BenchmarkMap benchmarks_map;
4873 return benchmarks_map;
4974}
5075
51- benchmark:: BenchRunner::BenchRunner (std::string name, benchmark:: BenchFunction func)
76+ BenchRunner::BenchRunner (std::string name, BenchFunction func, PriorityLevel level )
5277{
53- benchmarks ().insert (std::make_pair (name, func));
78+ benchmarks ().insert (std::make_pair (name, std::make_pair ( func, level) ));
5479}
5580
56- void benchmark:: BenchRunner::RunAll (const Args& args)
81+ void BenchRunner::RunAll (const Args& args)
5782{
5883 std::regex reFilter (args.regex_filter );
5984 std::smatch baseMatch;
@@ -63,33 +88,39 @@ void benchmark::BenchRunner::RunAll(const Args& args)
6388 }
6489
6590 std::vector<ankerl::nanobench::Result> benchmarkResults;
66- for (const auto & p : benchmarks ()) {
67- if (!std::regex_match (p.first , baseMatch, reFilter)) {
91+ for (const auto & [name, bench_func] : benchmarks ()) {
92+ const auto & [func, priority_level] = bench_func;
93+
94+ if (!(priority_level & args.priority )) {
95+ continue ;
96+ }
97+
98+ if (!std::regex_match (name, baseMatch, reFilter)) {
6899 continue ;
69100 }
70101
71102 if (args.is_list_only ) {
72- std::cout << p. first << std::endl;
103+ std::cout << name << std::endl;
73104 continue ;
74105 }
75106
76107 Bench bench;
77108 if (args.sanity_check ) {
78109 bench.epochs (1 ).epochIterations (1 );
79110 }
80- bench.name (p. first );
111+ bench.name (name );
81112 if (args.min_time > 0ms) {
82113 // convert to nanos before dividing to reduce rounding errors
83114 std::chrono::nanoseconds min_time_ns = args.min_time ;
84115 bench.minEpochTime (min_time_ns / bench.epochs ());
85116 }
86117
87118 if (args.asymptote .empty ()) {
88- p. second (bench);
119+ func (bench);
89120 } else {
90121 for (auto n : args.asymptote ) {
91122 bench.complexityN (n);
92- p. second (bench);
123+ func (bench);
93124 }
94125 std::cout << bench.complexityBigO () << std::endl;
95126 }
@@ -104,3 +135,5 @@ void benchmark::BenchRunner::RunAll(const Args& args)
104135 " {{/result}}" );
105136 GenerateTemplateResults (benchmarkResults, args.output_json , ankerl::nanobench::templates::json ());
106137}
138+
139+ } // namespace benchmark
0 commit comments