Benchmark.h revision 567bfb3779f238784be6d3fa9d384ecdc423ea39
1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef BENCHMARKS_BENCHMARK_H_
18#define BENCHMARKS_BENCHMARK_H_
19
20#include <regex.h>
21#include <stdint.h>
22
23#include <string>
24#include <vector>
25
26namespace testing {
27
28class Benchmark {
29public:
30  Benchmark() {
31    List().push_back(this);
32  }
33  virtual ~Benchmark() {}
34
35  virtual std::string Name() = 0;
36
37  virtual void RunAll() = 0;
38
39  bool ShouldRun(std::vector<regex_t*>&);
40
41  void SetBenchmarkBytesProcessed(uint64_t bytes) { bytes_processed_ += bytes; }
42  void StopBenchmarkTiming();
43  void StartBenchmarkTiming();
44
45  // Run all of the benchmarks that have registered.
46  static bool RunAll(std::vector<regex_t*>&);
47
48  static std::vector<Benchmark*>& List();
49
50  static int MaxNameColumnWidth();
51
52protected:
53  virtual int NameColumnWidth() = 0;
54
55  uint64_t bytes_processed_;
56  uint64_t total_time_ns_;
57  uint64_t start_time_ns_;
58};
59
60template <typename T>
61class BenchmarkT : public Benchmark {
62public:
63  BenchmarkT() {}
64  virtual ~BenchmarkT() {}
65
66protected:
67  void RunWithArg(T arg);
68  virtual void RunIterations(int, T) = 0;
69  virtual std::string GetNameStr(T) = 0;
70};
71
72class BenchmarkWithoutArg : public BenchmarkT<void*> {
73public:
74  BenchmarkWithoutArg() {}
75  virtual ~BenchmarkWithoutArg() {}
76
77protected:
78  virtual void RunAll() override {
79    RunWithArg(nullptr);
80  }
81
82  virtual void RunIterations(int iters, void*) override {
83    Run(iters);
84  }
85
86  virtual void Run(int) = 0;
87
88  virtual int NameColumnWidth() override {
89    return (int)Name().size();
90  }
91
92  virtual std::string GetNameStr(void *) override;
93};
94
95template<typename T>
96class BenchmarkWithArg : public BenchmarkT<T> {
97public:
98  BenchmarkWithArg() {}
99  virtual ~BenchmarkWithArg() {}
100
101  BenchmarkWithArg* Arg(T arg) {
102    args_.push_back(arg);
103    return this;
104  }
105
106protected:
107  virtual int NameColumnWidth() override {
108    int max = 0;
109    for (const auto arg : args_) {
110      max = std::max(max, (int)GetNameStr(arg).size());
111    }
112    return max;
113  }
114
115  std::string GetNameStr(T arg) override;
116
117  virtual void RunAll() override {
118    for (T arg : args_) {
119      BenchmarkT<T>::RunWithArg(arg);
120    }
121  }
122
123  virtual void RunIterations(int iters, T arg) override {
124    Run(iters, arg);
125  }
126
127  virtual void Run(int iters, T arg) = 0;
128
129private:
130  std::vector<T> args_;
131};
132
133}  // namespace testing
134
135#define BENCHMARK_START(f, super_class) \
136  class f : public super_class { \
137  public: \
138    f() {} \
139    virtual ~f() {} \
140    virtual std::string Name() override { return #f; } \
141
142#define BENCHMARK_NO_ARG(f) \
143  BENCHMARK_START(f, ::testing::BenchmarkWithoutArg) \
144    virtual void Run(int) override; \
145  }; \
146  static ::testing::Benchmark* __benchmark_##f = new f()
147
148#define BENCHMARK_WITH_ARG(f, arg_type) \
149  BENCHMARK_START(f, ::testing::BenchmarkWithArg<arg_type>) \
150    virtual void Run(int, arg_type) override; \
151  }; \
152  static ::testing::BenchmarkWithArg<arg_type>* __benchmark_##f = (new f())
153
154#endif  // BENCHMARKS_BENCHMARK_H_
155