blob: 7daac09b1c6441623f397516bd742cd57d96e491 [file] [log] [blame]
kraynov96d0ab952016-10-12 14:46:581// 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
15namespace 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.
20class ScopedStopwatch {
21 public:
Brian Sheedy758d0bda2019-10-02 23:45:2322 ScopedStopwatch(const std::string& metric);
kraynov96d0ab952016-10-12 14:46:5823 ~ScopedStopwatch();
24
25 private:
26 base::TimeTicks begin_;
Brian Sheedy758d0bda2019-10-02 23:45:2327 const std::string metric_;
kraynov96d0ab952016-10-12 14:46:5828
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.
41class IterableStopwatch {
42 public:
Brian Sheedy758d0bda2019-10-02 23:45:2343 IterableStopwatch(const std::string& metric);
kraynov96d0ab952016-10-12 14:46:5844 ~IterableStopwatch();
45
46 void NextLap();
47
48 private:
49 base::TimeTicks begin_;
Brian Sheedy758d0bda2019-10-02 23:45:2350 std::vector<double> laps_;
51 const std::string metric_;
kraynov96d0ab952016-10-12 14:46:5852
53 DISALLOW_COPY_AND_ASSIGN(IterableStopwatch);
54};
55
56} // namespace tracing
57
58#endif // COMPONENTS_TRACING_TEST_PERF_TEST_HELPERS_H_