Skip to content

Commit b8a2206

Browse files
EricWFdominichamon
authored andcommitted
Add ClearRegisteredBenchmark() function. (google#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 google#400, at least partially. * Remove unused change
1 parent d6aacaf commit b8a2206

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

include/benchmark/benchmark_api.h

+4
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,10 @@ template <class Lambda>
707707
internal::Benchmark* RegisterBenchmark(const char* name, Lambda&& fn);
708708
#endif
709709

710+
// Remove all registered benchmarks. All pointers to previously registered
711+
// benchmarks are invalidated.
712+
void ClearRegisteredBenchmarks();
713+
710714
namespace internal {
711715
// The class used to hold all Benchmarks created from static function.
712716
// (ie those created using the BENCHMARK(...) macros.

src/benchmark_register.cc

+14
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ class BenchmarkFamilies {
7070
// Registers a benchmark family and returns the index assigned to it.
7171
size_t AddBenchmark(std::unique_ptr<Benchmark> family);
7272

73+
// Clear all registered benchmark families.
74+
void ClearBenchmarks();
75+
7376
// Extract the list of benchmark instances that match the specified
7477
// regular expression.
7578
bool FindBenchmarks(const std::string& re,
@@ -95,6 +98,12 @@ size_t BenchmarkFamilies::AddBenchmark(std::unique_ptr<Benchmark> family) {
9598
return index;
9699
}
97100

101+
void BenchmarkFamilies::ClearBenchmarks() {
102+
MutexLock l(mutex_);
103+
families_.clear();
104+
families_.shrink_to_fit();
105+
}
106+
98107
bool BenchmarkFamilies::FindBenchmarks(
99108
const std::string& spec, std::vector<Benchmark::Instance>* benchmarks,
100109
std::ostream* ErrStream) {
@@ -450,4 +459,9 @@ int Benchmark::ArgsCnt() const {
450459
void FunctionBenchmark::Run(State& st) { func_(st); }
451460

452461
} // end namespace internal
462+
463+
void ClearRegisteredBenchmarks() {
464+
internal::BenchmarkFamilies::GetInstance()->ClearBenchmarks();
465+
}
466+
453467
} // end namespace benchmark

test/register_benchmark_test.cc

+38-4
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,11 @@ void TestRegistrationAtRuntime() {
126126
#endif
127127
}
128128

129-
int main(int argc, char* argv[]) {
129+
// Test that all benchmarks, registered at either during static init or runtime,
130+
// are run and the results are passed to the reported.
131+
void RunTestOne() {
130132
TestRegistrationAtRuntime();
131133

132-
benchmark::Initialize(&argc, argv);
133-
134134
TestReporter test_reporter;
135135
benchmark::RunSpecifiedBenchmarks(&test_reporter);
136136

@@ -143,6 +143,40 @@ int main(int argc, char* argv[]) {
143143
++EB;
144144
}
145145
assert(EB == ExpectedResults.end());
146+
}
146147

147-
return 0;
148+
// Test that ClearRegisteredBenchmarks() clears all previously registered
149+
// benchmarks.
150+
// Also test that new benchmarks can be registered and ran afterwards.
151+
void RunTestTwo() {
152+
assert(ExpectedResults.size() != 0 &&
153+
"must have at least one registered benchmark");
154+
ExpectedResults.clear();
155+
benchmark::ClearRegisteredBenchmarks();
156+
157+
TestReporter test_reporter;
158+
size_t num_ran = benchmark::RunSpecifiedBenchmarks(&test_reporter);
159+
assert(num_ran == 0);
160+
assert(test_reporter.all_runs_.begin() == test_reporter.all_runs_.end());
161+
162+
TestRegistrationAtRuntime();
163+
num_ran = benchmark::RunSpecifiedBenchmarks(&test_reporter);
164+
assert(num_ran == ExpectedResults.size());
165+
166+
typedef benchmark::BenchmarkReporter::Run Run;
167+
auto EB = ExpectedResults.begin();
168+
169+
for (Run const& run : test_reporter.all_runs_) {
170+
assert(EB != ExpectedResults.end());
171+
EB->CheckRun(run);
172+
++EB;
173+
}
174+
assert(EB == ExpectedResults.end());
175+
}
176+
177+
int main(int argc, char* argv[]) {
178+
benchmark::Initialize(&argc, argv);
179+
180+
RunTestOne();
181+
RunTestTwo();
148182
}

0 commit comments

Comments
 (0)