Revert 178383
[chromium-blink-merge.git] / base / perftimer.h
blob1ac1a7d18901e268cfe02585322b68b0aafc3c03
1 // Copyright (c) 2006-2008 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.
5 #ifndef BASE_PERFTIMER_H_
6 #define BASE_PERFTIMER_H_
8 #include <string>
10 #include "base/basictypes.h"
11 #include "base/time.h"
13 class FilePath;
15 // ----------------------------------------------------------------------
16 // Initializes and finalizes the perf log. These functions should be
17 // called at the beginning and end (respectively) of running all the
18 // performance tests. The init function returns true on success.
19 // ----------------------------------------------------------------------
20 bool InitPerfLog(const FilePath& log_path);
21 void FinalizePerfLog();
23 // ----------------------------------------------------------------------
24 // LogPerfResult
25 // Writes to the perf result log the given 'value' resulting from the
26 // named 'test'. The units are to aid in reading the log by people.
27 // ----------------------------------------------------------------------
28 void LogPerfResult(const char* test_name, double value, const char* units);
30 // ----------------------------------------------------------------------
31 // PerfTimer
32 // A simple wrapper around Now()
33 // ----------------------------------------------------------------------
34 class PerfTimer {
35 public:
36 PerfTimer() {
37 begin_ = base::TimeTicks::Now();
40 // Returns the time elapsed since object construction
41 base::TimeDelta Elapsed() const {
42 return base::TimeTicks::Now() - begin_;
45 private:
46 base::TimeTicks begin_;
49 // ----------------------------------------------------------------------
50 // PerfTimeLogger
51 // Automates calling LogPerfResult for the common case where you want
52 // to measure the time that something took. Call Done() when the test
53 // is complete if you do extra work after the test or there are stack
54 // objects with potentially expensive constructors. Otherwise, this
55 // class with automatically log on destruction.
56 // ----------------------------------------------------------------------
57 class PerfTimeLogger {
58 public:
59 explicit PerfTimeLogger(const char* test_name)
60 : logged_(false),
61 test_name_(test_name) {
64 ~PerfTimeLogger() {
65 if (!logged_)
66 Done();
69 void Done() {
70 // we use a floating-point millisecond value because it is more
71 // intuitive than microseconds and we want more precision than
72 // integer milliseconds
73 LogPerfResult(test_name_.c_str(), timer_.Elapsed().InMillisecondsF(), "ms");
74 logged_ = true;
77 private:
78 bool logged_;
79 std::string test_name_;
80 PerfTimer timer_;
83 #endif // BASE_PERFTIMER_H_