perf_test_helpers.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2016 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_TRACING_TEST_PERF_TEST_HELPERS_H_
  5. #define COMPONENTS_TRACING_TEST_PERF_TEST_HELPERS_H_
  6. #include <stdint.h>
  7. #include <vector>
  8. #include "base/time/time.h"
  9. namespace tracing {
  10. // Measure time spent in a scope.
  11. // Must be used inside GTest case and result will be printed in perf_test format
  12. // with value name passed to constructor.
  13. class ScopedStopwatch {
  14. public:
  15. ScopedStopwatch(const std::string& metric);
  16. ScopedStopwatch(const ScopedStopwatch&) = delete;
  17. ScopedStopwatch& operator=(const ScopedStopwatch&) = delete;
  18. ~ScopedStopwatch();
  19. private:
  20. base::TimeTicks begin_;
  21. const std::string metric_;
  22. };
  23. // Measure median time of loop iterations.
  24. // Example:
  25. // IterableStopwatch stopwatch("foo");
  26. // for (int i = 0; i < 100; i++) {
  27. // ...
  28. // stopwatch.NextLap();
  29. // }
  30. // Must be used inside GTest case and result will be printed in perf_test format
  31. // with value name passed to constructor.
  32. class IterableStopwatch {
  33. public:
  34. IterableStopwatch(const std::string& metric);
  35. IterableStopwatch(const IterableStopwatch&) = delete;
  36. IterableStopwatch& operator=(const IterableStopwatch&) = delete;
  37. ~IterableStopwatch();
  38. void NextLap();
  39. private:
  40. base::TimeTicks begin_;
  41. std::vector<double> laps_;
  42. const std::string metric_;
  43. };
  44. } // namespace tracing
  45. #endif // COMPONENTS_TRACING_TEST_PERF_TEST_HELPERS_H_