OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_TEST_PERFTIMER_H_ | 5 #ifndef BASE_TEST_PERFTIMER_H_ |
6 #define BASE_TEST_PERFTIMER_H_ | 6 #define BASE_TEST_PERFTIMER_H_ |
7 | 7 |
8 #include <string> | |
9 | |
10 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
11 #include "base/time/time.h" | 9 #include "base/time/time.h" |
12 | 10 |
13 namespace base { | 11 namespace base { |
14 class FilePath; | 12 class FilePath; |
15 } | 13 } |
16 | 14 |
17 // ---------------------------------------------------------------------- | 15 // A simple wrapper around Now() |
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(); | |
24 | |
25 // ---------------------------------------------------------------------- | |
26 // LogPerfResult | |
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); | |
31 | |
32 // ---------------------------------------------------------------------- | |
33 // PerfTimer | |
34 // A simple wrapper around Now() | |
35 // ---------------------------------------------------------------------- | |
36 class PerfTimer { | 16 class PerfTimer { |
37 public: | 17 public: |
38 PerfTimer() { | 18 PerfTimer() { |
39 begin_ = base::TimeTicks::Now(); | 19 begin_ = base::TimeTicks::Now(); |
40 } | 20 } |
41 | 21 |
42 // Returns the time elapsed since object construction | 22 // Returns the time elapsed since object construction |
43 base::TimeDelta Elapsed() const { | 23 base::TimeDelta Elapsed() const { |
44 return base::TimeTicks::Now() - begin_; | 24 return base::TimeTicks::Now() - begin_; |
45 } | 25 } |
46 | 26 |
47 private: | 27 private: |
48 base::TimeTicks begin_; | 28 base::TimeTicks begin_; |
49 }; | |
50 | 29 |
51 // ---------------------------------------------------------------------- | 30 DISALLOW_COPY_AND_ASSIGN(PerfTimer); |
52 // PerfTimeLogger | |
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 { | |
60 public: | |
61 explicit PerfTimeLogger(const char* test_name) | |
62 : logged_(false), | |
63 test_name_(test_name) { | |
64 } | |
65 | |
66 ~PerfTimeLogger() { | |
67 if (!logged_) | |
68 Done(); | |
69 } | |
70 | |
71 void Done() { | |
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"); | |
76 logged_ = true; | |
77 } | |
78 | |
79 private: | |
80 bool logged_; | |
81 std::string test_name_; | |
82 PerfTimer timer_; | |
83 }; | 31 }; |
84 | 32 |
85 #endif // BASE_TEST_PERFTIMER_H_ | 33 #endif // BASE_TEST_PERFTIMER_H_ |
OLD | NEW |