mirror of
https://github.com/google/benchmark.git
synced 2025-02-27 03:40:08 +08:00
Add ClearRegisteredBenchmark() function. (#402)
* Add ClearRegisteredBenchmark() function. Since benchmarks can be registered at runtime using the RegisterBenchmark(...) functions, it makes sense to have a ClearRegisteredBenchmarks() function too, that can be used at runtime to clear the currently registered benchmark and re-register an entirely new set. This allows users to run a set of registered benchmarks, get the output using a custom reporter, and then clear and re-register new benchmarks based on the previous results. This fixes issue #400, at least partially. * Remove unused change
This commit is contained in:
parent
d6aacaf48f
commit
b8a2206fb2
@ -707,6 +707,10 @@ template <class Lambda>
|
|||||||
internal::Benchmark* RegisterBenchmark(const char* name, Lambda&& fn);
|
internal::Benchmark* RegisterBenchmark(const char* name, Lambda&& fn);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// Remove all registered benchmarks. All pointers to previously registered
|
||||||
|
// benchmarks are invalidated.
|
||||||
|
void ClearRegisteredBenchmarks();
|
||||||
|
|
||||||
namespace internal {
|
namespace internal {
|
||||||
// The class used to hold all Benchmarks created from static function.
|
// The class used to hold all Benchmarks created from static function.
|
||||||
// (ie those created using the BENCHMARK(...) macros.
|
// (ie those created using the BENCHMARK(...) macros.
|
||||||
|
@ -70,6 +70,9 @@ class BenchmarkFamilies {
|
|||||||
// Registers a benchmark family and returns the index assigned to it.
|
// Registers a benchmark family and returns the index assigned to it.
|
||||||
size_t AddBenchmark(std::unique_ptr<Benchmark> family);
|
size_t AddBenchmark(std::unique_ptr<Benchmark> family);
|
||||||
|
|
||||||
|
// Clear all registered benchmark families.
|
||||||
|
void ClearBenchmarks();
|
||||||
|
|
||||||
// Extract the list of benchmark instances that match the specified
|
// Extract the list of benchmark instances that match the specified
|
||||||
// regular expression.
|
// regular expression.
|
||||||
bool FindBenchmarks(const std::string& re,
|
bool FindBenchmarks(const std::string& re,
|
||||||
@ -95,6 +98,12 @@ size_t BenchmarkFamilies::AddBenchmark(std::unique_ptr<Benchmark> family) {
|
|||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void BenchmarkFamilies::ClearBenchmarks() {
|
||||||
|
MutexLock l(mutex_);
|
||||||
|
families_.clear();
|
||||||
|
families_.shrink_to_fit();
|
||||||
|
}
|
||||||
|
|
||||||
bool BenchmarkFamilies::FindBenchmarks(
|
bool BenchmarkFamilies::FindBenchmarks(
|
||||||
const std::string& spec, std::vector<Benchmark::Instance>* benchmarks,
|
const std::string& spec, std::vector<Benchmark::Instance>* benchmarks,
|
||||||
std::ostream* ErrStream) {
|
std::ostream* ErrStream) {
|
||||||
@ -450,4 +459,9 @@ int Benchmark::ArgsCnt() const {
|
|||||||
void FunctionBenchmark::Run(State& st) { func_(st); }
|
void FunctionBenchmark::Run(State& st) { func_(st); }
|
||||||
|
|
||||||
} // end namespace internal
|
} // end namespace internal
|
||||||
|
|
||||||
|
void ClearRegisteredBenchmarks() {
|
||||||
|
internal::BenchmarkFamilies::GetInstance()->ClearBenchmarks();
|
||||||
|
}
|
||||||
|
|
||||||
} // end namespace benchmark
|
} // end namespace benchmark
|
||||||
|
@ -126,11 +126,11 @@ void TestRegistrationAtRuntime() {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
// Test that all benchmarks, registered at either during static init or runtime,
|
||||||
|
// are run and the results are passed to the reported.
|
||||||
|
void RunTestOne() {
|
||||||
TestRegistrationAtRuntime();
|
TestRegistrationAtRuntime();
|
||||||
|
|
||||||
benchmark::Initialize(&argc, argv);
|
|
||||||
|
|
||||||
TestReporter test_reporter;
|
TestReporter test_reporter;
|
||||||
benchmark::RunSpecifiedBenchmarks(&test_reporter);
|
benchmark::RunSpecifiedBenchmarks(&test_reporter);
|
||||||
|
|
||||||
@ -143,6 +143,40 @@ int main(int argc, char* argv[]) {
|
|||||||
++EB;
|
++EB;
|
||||||
}
|
}
|
||||||
assert(EB == ExpectedResults.end());
|
assert(EB == ExpectedResults.end());
|
||||||
|
}
|
||||||
return 0;
|
|
||||||
|
// Test that ClearRegisteredBenchmarks() clears all previously registered
|
||||||
|
// benchmarks.
|
||||||
|
// Also test that new benchmarks can be registered and ran afterwards.
|
||||||
|
void RunTestTwo() {
|
||||||
|
assert(ExpectedResults.size() != 0 &&
|
||||||
|
"must have at least one registered benchmark");
|
||||||
|
ExpectedResults.clear();
|
||||||
|
benchmark::ClearRegisteredBenchmarks();
|
||||||
|
|
||||||
|
TestReporter test_reporter;
|
||||||
|
size_t num_ran = benchmark::RunSpecifiedBenchmarks(&test_reporter);
|
||||||
|
assert(num_ran == 0);
|
||||||
|
assert(test_reporter.all_runs_.begin() == test_reporter.all_runs_.end());
|
||||||
|
|
||||||
|
TestRegistrationAtRuntime();
|
||||||
|
num_ran = benchmark::RunSpecifiedBenchmarks(&test_reporter);
|
||||||
|
assert(num_ran == ExpectedResults.size());
|
||||||
|
|
||||||
|
typedef benchmark::BenchmarkReporter::Run Run;
|
||||||
|
auto EB = ExpectedResults.begin();
|
||||||
|
|
||||||
|
for (Run const& run : test_reporter.all_runs_) {
|
||||||
|
assert(EB != ExpectedResults.end());
|
||||||
|
EB->CheckRun(run);
|
||||||
|
++EB;
|
||||||
|
}
|
||||||
|
assert(EB == ExpectedResults.end());
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char* argv[]) {
|
||||||
|
benchmark::Initialize(&argc, argv);
|
||||||
|
|
||||||
|
RunTestOne();
|
||||||
|
RunTestTwo();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user