blob: 03a77136c5d289fe367eee5a3aa934124c004a32 [file] [log] [blame]
initial.commit3f4a7322008-07-27 06:49:38 +09001// Copyright 2008, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30#ifndef BASE_PERFTTIMER_H__
31#define BASE_PERFTTIMER_H__
32
33#include <string>
34#include "base/basictypes.h"
35#include "base/time.h"
36
37// ----------------------------------------------------------------------
38// Initializes and finalizes the perf log. These functions should be
39// called at the beginning and end (respectively) of running all the
40// performance tests. The init function returns true on success.
41// ----------------------------------------------------------------------
42bool InitPerfLog(const char* log_file);
43void FinalizePerfLog();
44
45// ----------------------------------------------------------------------
46// LogPerfResult
47// Writes to the perf result log the given 'value' resulting from the
48// named 'test'. The units are to aid in reading the log by people.
49// ----------------------------------------------------------------------
50void LogPerfResult(const char* test_name, double value, const char* units);
51
52// ----------------------------------------------------------------------
53// PerfTimer
54// A simple wrapper around Now()
55// ----------------------------------------------------------------------
56class PerfTimer {
57 public:
58 PerfTimer() {
59 begin_ = TimeTicks::Now();
60 }
61
62 // Returns the time elapsed since object construction
63 TimeDelta Elapsed() const {
64 return TimeTicks::Now() - begin_;
65 }
66
67 private:
68 TimeTicks begin_;
69};
70
71// ----------------------------------------------------------------------
72// PerfTimeLogger
73// Automates calling LogPerfResult for the common case where you want
74// to measure the time that something took. Call Done() when the test
75// is complete if you do extra work after the test or there are stack
76// objects with potentially expensive constructors. Otherwise, this
77// class with automatically log on destruction.
78// ----------------------------------------------------------------------
79class PerfTimeLogger {
80 public:
81 explicit PerfTimeLogger(const char* test_name)
82 : logged_(false),
83 test_name_(test_name) {
84 }
85
86 ~PerfTimeLogger() {
87 if (!logged_)
88 Done();
89 }
90
91 void Done() {
92 // we use a floating-point millisecond value because it is more
93 // intuitive than microseconds and we want more precision than
94 // integer milliseconds
95 LogPerfResult(test_name_.c_str(), timer_.Elapsed().InMillisecondsF(), "ms");
96 logged_ = true;
97 }
98
99 private:
100 bool logged_;
101 std::string test_name_;
102 PerfTimer timer_;
103};
104
105#endif // BASE_PERFTTIMER_H__