bench: Remove -priority-level= option

This commit is contained in:
MarcoFalke
2026-01-06 16:18:42 +01:00
parent 114901c065
commit fa790c3eea
3 changed files with 9 additions and 70 deletions

View File

@@ -5,25 +5,19 @@
#include <bench/bench.h>
#include <test/util/setup_common.h> // IWYU pragma: keep
#include <tinyformat.h>
#include <util/fs.h>
#include <util/string.h>
#include <chrono>
#include <compare>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <ratio>
#include <regex>
#include <set>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
using namespace std::chrono_literals;
using util::Join;
const std::function<void(const std::string&)> G_TEST_LOG_FUN{};
@@ -69,37 +63,15 @@ void GenerateTemplateResults(const std::vector<ankerl::nanobench::Result>& bench
namespace benchmark {
// map a label to one or multiple priority levels
std::map<std::string, uint8_t> map_label_priority = {
{"high", PriorityLevel::HIGH},
{"low", PriorityLevel::LOW},
{"all", 0xff}
};
std::string ListPriorities()
{
using item_t = std::pair<std::string, uint8_t>;
auto sort_by_priority = [](item_t a, item_t b){ return a.second < b.second; };
std::set<item_t, decltype(sort_by_priority)> sorted_priorities(map_label_priority.begin(), map_label_priority.end(), sort_by_priority);
return Join(sorted_priorities, ',', [](const auto& entry){ return entry.first; });
}
uint8_t StringToPriority(const std::string& str)
{
auto it = map_label_priority.find(str);
if (it == map_label_priority.end()) throw std::runtime_error(strprintf("Unknown priority level %s", str));
return it->second;
}
BenchRunner::BenchmarkMap& BenchRunner::benchmarks()
{
static BenchmarkMap benchmarks_map;
return benchmarks_map;
}
BenchRunner::BenchRunner(std::string name, BenchFunction func, PriorityLevel level)
BenchRunner::BenchRunner(std::string name, BenchFunction func)
{
benchmarks().insert(std::make_pair(name, std::make_pair(func, level)));
benchmarks().insert(std::make_pair(name, func));
}
void BenchRunner::RunAll(const Args& args)
@@ -120,12 +92,7 @@ void BenchRunner::RunAll(const Args& args)
};
std::vector<ankerl::nanobench::Result> benchmarkResults;
for (const auto& [name, bench_func] : benchmarks()) {
const auto& [func, priority_level] = bench_func;
if (!(priority_level & args.priority)) {
continue;
}
for (const auto& [name, func] : benchmarks()) {
if (!std::regex_match(name, baseMatch, reFilter)) {
continue;

View File

@@ -10,11 +10,9 @@
#include <util/macros.h>
#include <chrono>
#include <cstdint>
#include <functional>
#include <map>
#include <string>
#include <utility>
#include <vector>
/*
@@ -42,16 +40,6 @@ using ankerl::nanobench::Bench;
typedef std::function<void(Bench&)> BenchFunction;
enum PriorityLevel : uint8_t
{
LOW = 1 << 0,
HIGH = 1 << 2,
};
// List priority labels, comma-separated and sorted by increasing priority
std::string ListPriorities();
uint8_t StringToPriority(const std::string& str);
struct Args {
bool is_list_only;
bool sanity_check;
@@ -60,25 +48,24 @@ struct Args {
fs::path output_csv;
fs::path output_json;
std::string regex_filter;
uint8_t priority;
std::vector<std::string> setup_args;
};
class BenchRunner
{
// maps from "name" -> (function, priority_level)
typedef std::map<std::string, std::pair<BenchFunction, PriorityLevel>> BenchmarkMap;
// maps from "name" -> function
using BenchmarkMap = std::map<std::string, BenchFunction>;
static BenchmarkMap& benchmarks();
public:
BenchRunner(std::string name, BenchFunction func, PriorityLevel level);
BenchRunner(std::string name, BenchFunction func);
static void RunAll(const Args& args);
};
} // namespace benchmark
// BENCHMARK(foo) expands to: benchmark::BenchRunner bench_11foo("foo", foo, priority_level);
// BENCHMARK(foo) expands to: benchmark::BenchRunner bench_11foo{"foo", foo};
#define BENCHMARK(n, priority_level) \
benchmark::BenchRunner PASTE2(bench_, PASTE2(__LINE__, n))(STRINGIZE(n), n, priority_level);
benchmark::BenchRunner PASTE2(bench_, PASTE2(__LINE__, n)){STRINGIZE(n), n};
#endif // BITCOIN_BENCH_BENCH_H

View File

@@ -18,12 +18,8 @@
#include <sstream>
#include <vector>
using util::SplitString;
static const char* DEFAULT_BENCH_FILTER = ".*";
static constexpr int64_t DEFAULT_MIN_TIME_MS{10};
/** Priority level default value, run "all" priority levels */
static const std::string DEFAULT_PRIORITY{"all"};
static void SetupBenchArgs(ArgsManager& argsman)
{
@@ -37,8 +33,6 @@ static void SetupBenchArgs(ArgsManager& argsman)
argsman.AddArg("-output-csv=<output.csv>", "Generate CSV file with the most important benchmark results", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-output-json=<output.json>", "Generate JSON file with all benchmark results", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-sanity-check", "Run benchmarks for only one iteration with no output", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-priority-level=<l1,l2,l3>", strprintf("Run benchmarks of one or multiple priority level(s) (%s), default: '%s'",
benchmark::ListPriorities(), DEFAULT_PRIORITY), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
}
// parses a comma separated list like "10,20,30,50"
@@ -54,14 +48,6 @@ static std::vector<double> parseAsymptote(const std::string& str) {
return numbers;
}
static uint8_t parsePriorityLevel(const std::string& str) {
uint8_t levels{0};
for (const auto& level: SplitString(str, ',')) {
levels |= benchmark::StringToPriority(level);
}
return levels;
}
static std::vector<std::string> parseTestSetupArgs(const ArgsManager& argsman)
{
// Parses unit test framework arguments supported by the benchmark framework.
@@ -144,7 +130,6 @@ int main(int argc, char** argv)
args.output_json = argsman.GetPathArg("-output-json");
args.regex_filter = argsman.GetArg("-filter", DEFAULT_BENCH_FILTER);
args.sanity_check = argsman.GetBoolArg("-sanity-check", false);
args.priority = parsePriorityLevel(argsman.GetArg("-priority-level", DEFAULT_PRIORITY));
args.setup_args = parseTestSetupArgs(argsman);
benchmark::BenchRunner::RunAll(args);