Add SetBenchmarkFilter() to set --benchmark_filter flag value in user code (#1362)

* Add SetBenchmarkFilter() to set --benchmark_filter flag value in user code.

Use case:  Provide an API to set this flag indepedence of the flag's implementation (ie., absl flag vs benchmark's flag facility)

* add test

* added notes on Initialize()
This commit is contained in:
Vy Nguyen 2022-03-08 11:02:37 -05:00 committed by GitHub
parent df7749cd09
commit eacce0b503
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 0 deletions

View File

@ -302,6 +302,12 @@ BENCHMARK_EXPORT bool ReportUnrecognizedArguments(int argc, char** argv);
// Returns the current value of --benchmark_filter.
BENCHMARK_EXPORT std::string GetBenchmarkFilter();
// Sets a new value to --benchmark_filter. (This will override this flag's
// current value).
// Should be called after `benchmark::Initialize()`, as
// `benchmark::Initialize()` will override the flag's value.
BENCHMARK_EXPORT void SetBenchmarkFilter(std::string value);
// Creates a default display reporter. Used by the library when no display
// reporter is provided, but also made available for external use in case a
// custom reporter should respect the `--benchmark_format` flag as a fallback

View File

@ -535,6 +535,10 @@ void SetDefaultTimeUnit(TimeUnit unit) { default_time_unit = unit; }
std::string GetBenchmarkFilter() { return FLAGS_benchmark_filter; }
void SetBenchmarkFilter(std::string value) {
FLAGS_benchmark_filter = std::move(value);
}
void RegisterMemoryManager(MemoryManager* manager) {
internal::memory_manager = manager;
}

View File

@ -91,5 +91,15 @@ int main(int argc, char** argv) {
<< matched_functions.front() << "]\n";
return 2;
}
// Test that SetBenchmarkFilter works.
const std::string golden_value = "golden_value";
benchmark::SetBenchmarkFilter(golden_value);
std::string current_value = benchmark::GetBenchmarkFilter();
if (golden_value != current_value) {
std::cerr << "Expected [" << golden_value
<< "] for --benchmark_filter but got [" << current_value << "]\n";
return 3;
}
return 0;
}