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