summaryrefslogtreecommitdiffstats
path: root/base/perftimer.h
blob: 466f81d32b3ee802eddebdb09df44ccbb0e5ce25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_PERFTIMER_H_
#define BASE_PERFTIMER_H_

#include <string>

#include "base/basictypes.h"
#include "base/time/time.h"

namespace base {
class FilePath;
}

// ----------------------------------------------------------------------
// Initializes and finalizes the perf log. These functions should be
// called at the beginning and end (respectively) of running all the
// performance tests. The init function returns true on success.
// ----------------------------------------------------------------------
bool InitPerfLog(const base::FilePath& log_path);
void FinalizePerfLog();

// ----------------------------------------------------------------------
// LogPerfResult
//   Writes to the perf result log the given 'value' resulting from the
//   named 'test'. The units are to aid in reading the log by people.
// ----------------------------------------------------------------------
void LogPerfResult(const char* test_name, double value, const char* units);

// ----------------------------------------------------------------------
// PerfTimer
//   A simple wrapper around Now()
// ----------------------------------------------------------------------
class PerfTimer {
 public:
  PerfTimer() {
    begin_ = base::TimeTicks::Now();
  }

  // Returns the time elapsed since object construction
  base::TimeDelta Elapsed() const {
    return base::TimeTicks::Now() - begin_;
  }

 private:
  base::TimeTicks begin_;
};

// ----------------------------------------------------------------------
// PerfTimeLogger
//   Automates calling LogPerfResult for the common case where you want
//   to measure the time that something took. Call Done() when the test
//   is complete if you do extra work after the test or there are stack
//   objects with potentially expensive constructors. Otherwise, this
//   class with automatically log on destruction.
// ----------------------------------------------------------------------
class PerfTimeLogger {
 public:
  explicit PerfTimeLogger(const char* test_name)
      : logged_(false),
        test_name_(test_name) {
  }

  ~PerfTimeLogger() {
    if (!logged_)
      Done();
  }

  void Done() {
    // we use a floating-point millisecond value because it is more
    // intuitive than microseconds and we want more precision than
    // integer milliseconds
    LogPerfResult(test_name_.c_str(), timer_.Elapsed().InMillisecondsF(), "ms");
    logged_ = true;
  }

 private:
  bool logged_;
  std::string test_name_;
  PerfTimer timer_;
};

#endif  // BASE_PERFTIMER_H_