Commit b8a2206f by Eric Committed by Dominic Hamon

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
parent d6aacaf4
......@@ -707,6 +707,10 @@ template <class Lambda>
internal::Benchmark* RegisterBenchmark(const char* name, Lambda&& fn);
#endif
// Remove all registered benchmarks. All pointers to previously registered
// benchmarks are invalidated.
void ClearRegisteredBenchmarks();
namespace internal {
// The class used to hold all Benchmarks created from static function.
// (ie those created using the BENCHMARK(...) macros.
......
......@@ -70,6 +70,9 @@ class BenchmarkFamilies {
// Registers a benchmark family and returns the index assigned to it.
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
// regular expression.
bool FindBenchmarks(const std::string& re,
......@@ -95,6 +98,12 @@ size_t BenchmarkFamilies::AddBenchmark(std::unique_ptr<Benchmark> family) {
return index;
}
void BenchmarkFamilies::ClearBenchmarks() {
MutexLock l(mutex_);
families_.clear();
families_.shrink_to_fit();
}
bool BenchmarkFamilies::FindBenchmarks(
const std::string& spec, std::vector<Benchmark::Instance>* benchmarks,
std::ostream* ErrStream) {
......@@ -450,4 +459,9 @@ int Benchmark::ArgsCnt() const {
void FunctionBenchmark::Run(State& st) { func_(st); }
} // end namespace internal
void ClearRegisteredBenchmarks() {
internal::BenchmarkFamilies::GetInstance()->ClearBenchmarks();
}
} // end namespace benchmark
......@@ -126,11 +126,11 @@ void TestRegistrationAtRuntime() {
#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();
benchmark::Initialize(&argc, argv);
TestReporter test_reporter;
benchmark::RunSpecifiedBenchmarks(&test_reporter);
......@@ -143,6 +143,40 @@ int main(int argc, char* argv[]) {
++EB;
}
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();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment