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