blob: 1d319767256034582f62fdae267dd982000cb2df [file] [log] [blame]
Bruno Rochabe388f32011-08-02 12:40:17 -07001// Copyright (c) 2011 The Chromium OS 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// Timer - class that provides timer tracking.
6
7#ifndef METRICS_TIMER_H_
8#define METRICS_TIMER_H_
9
10#include <string>
11
12#include <base/memory/scoped_ptr.h>
13#include <base/time.h>
14#include <gtest/gtest_prod.h> // for FRIEND_TEST
15
16class MetricsLibraryInterface;
17
18namespace chromeos_metrics {
19
20class TimerInterface {
21 public:
22 virtual ~TimerInterface() {}
23
24 virtual bool Start() = 0;
25 virtual bool Stop() = 0;
26 virtual bool Reset() = 0;
27 virtual bool HasStarted() const = 0;
28};
29
30// Wrapper for calls to the system clock.
31class ClockWrapper {
32 public:
33 ClockWrapper() {}
34 virtual ~ClockWrapper() {}
35
36 // Returns the current time from the system.
37 virtual base::TimeTicks GetCurrentTime() const;
38
39 private:
40 DISALLOW_COPY_AND_ASSIGN(ClockWrapper);
41};
42
43// Implements a Timer.
44class Timer : public TimerInterface {
45 public:
46 Timer();
47 virtual ~Timer() {}
48
49 // Starts the timer. If a timer is already running, also resets current
50 // timer. Always returns true.
51 virtual bool Start();
52
53 // Stops the timer and calculates the total time elapsed between now and when
54 // Start() was called. Note that this method needs a prior call to Start().
55 // Otherwise, it fails (returns false).
56 virtual bool Stop();
57
58 // Resets the timer, erasing the current duration being tracked. Always
59 // returns true.
60 virtual bool Reset();
61
62 // Returns whether the timer has started or not.
63 virtual bool HasStarted() const;
64
65 // Stores the current elapsed time in |elapsed_time|. If timer is stopped,
66 // stores the elapsed time from when Stop() was last called. Otherwise,
67 // calculates and stores the elapsed time since the last Start().
68 // Returns false if the timer was never Start()'ed or if called with a null
69 // pointer argument.
70 virtual bool GetElapsedTime(base::TimeDelta* elapsed_time) const;
71
72 private:
73 friend class TimerTest;
74 friend class TimerReporterTest;
75 FRIEND_TEST(TimerTest, StartStop);
76 FRIEND_TEST(TimerTest, ReStart);
77 FRIEND_TEST(TimerTest, Reset);
78 FRIEND_TEST(TimerTest, SeparatedTimers);
79 FRIEND_TEST(TimerTest, InvalidStop);
80 FRIEND_TEST(TimerTest, InvalidElapsedTime);
81 FRIEND_TEST(TimerReporterTest, StartStopReport);
82
83 // Elapsed time of the last use of the timer.
84 base::TimeDelta elapsed_time_;
85
86 // Starting time value.
87 base::TimeTicks start_time_;
88
89 // Whether the timer has started or not.
90 bool is_started_;
91
92 // Wrapper for the calls to the system clock.
93 scoped_ptr<ClockWrapper> clock_wrapper_;
94
95 DISALLOW_COPY_AND_ASSIGN(Timer);
96};
97
98// Extends the Timer class to report the elapsed time in milliseconds through
99// the UMA metrics library.
100class TimerReporter : public Timer {
101 public:
102 // Initializes the timer by providing a |histogram_name| to report to with
103 // |min|, |max| and |num_buckets| attributes for the histogram.
104 TimerReporter(const std::string& histogram_name, int min, int max,
105 int num_buckets);
106 virtual ~TimerReporter() {}
107
108 // Sets the metrics library used by all instances of this class.
109 static void set_metrics_lib(MetricsLibraryInterface* metrics_lib) {
110 metrics_lib_ = metrics_lib;
111 }
112
113 // Reports the current duration to UMA, in milliseconds. Returns false if
114 // there is nothing to report, i.e. the timer was not started or a metrics
115 // library is not set.
116 virtual bool ReportMilliseconds() const;
117
118 // Accessor methods.
119 const std::string& histogram_name() const { return histogram_name_; }
120 int min() const { return min_; }
121 int max() const { return max_; }
122 int num_buckets() const { return num_buckets_; }
123
124 private:
125 friend class TimerReporterTest;
126 FRIEND_TEST(TimerReporterTest, StartStopReport);
127 FRIEND_TEST(TimerReporterTest, InvalidReport);
128
129 static MetricsLibraryInterface* metrics_lib_;
130 std::string histogram_name_;
131 int min_;
132 int max_;
133 int num_buckets_;
134
135 DISALLOW_COPY_AND_ASSIGN(TimerReporter);
136};
137
138} // namespace chromeos_metrics
139
140#endif // METRICS_TIMER_H_