am f8eec1e5: Merge "Allow wildcards to match arg values."

* commit 'f8eec1e54fbfb7b0304b73cb9ce2de44760a672e':
  Allow wildcards to match arg values.
This commit is contained in:
Christopher Ferris 2015-02-26 03:52:35 +00:00 committed by Android Git Automerger
commit e6c7e29482
3 changed files with 51 additions and 31 deletions

View File

@ -39,40 +39,45 @@ static uint64_t NanoTime() {
return static_cast<uint64_t>(t.tv_sec) * 1000000000LL + t.tv_nsec; return static_cast<uint64_t>(t.tv_sec) * 1000000000LL + t.tv_nsec;
} }
bool Benchmark::header_printed_;
std::vector<Benchmark*>& Benchmark::List() { std::vector<Benchmark*>& Benchmark::List() {
static std::vector<Benchmark*> list; static std::vector<Benchmark*> list;
return list; return list;
} }
int Benchmark::MaxNameColumnWidth() { int Benchmark::MaxNameColumnWidth() {
int max = 20; size_t max = 20;
for (auto& benchmark : List()) { for (auto& benchmark : List()) {
max = std::max(max, benchmark->NameColumnWidth()); max = std::max(max, benchmark->NameColumnWidth());
} }
return max; return static_cast<int>(max);
} }
bool Benchmark::RunAll(std::vector<regex_t*>& regs) { size_t Benchmark::RunAll(std::vector<regex_t*>& regs) {
bool ran_benchmark = false; size_t benchmarks_run = 0;
header_printed_ = false;
for (auto& benchmark : List()) { for (auto& benchmark : List()) {
if (benchmark->ShouldRun(regs)) { benchmarks_run += benchmark->RunAllArgs(regs);
if (!ran_benchmark) {
printf("%-*s %10s %10s\n", MaxNameColumnWidth(), "", "iterations", "ns/op");
ran_benchmark = true;
} }
benchmark->RunAll(); return benchmarks_run;
}
}
return ran_benchmark;
} }
bool Benchmark::ShouldRun(std::vector<regex_t*>& regs) { void Benchmark::PrintHeader() {
if (!header_printed_) {
printf("%-*s %10s %10s\n", MaxNameColumnWidth(), "", "iterations", "ns/op");
header_printed_ = true;
}
}
template <typename T>
bool BenchmarkT<T>::ShouldRun(std::vector<regex_t*>& regs, T arg) {
if (regs.empty()) { if (regs.empty()) {
return true; return true;
} }
for (const auto& re : regs) { for (const auto& re : regs) {
if (regexec(re, Name().c_str(), 0, NULL, 0) != REG_NOMATCH) { if (regexec(re, GetNameStr(arg).c_str(), 0, NULL, 0) != REG_NOMATCH) {
return true; return true;
} }
} }

View File

@ -34,27 +34,29 @@ public:
virtual std::string Name() = 0; virtual std::string Name() = 0;
virtual void RunAll() = 0; virtual size_t RunAllArgs(std::vector<regex_t*>&) = 0;
bool ShouldRun(std::vector<regex_t*>&);
void SetBenchmarkBytesProcessed(uint64_t bytes) { bytes_processed_ += bytes; } void SetBenchmarkBytesProcessed(uint64_t bytes) { bytes_processed_ += bytes; }
void StopBenchmarkTiming(); void StopBenchmarkTiming();
void StartBenchmarkTiming(); void StartBenchmarkTiming();
// Run all of the benchmarks that have registered. // Run all of the benchmarks that have registered.
static bool RunAll(std::vector<regex_t*>&); static size_t RunAll(std::vector<regex_t*>&);
static std::vector<Benchmark*>& List(); static std::vector<Benchmark*>& List();
static int MaxNameColumnWidth(); static int MaxNameColumnWidth();
protected: protected:
virtual int NameColumnWidth() = 0; virtual size_t NameColumnWidth() = 0;
uint64_t bytes_processed_; uint64_t bytes_processed_;
uint64_t total_time_ns_; uint64_t total_time_ns_;
uint64_t start_time_ns_; uint64_t start_time_ns_;
static bool header_printed_;
static void PrintHeader();
}; };
template <typename T> template <typename T>
@ -64,6 +66,7 @@ public:
virtual ~BenchmarkT() {} virtual ~BenchmarkT() {}
protected: protected:
bool ShouldRun(std::vector<regex_t*>&, T arg);
void RunWithArg(T arg); void RunWithArg(T arg);
virtual void RunIterations(int, T) = 0; virtual void RunIterations(int, T) = 0;
virtual std::string GetNameStr(T) = 0; virtual std::string GetNameStr(T) = 0;
@ -75,8 +78,14 @@ public:
virtual ~BenchmarkWithoutArg() {} virtual ~BenchmarkWithoutArg() {}
protected: protected:
virtual void RunAll() override { virtual size_t RunAllArgs(std::vector<regex_t*>& regs) override {
size_t benchmarks_run = 0;
if (BenchmarkT<void*>::ShouldRun(regs, nullptr)) {
PrintHeader();
RunWithArg(nullptr); RunWithArg(nullptr);
benchmarks_run++;
}
return benchmarks_run;
} }
virtual void RunIterations(int iters, void*) override { virtual void RunIterations(int iters, void*) override {
@ -85,8 +94,8 @@ protected:
virtual void Run(int) = 0; virtual void Run(int) = 0;
virtual int NameColumnWidth() override { virtual size_t NameColumnWidth() override {
return (int)Name().size(); return Name().size();
} }
virtual std::string GetNameStr(void *) override; virtual std::string GetNameStr(void *) override;
@ -104,21 +113,27 @@ public:
} }
protected: protected:
virtual int NameColumnWidth() override { virtual size_t NameColumnWidth() override {
int max = 0; size_t max = 0;
for (const auto arg : args_) { for (const auto& arg : args_) {
max = std::max(max, (int)GetNameStr(arg).size()); max = std::max(max, GetNameStr(arg).size());
} }
return max; return max;
} }
std::string GetNameStr(T arg) override; std::string GetNameStr(T arg) override;
virtual void RunAll() override { virtual size_t RunAllArgs(std::vector<regex_t*>& regs) override {
for (T arg : args_) { size_t benchmarks_run = 0;
for (T& arg : args_) {
if (BenchmarkT<T>::ShouldRun(regs, arg)) {
Benchmark::PrintHeader();
BenchmarkT<T>::RunWithArg(arg); BenchmarkT<T>::RunWithArg(arg);
benchmarks_run++;
} }
} }
return benchmarks_run;
}
virtual void RunIterations(int iters, T arg) override { virtual void RunIterations(int iters, T arg) override {
Run(iters, arg); Run(iters, arg);

View File

@ -48,7 +48,7 @@ int main(int argc, char* argv[]) {
regs.push_back(re); regs.push_back(re);
} }
if (!::testing::Benchmark::RunAll(regs)) { if (::testing::Benchmark::RunAll(regs) == 0) {
fprintf(stderr, "No matching benchmarks!\n"); fprintf(stderr, "No matching benchmarks!\n");
fprintf(stderr, "Available benchmarks:\n"); fprintf(stderr, "Available benchmarks:\n");
for (const auto& benchmark : ::testing::Benchmark::List()) { for (const auto& benchmark : ::testing::Benchmark::List()) {