kraynov | 96d0ab95 | 2016-10-12 14:46:58 | [diff] [blame] | 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 | |
| 5 | #ifndef COMPONENTS_TRACING_TEST_PERF_TEST_HELPERS_H_ |
| 6 | #define COMPONENTS_TRACING_TEST_PERF_TEST_HELPERS_H_ |
| 7 | |
| 8 | #include <stdint.h> |
| 9 | |
| 10 | #include <vector> |
| 11 | |
| 12 | #include "base/macros.h" |
| 13 | #include "base/time/time.h" |
| 14 | |
| 15 | namespace tracing { |
| 16 | |
| 17 | // Measure time spent in a scope. |
| 18 | // Must be used inside GTest case and result will be printed in perf_test format |
| 19 | // with value name passed to constructor. |
| 20 | class ScopedStopwatch { |
| 21 | public: |
Brian Sheedy | 758d0bda | 2019-10-02 23:45:23 | [diff] [blame] | 22 | ScopedStopwatch(const std::string& metric); |
kraynov | 96d0ab95 | 2016-10-12 14:46:58 | [diff] [blame] | 23 | ~ScopedStopwatch(); |
| 24 | |
| 25 | private: |
| 26 | base::TimeTicks begin_; |
Brian Sheedy | 758d0bda | 2019-10-02 23:45:23 | [diff] [blame] | 27 | const std::string metric_; |
kraynov | 96d0ab95 | 2016-10-12 14:46:58 | [diff] [blame] | 28 | |
| 29 | DISALLOW_COPY_AND_ASSIGN(ScopedStopwatch); |
| 30 | }; |
| 31 | |
| 32 | // Measure median time of loop iterations. |
| 33 | // Example: |
| 34 | // IterableStopwatch stopwatch("foo"); |
| 35 | // for (int i = 0; i < 100; i++) { |
| 36 | // ... |
| 37 | // stopwatch.NextLap(); |
| 38 | // } |
| 39 | // Must be used inside GTest case and result will be printed in perf_test format |
| 40 | // with value name passed to constructor. |
| 41 | class IterableStopwatch { |
| 42 | public: |
Brian Sheedy | 758d0bda | 2019-10-02 23:45:23 | [diff] [blame] | 43 | IterableStopwatch(const std::string& metric); |
kraynov | 96d0ab95 | 2016-10-12 14:46:58 | [diff] [blame] | 44 | ~IterableStopwatch(); |
| 45 | |
| 46 | void NextLap(); |
| 47 | |
| 48 | private: |
| 49 | base::TimeTicks begin_; |
Brian Sheedy | 758d0bda | 2019-10-02 23:45:23 | [diff] [blame] | 50 | std::vector<double> laps_; |
| 51 | const std::string metric_; |
kraynov | 96d0ab95 | 2016-10-12 14:46:58 | [diff] [blame] | 52 | |
| 53 | DISALLOW_COPY_AND_ASSIGN(IterableStopwatch); |
| 54 | }; |
| 55 | |
| 56 | } // namespace tracing |
| 57 | |
| 58 | #endif // COMPONENTS_TRACING_TEST_PERF_TEST_HELPERS_H_ |