benchmark.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. #include <stddef.h>
  17. #include <stdint.h>
  18. #include <vector>
  19. namespace testing {
  20. class Benchmark {
  21. public:
  22. Benchmark(const char* name, void (*fn)(int)) {
  23. Register(name, fn, NULL);
  24. }
  25. Benchmark(const char* name, void (*fn_range)(int, int)) {
  26. Register(name, NULL, fn_range);
  27. }
  28. Benchmark* Arg(int x);
  29. Benchmark* Range(int lo, int hi);
  30. const char* Name();
  31. bool ShouldRun(int argc, char* argv[]);
  32. void Run();
  33. private:
  34. const char* name_;
  35. void (*fn_)(int);
  36. void (*fn_range_)(int, int);
  37. std::vector<int> args_;
  38. void Register(const char* name, void (*fn)(int), void (*fn_range)(int, int));
  39. void RunRepeatedlyWithArg(int iterations, int arg);
  40. void RunWithArg(int arg);
  41. };
  42. } // namespace testing
  43. void SetBenchmarkFlopsProcessed(int64_t);
  44. void StopBenchmarkTiming();
  45. void StartBenchmarkTiming();
  46. #define BENCHMARK(f) \
  47. static ::testing::Benchmark* _benchmark_##f __attribute__((unused)) = \
  48. (new ::testing::Benchmark(#f, f))