Add option to get the verbosity provided by commandline flag -v (#1330) (#1397)

* Add option to get the verbosity provided by commandline flag -v (#1330)

* replace assert with test failure

asserts are stripped out in non debug builds, and we run tests in non-debug CI bots.

* clang-format my own tweak

Co-authored-by: Dominic Hamon <dominichamon@users.noreply.github.com>
This commit is contained in:
Matthdonau 2022-05-17 18:59:36 +02:00 committed by GitHub
parent aecbdbff4f
commit 37be1e8252
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 52 additions and 0 deletions

View File

@ -308,6 +308,9 @@ BENCHMARK_EXPORT std::string GetBenchmarkFilter();
// `benchmark::Initialize()` will override the flag's value.
BENCHMARK_EXPORT void SetBenchmarkFilter(std::string value);
// Returns the current value of --v (command line value for verbosity).
BENCHMARK_EXPORT int32_t GetBenchmarkVerbosity();
// 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

@ -539,6 +539,8 @@ void SetBenchmarkFilter(std::string value) {
FLAGS_benchmark_filter = std::move(value);
}
int32_t GetBenchmarkVerbosity() { return FLAGS_v; }
void RegisterMemoryManager(MemoryManager* manager) {
internal::memory_manager = manager;
}

View File

@ -23,6 +23,7 @@ PER_SRC_TEST_ARGS = ({
"user_counters_tabular_test.cc": ["--benchmark_counters_tabular=true"],
"repetitions_test.cc": [" --benchmark_repetitions=3"],
"spec_arg_test.cc" : ["--benchmark_filter=BM_NotChosen"],
"spec_arg_verbosity_test.cc" : ["--v=42"],
})
load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test")

View File

@ -62,6 +62,9 @@ add_test(NAME benchmark COMMAND benchmark_test --benchmark_min_time=0.01)
compile_benchmark_test(spec_arg_test)
add_test(NAME spec_arg COMMAND spec_arg_test --benchmark_filter=BM_NotChosen)
compile_benchmark_test(spec_arg_verbosity_test)
add_test(NAME spec_arg_verbosity COMMAND spec_arg_verbosity_test --v=42)
compile_benchmark_test(benchmark_setup_teardown_test)
add_test(NAME benchmark_setup_teardown COMMAND benchmark_setup_teardown_test)

View File

@ -0,0 +1,43 @@
#include <string.h>
#include <iostream>
#include "benchmark/benchmark.h"
// Tests that the user specified verbosity level can be get.
static void BM_Verbosity(benchmark::State& state) {
for (auto _ : state) {
}
}
BENCHMARK(BM_Verbosity);
int main(int argc, char** argv) {
const int32_t flagv = 42;
// Verify that argv specify --v=42.
bool found = false;
for (int i = 0; i < argc; ++i) {
if (strcmp("--v=42", argv[i]) == 0) {
found = true;
break;
}
}
if (!found) {
std::cerr << "This test requires '--v=42' to be passed as a command-line "
<< "argument.\n";
return 1;
}
benchmark::Initialize(&argc, argv);
// Check that the current flag value is reported accurately via the
// GetBenchmarkVerbosity() function.
if (flagv != benchmark::GetBenchmarkVerbosity()) {
std::cerr
<< "Seeing different value for flags. GetBenchmarkVerbosity() returns ["
<< benchmark::GetBenchmarkVerbosity() << "] expected flag=[" << flagv
<< "]\n";
return 1;
}
return 0;
}