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_
10 #include "base/basictypes.h"
11 #include "base/time.h"
17 // ----------------------------------------------------------------------
18 // Initializes and finalizes the perf log. These functions should be
19 // called at the beginning and end (respectively) of running all the
20 // performance tests. The init function returns true on success.
21 // ----------------------------------------------------------------------
22 bool InitPerfLog(const base::FilePath
& log_path
);
23 void FinalizePerfLog();
25 // ----------------------------------------------------------------------
27 // Writes to the perf result log the given 'value' resulting from the
28 // named 'test'. The units are to aid in reading the log by people.
29 // ----------------------------------------------------------------------
30 void LogPerfResult(const char* test_name
, double value
, const char* units
);
32 // ----------------------------------------------------------------------
34 // A simple wrapper around Now()
35 // ----------------------------------------------------------------------
39 begin_
= base::TimeTicks::Now();
42 // Returns the time elapsed since object construction
43 base::TimeDelta
Elapsed() const {
44 return base::TimeTicks::Now() - begin_
;
48 base::TimeTicks begin_
;
51 // ----------------------------------------------------------------------
53 // Automates calling LogPerfResult for the common case where you want
54 // to measure the time that something took. Call Done() when the test
55 // is complete if you do extra work after the test or there are stack
56 // objects with potentially expensive constructors. Otherwise, this
57 // class with automatically log on destruction.
58 // ----------------------------------------------------------------------
59 class PerfTimeLogger
{
61 explicit PerfTimeLogger(const char* test_name
)
63 test_name_(test_name
) {
72 // we use a floating-point millisecond value because it is more
73 // intuitive than microseconds and we want more precision than
74 // integer milliseconds
75 LogPerfResult(test_name_
.c_str(), timer_
.Elapsed().InMillisecondsF(), "ms");
81 std::string test_name_
;
85 #endif // BASE_PERFTIMER_H_