Home | History | Annotate | Download | only in base
      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.
      4 
      5 #ifndef BASE_PERFTIMER_H_
      6 #define BASE_PERFTIMER_H_
      7 
      8 #include <string>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/time/time.h"
     12 
     13 namespace base {
     14 class FilePath;
     15 }
     16 
     17 // ----------------------------------------------------------------------
     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 {
     37  public:
     38   PerfTimer() {
     39     begin_ = base::TimeTicks::Now();
     40   }
     41 
     42   // Returns the time elapsed since object construction
     43   base::TimeDelta Elapsed() const {
     44     return base::TimeTicks::Now() - begin_;
     45   }
     46 
     47  private:
     48   base::TimeTicks begin_;
     49 };
     50 
     51 // ----------------------------------------------------------------------
     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 };
     84 
     85 #endif  // BASE_PERFTIMER_H_
     86