blob: 67b0fd14be5873bccab145b9ca72a2106878e070 [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#include "timer.h"
6
7#include <string>
8
9#include <base/memory/scoped_ptr.h>
10#include <base/time.h>
11
12#include "metrics_library.h"
13
14namespace chromeos_metrics {
15
16base::TimeTicks ClockWrapper::GetCurrentTime() const {
17 return base::TimeTicks::Now();
18}
19
20Timer::Timer()
21 : is_started_(false),
22 clock_wrapper_(new ClockWrapper()) {}
23
24bool Timer::Start() {
25 start_time_ = clock_wrapper_->GetCurrentTime();
26 is_started_ = true;
27 return true;
28}
29
30bool Timer::Stop() {
31 // Check if the timer has been started.
32 if (!is_started_) return false;
33 is_started_ = false;
34 elapsed_time_ = clock_wrapper_->GetCurrentTime() - start_time_;
35 return true;
36}
37
38bool Timer::Reset() {
39 is_started_ = false;
40 return true;
41}
42
43bool Timer::HasStarted() const {
44 return is_started_;
45}
46
47bool Timer::GetElapsedTime(base::TimeDelta* elapsed_time) const {
48 if (start_time_.is_null() || !elapsed_time) return false;
49 if (is_started_) {
50 *elapsed_time = clock_wrapper_->GetCurrentTime() - start_time_;
51 } else {
52 *elapsed_time = elapsed_time_;
53 }
54 return true;
55}
56
57// static
58MetricsLibraryInterface* TimerReporter::metrics_lib_ = NULL;
59
60TimerReporter::TimerReporter(const std::string& histogram_name, int min,
61 int max, int num_buckets)
62 : histogram_name_(histogram_name),
63 min_(min),
64 max_(max),
65 num_buckets_(num_buckets) {}
66
67bool TimerReporter::ReportMilliseconds() const {
68 base::TimeDelta elapsed_time;
69 if (!metrics_lib_ || !GetElapsedTime(&elapsed_time)) return false;
70 return metrics_lib_->SendToUMA(histogram_name_,
71 elapsed_time.InMilliseconds(),
72 min_,
73 max_,
74 num_buckets_);
75}
76
77} // namespace chromeos_metrics