benchmark_api.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. // Support for registering benchmarks for functions.
  2. /* Example usage:
  3. // Define a function that executes the code to be measured a
  4. // specified number of times:
  5. static void BM_StringCreation(benchmark::State& state) {
  6. while (state.KeepRunning())
  7. std::string empty_string;
  8. }
  9. // Register the function as a benchmark
  10. BENCHMARK(BM_StringCreation);
  11. // Define another benchmark
  12. static void BM_StringCopy(benchmark::State& state) {
  13. std::string x = "hello";
  14. while (state.KeepRunning())
  15. std::string copy(x);
  16. }
  17. BENCHMARK(BM_StringCopy);
  18. // Augment the main() program to invoke benchmarks if specified
  19. // via the --benchmarks command line flag. E.g.,
  20. // my_unittest --benchmark_filter=all
  21. // my_unittest --benchmark_filter=BM_StringCreation
  22. // my_unittest --benchmark_filter=String
  23. // my_unittest --benchmark_filter='Copy|Creation'
  24. int main(int argc, char** argv) {
  25. benchmark::Initialize(&argc, argv);
  26. benchmark::RunSpecifiedBenchmarks();
  27. return 0;
  28. }
  29. // Sometimes a family of microbenchmarks can be implemented with
  30. // just one routine that takes an extra argument to specify which
  31. // one of the family of benchmarks to run. For example, the following
  32. // code defines a family of microbenchmarks for measuring the speed
  33. // of memcpy() calls of different lengths:
  34. static void BM_memcpy(benchmark::State& state) {
  35. char* src = new char[state.range_x()]; char* dst = new char[state.range_x()];
  36. memset(src, 'x', state.range_x());
  37. while (state.KeepRunning())
  38. memcpy(dst, src, state.range_x());
  39. state.SetBytesProcessed(int64_t(state.iterations()) *
  40. int64_t(state.range_x()));
  41. delete[] src; delete[] dst;
  42. }
  43. BENCHMARK(BM_memcpy)->Arg(8)->Arg(64)->Arg(512)->Arg(1<<10)->Arg(8<<10);
  44. // The preceding code is quite repetitive, and can be replaced with the
  45. // following short-hand. The following invocation will pick a few
  46. // appropriate arguments in the specified range and will generate a
  47. // microbenchmark for each such argument.
  48. BENCHMARK(BM_memcpy)->Range(8, 8<<10);
  49. // You might have a microbenchmark that depends on two inputs. For
  50. // example, the following code defines a family of microbenchmarks for
  51. // measuring the speed of set insertion.
  52. static void BM_SetInsert(benchmark::State& state) {
  53. while (state.KeepRunning()) {
  54. state.PauseTiming();
  55. set<int> data = ConstructRandomSet(state.range_x());
  56. state.ResumeTiming();
  57. for (int j = 0; j < state.range_y(); ++j)
  58. data.insert(RandomNumber());
  59. }
  60. }
  61. BENCHMARK(BM_SetInsert)
  62. ->ArgPair(1<<10, 1)
  63. ->ArgPair(1<<10, 8)
  64. ->ArgPair(1<<10, 64)
  65. ->ArgPair(1<<10, 512)
  66. ->ArgPair(8<<10, 1)
  67. ->ArgPair(8<<10, 8)
  68. ->ArgPair(8<<10, 64)
  69. ->ArgPair(8<<10, 512);
  70. // The preceding code is quite repetitive, and can be replaced with
  71. // the following short-hand. The following macro will pick a few
  72. // appropriate arguments in the product of the two specified ranges
  73. // and will generate a microbenchmark for each such pair.
  74. BENCHMARK(BM_SetInsert)->RangePair(1<<10, 8<<10, 1, 512);
  75. // For more complex patterns of inputs, passing a custom function
  76. // to Apply allows programmatic specification of an
  77. // arbitrary set of arguments to run the microbenchmark on.
  78. // The following example enumerates a dense range on
  79. // one parameter, and a sparse range on the second.
  80. static void CustomArguments(benchmark::internal::Benchmark* b) {
  81. for (int i = 0; i <= 10; ++i)
  82. for (int j = 32; j <= 1024*1024; j *= 8)
  83. b->ArgPair(i, j);
  84. }
  85. BENCHMARK(BM_SetInsert)->Apply(CustomArguments);
  86. // Templated microbenchmarks work the same way:
  87. // Produce then consume 'size' messages 'iters' times
  88. // Measures throughput in the absence of multiprogramming.
  89. template <class Q> int BM_Sequential(benchmark::State& state) {
  90. Q q;
  91. typename Q::value_type v;
  92. while (state.KeepRunning()) {
  93. for (int i = state.range_x(); i--; )
  94. q.push(v);
  95. for (int e = state.range_x(); e--; )
  96. q.Wait(&v);
  97. }
  98. // actually messages, not bytes:
  99. state.SetBytesProcessed(
  100. static_cast<int64_t>(state.iterations())*state.range_x());
  101. }
  102. BENCHMARK_TEMPLATE(BM_Sequential, WaitQueue<int>)->Range(1<<0, 1<<10);
  103. Use `Benchmark::MinTime(double t)` to set the minimum time used to run the
  104. benchmark. This option overrides the `benchmark_min_time` flag.
  105. void BM_test(benchmark::State& state) {
  106. ... body ...
  107. }
  108. BENCHMARK(BM_test)->MinTime(2.0); // Run for at least 2 seconds.
  109. In a multithreaded test, it is guaranteed that none of the threads will start
  110. until all have called KeepRunning, and all will have finished before KeepRunning
  111. returns false. As such, any global setup or teardown you want to do can be
  112. wrapped in a check against the thread index:
  113. static void BM_MultiThreaded(benchmark::State& state) {
  114. if (state.thread_index == 0) {
  115. // Setup code here.
  116. }
  117. while (state.KeepRunning()) {
  118. // Run the test as normal.
  119. }
  120. if (state.thread_index == 0) {
  121. // Teardown code here.
  122. }
  123. }
  124. BENCHMARK(BM_MultiThreaded)->Threads(4);
  125. If a benchmark runs a few milliseconds it may be hard to visually compare the
  126. measured times, since the output data is given in nanoseconds per default. In
  127. order to manually set the time unit, you can specify it manually:
  128. BENCHMARK(BM_test)->Unit(benchmark::kMillisecond);
  129. */
  130. #ifndef BENCHMARK_BENCHMARK_API_H_
  131. #define BENCHMARK_BENCHMARK_API_H_
  132. #include <assert.h>
  133. #include <stddef.h>
  134. #include <stdint.h>
  135. #include "macros.h"
  136. namespace benchmark {
  137. class BenchmarkReporter;
  138. void Initialize(int* argc, char** argv);
  139. // Generate a list of benchmarks matching the specified --benchmark_filter flag
  140. // and if --benchmark_list_tests is specified return after printing the name
  141. // of each matching benchmark. Otherwise run each matching benchmark and
  142. // report the results.
  143. //
  144. // The second overload reports the results using the specified 'reporter'.
  145. //
  146. // RETURNS: The number of matching benchmarks.
  147. size_t RunSpecifiedBenchmarks();
  148. size_t RunSpecifiedBenchmarks(BenchmarkReporter* reporter);
  149. // If this routine is called, peak memory allocation past this point in the
  150. // benchmark is reported at the end of the benchmark report line. (It is
  151. // computed by running the benchmark once with a single iteration and a memory
  152. // tracer.)
  153. // TODO(dominic)
  154. // void MemoryUsage();
  155. namespace internal {
  156. class Benchmark;
  157. class BenchmarkImp;
  158. class BenchmarkFamilies;
  159. template <class T> struct Voider {
  160. typedef void type;
  161. };
  162. template <class T, class = void>
  163. struct EnableIfString {};
  164. template <class T>
  165. struct EnableIfString<T, typename Voider<typename T::basic_string>::type> {
  166. typedef int type;
  167. };
  168. void UseCharPointer(char const volatile*);
  169. // Take ownership of the pointer and register the benchmark. Return the
  170. // registered benchmark.
  171. Benchmark* RegisterBenchmarkInternal(Benchmark*);
  172. } // end namespace internal
  173. // The DoNotOptimize(...) function can be used to prevent a value or
  174. // expression from being optimized away by the compiler. This function is
  175. // intended to add little to no overhead.
  176. // See: https://youtu.be/nXaxk27zwlk?t=2441
  177. #if defined(__GNUC__)
  178. template <class Tp>
  179. inline BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp const& value) {
  180. asm volatile("" : : "g"(value) : "memory");
  181. }
  182. // Force the compiler to flush pending writes to global memory. Acts as an
  183. // effective read/write barrier
  184. inline BENCHMARK_ALWAYS_INLINE void ClobberMemory() {
  185. asm volatile("" : : : "memory");
  186. }
  187. #else
  188. template <class Tp>
  189. inline BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp const& value) {
  190. internal::UseCharPointer(&reinterpret_cast<char const volatile&>(value));
  191. }
  192. // FIXME Add ClobberMemory() for non-gnu compilers
  193. #endif
  194. // TimeUnit is passed to a benchmark in order to specify the order of magnitude
  195. // for the measured time.
  196. enum TimeUnit {
  197. kNanosecond,
  198. kMicrosecond,
  199. kMillisecond
  200. };
  201. // BigO is passed to a benchmark in order to specify the asymptotic computational
  202. // complexity for the benchmark. In case oAuto is selected, complexity will be
  203. // calculated automatically to the best fit.
  204. enum BigO {
  205. oNone,
  206. o1,
  207. oN,
  208. oNSquared,
  209. oNCubed,
  210. oLogN,
  211. oNLogN,
  212. oAuto,
  213. oLambda
  214. };
  215. // BigOFunc is passed to a benchmark in order to specify the asymptotic
  216. // computational complexity for the benchmark.
  217. typedef double(BigOFunc)(int);
  218. // State is passed to a running Benchmark and contains state for the
  219. // benchmark to use.
  220. class State {
  221. public:
  222. State(size_t max_iters, bool has_x, int x, bool has_y, int y,
  223. int thread_i, int n_threads);
  224. // Returns true if the benchmark should continue through another iteration.
  225. // NOTE: A benchmark may not return from the test until KeepRunning() has
  226. // returned false.
  227. bool KeepRunning() {
  228. if (BENCHMARK_BUILTIN_EXPECT(!started_, false)) {
  229. assert(!finished_);
  230. started_ = true;
  231. ResumeTiming();
  232. }
  233. bool const res = total_iterations_++ < max_iterations;
  234. if (BENCHMARK_BUILTIN_EXPECT(!res, false)) {
  235. assert(started_ && (!finished_ || error_occurred_));
  236. if (!error_occurred_) {
  237. PauseTiming();
  238. }
  239. // Total iterations now is one greater than max iterations. Fix this.
  240. total_iterations_ = max_iterations;
  241. finished_ = true;
  242. }
  243. return res;
  244. }
  245. // REQUIRES: timer is running and 'SkipWithError(...)' has not been called
  246. // by the current thread.
  247. // Stop the benchmark timer. If not called, the timer will be
  248. // automatically stopped after KeepRunning() returns false for the first time.
  249. //
  250. // For threaded benchmarks the PauseTiming() function acts
  251. // like a barrier. I.e., the ith call by a particular thread to this
  252. // function will block until all active threads have made their ith call.
  253. // The timer will stop when the last thread has called this function.
  254. //
  255. // NOTE: PauseTiming()/ResumeTiming() are relatively
  256. // heavyweight, and so their use should generally be avoided
  257. // within each benchmark iteration, if possible.
  258. void PauseTiming();
  259. // REQUIRES: timer is not running and 'SkipWithError(...)' has not been called
  260. // by the current thread.
  261. // Start the benchmark timer. The timer is NOT running on entrance to the
  262. // benchmark function. It begins running after the first call to KeepRunning()
  263. //
  264. // For threaded benchmarks the ResumeTiming() function acts
  265. // like a barrier. I.e., the ith call by a particular thread to this
  266. // function will block until all active threads have made their ith call.
  267. // The timer will start when the last thread has called this function.
  268. //
  269. // NOTE: PauseTiming()/ResumeTiming() are relatively
  270. // heavyweight, and so their use should generally be avoided
  271. // within each benchmark iteration, if possible.
  272. void ResumeTiming();
  273. // REQUIRES: 'SkipWithError(...)' has not been called previously by the
  274. // current thread.
  275. // Skip any future iterations of the 'KeepRunning()' loop in the current
  276. // thread and report an error with the specified 'msg'. After this call
  277. // the user may explicitly 'return' from the benchmark.
  278. //
  279. // For threaded benchmarks only the current thread stops executing. If
  280. // multiple threads report an error only the first error message is used.
  281. // The current thread is no longer considered 'active' by
  282. // 'PauseTiming()' and 'ResumingTiming()'.
  283. //
  284. // NOTE: Calling 'SkipWithError(...)' does not cause the benchmark to exit
  285. // the current scope immediately. If the function is called from within
  286. // the 'KeepRunning()' loop the current iteration will finish. It is the users
  287. // responsibility to exit the scope as needed.
  288. void SkipWithError(const char* msg);
  289. // REQUIRES: called exactly once per iteration of the KeepRunning loop.
  290. // Set the manually measured time for this benchmark iteration, which
  291. // is used instead of automatically measured time if UseManualTime() was
  292. // specified.
  293. //
  294. // For threaded benchmarks the SetIterationTime() function acts
  295. // like a barrier. I.e., the ith call by a particular thread to this
  296. // function will block until all threads have made their ith call.
  297. // The time will be set by the last thread to call this function.
  298. void SetIterationTime(double seconds);
  299. // Set the number of bytes processed by the current benchmark
  300. // execution. This routine is typically called once at the end of a
  301. // throughput oriented benchmark. If this routine is called with a
  302. // value > 0, the report is printed in MB/sec instead of nanoseconds
  303. // per iteration.
  304. //
  305. // REQUIRES: a benchmark has exited its KeepRunning loop.
  306. BENCHMARK_ALWAYS_INLINE
  307. void SetBytesProcessed(size_t bytes) {
  308. bytes_processed_ = bytes;
  309. }
  310. BENCHMARK_ALWAYS_INLINE
  311. size_t bytes_processed() const {
  312. return bytes_processed_;
  313. }
  314. // If this routine is called with complexity_n > 0 and complexity report is requested for the
  315. // family benchmark, then current benchmark will be part of the computation and complexity_n will
  316. // represent the length of N.
  317. BENCHMARK_ALWAYS_INLINE
  318. void SetComplexityN(int complexity_n) {
  319. complexity_n_ = complexity_n;
  320. }
  321. BENCHMARK_ALWAYS_INLINE
  322. size_t complexity_length_n() {
  323. return complexity_n_;
  324. }
  325. // If this routine is called with items > 0, then an items/s
  326. // label is printed on the benchmark report line for the currently
  327. // executing benchmark. It is typically called at the end of a processing
  328. // benchmark where a processing items/second output is desired.
  329. //
  330. // REQUIRES: a benchmark has exited its KeepRunning loop.
  331. BENCHMARK_ALWAYS_INLINE
  332. void SetItemsProcessed(size_t items) {
  333. items_processed_ = items;
  334. }
  335. BENCHMARK_ALWAYS_INLINE
  336. size_t items_processed() const {
  337. return items_processed_;
  338. }
  339. // If this routine is called, the specified label is printed at the
  340. // end of the benchmark report line for the currently executing
  341. // benchmark. Example:
  342. // static void BM_Compress(benchmark::State& state) {
  343. // ...
  344. // double compress = input_size / output_size;
  345. // state.SetLabel(StringPrintf("compress:%.1f%%", 100.0*compression));
  346. // }
  347. // Produces output that looks like:
  348. // BM_Compress 50 50 14115038 compress:27.3%
  349. //
  350. // REQUIRES: a benchmark has exited its KeepRunning loop.
  351. void SetLabel(const char* label);
  352. // Allow the use of std::string without actually including <string>.
  353. // This function does not participate in overload resolution unless StringType
  354. // has the nested typename `basic_string`. This typename should be provided
  355. // as an injected class name in the case of std::string.
  356. template <class StringType>
  357. void SetLabel(StringType const & str,
  358. typename internal::EnableIfString<StringType>::type = 1) {
  359. this->SetLabel(str.c_str());
  360. }
  361. // Range arguments for this run. CHECKs if the argument has been set.
  362. BENCHMARK_ALWAYS_INLINE
  363. int range_x() const {
  364. assert(has_range_x_);
  365. ((void)has_range_x_); // Prevent unused warning.
  366. return range_x_;
  367. }
  368. BENCHMARK_ALWAYS_INLINE
  369. int range_y() const {
  370. assert(has_range_y_);
  371. ((void)has_range_y_); // Prevent unused warning.
  372. return range_y_;
  373. }
  374. BENCHMARK_ALWAYS_INLINE
  375. size_t iterations() const { return total_iterations_; }
  376. private:
  377. bool started_;
  378. bool finished_;
  379. size_t total_iterations_;
  380. bool has_range_x_;
  381. int range_x_;
  382. bool has_range_y_;
  383. int range_y_;
  384. size_t bytes_processed_;
  385. size_t items_processed_;
  386. int complexity_n_;
  387. public:
  388. // FIXME: Make this private somehow.
  389. bool error_occurred_;
  390. public:
  391. // Index of the executing thread. Values from [0, threads).
  392. const int thread_index;
  393. // Number of threads concurrently executing the benchmark.
  394. const int threads;
  395. const size_t max_iterations;
  396. private:
  397. BENCHMARK_DISALLOW_COPY_AND_ASSIGN(State);
  398. };
  399. namespace internal {
  400. typedef void(Function)(State&);
  401. // ------------------------------------------------------
  402. // Benchmark registration object. The BENCHMARK() macro expands
  403. // into an internal::Benchmark* object. Various methods can
  404. // be called on this object to change the properties of the benchmark.
  405. // Each method returns "this" so that multiple method calls can
  406. // chained into one expression.
  407. class Benchmark {
  408. public:
  409. virtual ~Benchmark();
  410. // Note: the following methods all return "this" so that multiple
  411. // method calls can be chained together in one expression.
  412. // Run this benchmark once with "x" as the extra argument passed
  413. // to the function.
  414. // REQUIRES: The function passed to the constructor must accept an arg1.
  415. Benchmark* Arg(int x);
  416. // Run this benchmark with the given time unit for the generated output report
  417. Benchmark* Unit(TimeUnit unit);
  418. // Run this benchmark once for a number of values picked from the
  419. // range [start..limit]. (start and limit are always picked.)
  420. // REQUIRES: The function passed to the constructor must accept an arg1.
  421. Benchmark* Range(int start, int limit);
  422. // Run this benchmark once for every value in the range [start..limit]
  423. // REQUIRES: The function passed to the constructor must accept an arg1.
  424. Benchmark* DenseRange(int start, int limit);
  425. // Run this benchmark once with "x,y" as the extra arguments passed
  426. // to the function.
  427. // REQUIRES: The function passed to the constructor must accept arg1,arg2.
  428. Benchmark* ArgPair(int x, int y);
  429. // Pick a set of values A from the range [lo1..hi1] and a set
  430. // of values B from the range [lo2..hi2]. Run the benchmark for
  431. // every pair of values in the cartesian product of A and B
  432. // (i.e., for all combinations of the values in A and B).
  433. // REQUIRES: The function passed to the constructor must accept arg1,arg2.
  434. Benchmark* RangePair(int lo1, int hi1, int lo2, int hi2);
  435. // Pass this benchmark object to *func, which can customize
  436. // the benchmark by calling various methods like Arg, ArgPair,
  437. // Threads, etc.
  438. Benchmark* Apply(void (*func)(Benchmark* benchmark));
  439. // Set the range multiplier for non-dense range. If not called, the range multiplier
  440. // kRangeMultiplier will be used.
  441. Benchmark* RangeMultiplier(int multiplier);
  442. // Set the minimum amount of time to use when running this benchmark. This
  443. // option overrides the `benchmark_min_time` flag.
  444. // REQUIRES: `t > 0`
  445. Benchmark* MinTime(double t);
  446. // Specify the amount of times to repeat this benchmark. This option overrides
  447. // the `benchmark_repetitions` flag.
  448. // REQUIRES: `n > 0`
  449. Benchmark* Repetitions(int n);
  450. // If a particular benchmark is I/O bound, runs multiple threads internally or
  451. // if for some reason CPU timings are not representative, call this method. If
  452. // called, the elapsed time will be used to control how many iterations are
  453. // run, and in the printing of items/second or MB/seconds values. If not
  454. // called, the cpu time used by the benchmark will be used.
  455. Benchmark* UseRealTime();
  456. // If a benchmark must measure time manually (e.g. if GPU execution time is being
  457. // measured), call this method. If called, each benchmark iteration should call
  458. // SetIterationTime(seconds) to report the measured time, which will be used
  459. // to control how many iterations are run, and in the printing of items/second
  460. // or MB/second values.
  461. Benchmark* UseManualTime();
  462. // Set the asymptotic computational complexity for the benchmark. If called
  463. // the asymptotic computational complexity will be shown on the output.
  464. Benchmark* Complexity(BigO complexity = benchmark::oAuto);
  465. // Set the asymptotic computational complexity for the benchmark. If called
  466. // the asymptotic computational complexity will be shown on the output.
  467. Benchmark* Complexity(BigOFunc* complexity);
  468. // Support for running multiple copies of the same benchmark concurrently
  469. // in multiple threads. This may be useful when measuring the scaling
  470. // of some piece of code.
  471. // Run one instance of this benchmark concurrently in t threads.
  472. Benchmark* Threads(int t);
  473. // Pick a set of values T from [min_threads,max_threads].
  474. // min_threads and max_threads are always included in T. Run this
  475. // benchmark once for each value in T. The benchmark run for a
  476. // particular value t consists of t threads running the benchmark
  477. // function concurrently. For example, consider:
  478. // BENCHMARK(Foo)->ThreadRange(1,16);
  479. // This will run the following benchmarks:
  480. // Foo in 1 thread
  481. // Foo in 2 threads
  482. // Foo in 4 threads
  483. // Foo in 8 threads
  484. // Foo in 16 threads
  485. Benchmark* ThreadRange(int min_threads, int max_threads);
  486. // Equivalent to ThreadRange(NumCPUs(), NumCPUs())
  487. Benchmark* ThreadPerCpu();
  488. virtual void Run(State& state) = 0;
  489. // Used inside the benchmark implementation
  490. struct Instance;
  491. protected:
  492. explicit Benchmark(const char* name);
  493. Benchmark(Benchmark const&);
  494. void SetName(const char* name);
  495. private:
  496. friend class BenchmarkFamilies;
  497. BenchmarkImp* imp_;
  498. Benchmark& operator=(Benchmark const&);
  499. };
  500. // The class used to hold all Benchmarks created from static function.
  501. // (ie those created using the BENCHMARK(...) macros.
  502. class FunctionBenchmark : public Benchmark {
  503. public:
  504. FunctionBenchmark(const char* name, Function* func)
  505. : Benchmark(name), func_(func)
  506. {}
  507. virtual void Run(State& st);
  508. private:
  509. Function* func_;
  510. };
  511. } // end namespace internal
  512. // The base class for all fixture tests.
  513. class Fixture: public internal::Benchmark {
  514. public:
  515. Fixture() : internal::Benchmark("") {}
  516. virtual void Run(State& st) {
  517. this->SetUp(st);
  518. this->BenchmarkCase(st);
  519. this->TearDown(st);
  520. }
  521. virtual void SetUp(const State&) {}
  522. virtual void TearDown(const State&) {}
  523. protected:
  524. virtual void BenchmarkCase(State&) = 0;
  525. };
  526. } // end namespace benchmark
  527. // ------------------------------------------------------
  528. // Macro to register benchmarks
  529. // Check that __COUNTER__ is defined and that __COUNTER__ increases by 1
  530. // every time it is expanded. X + 1 == X + 0 is used in case X is defined to be
  531. // empty. If X is empty the expression becomes (+1 == +0).
  532. #if defined(__COUNTER__) && (__COUNTER__ + 1 == __COUNTER__ + 0)
  533. #define BENCHMARK_PRIVATE_UNIQUE_ID __COUNTER__
  534. #else
  535. #define BENCHMARK_PRIVATE_UNIQUE_ID __LINE__
  536. #endif
  537. // Helpers for generating unique variable names
  538. #define BENCHMARK_PRIVATE_NAME(n) \
  539. BENCHMARK_PRIVATE_CONCAT(_benchmark_, BENCHMARK_PRIVATE_UNIQUE_ID, n)
  540. #define BENCHMARK_PRIVATE_CONCAT(a, b, c) BENCHMARK_PRIVATE_CONCAT2(a, b, c)
  541. #define BENCHMARK_PRIVATE_CONCAT2(a, b, c) a##b##c
  542. #define BENCHMARK_PRIVATE_DECLARE(n) \
  543. static ::benchmark::internal::Benchmark* \
  544. BENCHMARK_PRIVATE_NAME(n) BENCHMARK_UNUSED
  545. #define BENCHMARK(n) \
  546. BENCHMARK_PRIVATE_DECLARE(n) = \
  547. (::benchmark::internal::RegisterBenchmarkInternal( \
  548. new ::benchmark::internal::FunctionBenchmark(#n, n)))
  549. // Old-style macros
  550. #define BENCHMARK_WITH_ARG(n, a) BENCHMARK(n)->Arg((a))
  551. #define BENCHMARK_WITH_ARG2(n, a1, a2) BENCHMARK(n)->ArgPair((a1), (a2))
  552. #define BENCHMARK_WITH_UNIT(n, t) BENCHMARK(n)->Unit((t))
  553. #define BENCHMARK_RANGE(n, lo, hi) BENCHMARK(n)->Range((lo), (hi))
  554. #define BENCHMARK_RANGE2(n, l1, h1, l2, h2) \
  555. BENCHMARK(n)->RangePair((l1), (h1), (l2), (h2))
  556. #if __cplusplus >= 201103L
  557. // Register a benchmark which invokes the function specified by `func`
  558. // with the additional arguments specified by `...`.
  559. //
  560. // For example:
  561. //
  562. // template <class ...ExtraArgs>`
  563. // void BM_takes_args(benchmark::State& state, ExtraArgs&&... extra_args) {
  564. // [...]
  565. //}
  566. // /* Registers a benchmark named "BM_takes_args/int_string_test` */
  567. // BENCHMARK_CAPTURE(BM_takes_args, int_string_test, 42, std::string("abc"));
  568. #define BENCHMARK_CAPTURE(func, test_case_name, ...) \
  569. BENCHMARK_PRIVATE_DECLARE(func) = \
  570. (::benchmark::internal::RegisterBenchmarkInternal( \
  571. new ::benchmark::internal::FunctionBenchmark( \
  572. #func "/" #test_case_name, \
  573. [](::benchmark::State& st) { func(st, __VA_ARGS__); })))
  574. #endif // __cplusplus >= 11
  575. // This will register a benchmark for a templatized function. For example:
  576. //
  577. // template<int arg>
  578. // void BM_Foo(int iters);
  579. //
  580. // BENCHMARK_TEMPLATE(BM_Foo, 1);
  581. //
  582. // will register BM_Foo<1> as a benchmark.
  583. #define BENCHMARK_TEMPLATE1(n, a) \
  584. BENCHMARK_PRIVATE_DECLARE(n) = \
  585. (::benchmark::internal::RegisterBenchmarkInternal( \
  586. new ::benchmark::internal::FunctionBenchmark(#n "<" #a ">", n<a>)))
  587. #define BENCHMARK_TEMPLATE2(n, a, b) \
  588. BENCHMARK_PRIVATE_DECLARE(n) = \
  589. (::benchmark::internal::RegisterBenchmarkInternal( \
  590. new ::benchmark::internal::FunctionBenchmark( \
  591. #n "<" #a "," #b ">", n<a, b>)))
  592. #if __cplusplus >= 201103L
  593. #define BENCHMARK_TEMPLATE(n, ...) \
  594. BENCHMARK_PRIVATE_DECLARE(n) = \
  595. (::benchmark::internal::RegisterBenchmarkInternal( \
  596. new ::benchmark::internal::FunctionBenchmark( \
  597. #n "<" #__VA_ARGS__ ">", n<__VA_ARGS__>)))
  598. #else
  599. #define BENCHMARK_TEMPLATE(n, a) BENCHMARK_TEMPLATE1(n, a)
  600. #endif
  601. #define BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \
  602. class BaseClass##_##Method##_Benchmark : public BaseClass { \
  603. public: \
  604. BaseClass##_##Method##_Benchmark() : BaseClass() { \
  605. this->SetName(#BaseClass "/" #Method);} \
  606. protected: \
  607. virtual void BenchmarkCase(::benchmark::State&); \
  608. };
  609. #define BENCHMARK_DEFINE_F(BaseClass, Method) \
  610. BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \
  611. void BaseClass##_##Method##_Benchmark::BenchmarkCase
  612. #define BENCHMARK_REGISTER_F(BaseClass, Method) \
  613. BENCHMARK_PRIVATE_REGISTER_F(BaseClass##_##Method##_Benchmark)
  614. #define BENCHMARK_PRIVATE_REGISTER_F(TestName) \
  615. BENCHMARK_PRIVATE_DECLARE(TestName) = \
  616. (::benchmark::internal::RegisterBenchmarkInternal(new TestName()))
  617. // This macro will define and register a benchmark within a fixture class.
  618. #define BENCHMARK_F(BaseClass, Method) \
  619. BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \
  620. BENCHMARK_REGISTER_F(BaseClass, Method); \
  621. void BaseClass##_##Method##_Benchmark::BenchmarkCase
  622. // Helper macro to create a main routine in a test that runs the benchmarks
  623. #define BENCHMARK_MAIN() \
  624. int main(int argc, char** argv) { \
  625. ::benchmark::Initialize(&argc, argv); \
  626. ::benchmark::RunSpecifiedBenchmarks(); \
  627. }
  628. #endif // BENCHMARK_BENCHMARK_API_H_