diff options
author | tfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-08-24 15:09:45 +0000 |
---|---|---|
committer | tfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-08-24 15:09:45 +0000 |
commit | bb5c95c9bff8920f56d1737b27e2eb1ae99c54ad (patch) | |
tree | 08bf4704252fdfc8fcb286a208c691d7d3045c33 /base/test/perftimer.h | |
parent | ab612c5e4139b7ab5be53f063efc1739bc452137 (diff) | |
download | chromium_src-bb5c95c9bff8920f56d1737b27e2eb1ae99c54ad.zip chromium_src-bb5c95c9bff8920f56d1737b27e2eb1ae99c54ad.tar.gz chromium_src-bb5c95c9bff8920f56d1737b27e2eb1ae99c54ad.tar.bz2 |
Move perftimer.* into base/test/ directory.
This should address the TODO from tools/gn/secondary/base/BUILD.gn
BUG=None
TEST=test_support_perf
R=brettw@chromium.org
TBR=cpu, dalecurtis, akalin, thestig
Review URL: https://chromiumcodereview.appspot.com/22887041
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@219448 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/test/perftimer.h')
-rw-r--r-- | base/test/perftimer.h | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/base/test/perftimer.h b/base/test/perftimer.h new file mode 100644 index 0000000..1a26cf5 --- /dev/null +++ b/base/test/perftimer.h @@ -0,0 +1,85 @@ +// Copyright 2013 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_TEST_PERFTIMER_H_ +#define BASE_TEST_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_TEST_PERFTIMER_H_ |