OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 The Chromium OS 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 // Some portions Copyright (c) 2009 The Chromium Authors. |
| 5 // |
| 6 // Simple microbenchmark framework |
| 7 |
| 8 #ifndef __CHROMEOS_MICROBENCHMARK_MICROBENCHMARK_H |
| 9 #define __CHROMEOS_MICROBENCHMARK_MICROBENCHMARK_H |
| 10 |
| 11 #include <errno.h> |
| 12 #include <time.h> |
| 13 |
| 14 #include <iostream> |
| 15 |
| 16 #include <base/basictypes.h> |
| 17 #include <base/command_line.h> |
| 18 #include <base/logging.h> |
| 19 #include <base/scoped_ptr.h> |
| 20 #include <gtest/gtest.h> |
| 21 |
| 22 #define CHROMEOS_MICROBENCHMARK_WITH_SETUP(_SETUP_NAME, _NAME, _RUNS) \ |
| 23 class _NAME ## Class : public Microbenchmark { \ |
| 24 public: \ |
| 25 _NAME ## Class() {} \ |
| 26 ~_NAME ## Class() {} \ |
| 27 const char *name() const { return #_NAME; } \ |
| 28 void Setup(uint64 runs) { _SETUP_NAME(runs); } \ |
| 29 void SingleTest(bool scaffold_only) { _NAME(scaffold_only); } \ |
| 30 }; \ |
| 31 TEST(_NAME, Microbenchmark) { \ |
| 32 _NAME ## Class chromeos_benchmark; \ |
| 33 CommandLine *cl = CommandLine::ForCurrentProcess(); \ |
| 34 errno = 0; \ |
| 35 std::string runs_str = \ |
| 36 cl->GetSwitchValueASCII(chromeos::Microbenchmark::kRunsSwitch); \ |
| 37 unsigned long long runs = _RUNS; \ |
| 38 if (!runs_str.empty()) { \ |
| 39 errno = 0; \ |
| 40 runs = strtoull(runs_str.c_str(), NULL, 0); \ |
| 41 if (errno) \ |
| 42 runs = _RUNS; \ |
| 43 } \ |
| 44 chromeos_benchmark.Run(runs); \ |
| 45 chromeos_benchmark.Print(); \ |
| 46 } |
| 47 |
| 48 #define CHROMEOS_MICROBENCHMARK(_NAME, _RUNS) \ |
| 49 CHROMEOS_MICROBENCHMARK_WITH_SETUP(chromeos::microbenchmark_helper::NoSetup, \ |
| 50 _NAME, \ |
| 51 _RUNS) |
| 52 namespace chromeos { |
| 53 |
| 54 namespace microbenchmark_helper { |
| 55 void NoSetup(uint64 runs); |
| 56 } // microbenchmark |
| 57 |
| 58 // A simple microbenchmarking abstract class. |
| 59 // This class is not thread-safe and should only be invoked |
| 60 // from one thread at a time. |
| 61 class Microbenchmark { |
| 62 public: |
| 63 Microbenchmark() : scaffold_total_ns_(0), |
| 64 scaffold_per_run_ns_(0), |
| 65 total_ns_(0), |
| 66 per_run_ns_(0), |
| 67 runs_(0) {} |
| 68 virtual ~Microbenchmark() {} |
| 69 // Switch to override the number of runs to perform. |
| 70 static const char *kRunsSwitch; |
| 71 // Performs the actual microbenchmarking. |
| 72 void Run(uint64 number_of_runs); |
| 73 // Outputs a standard format of the testing data to stdout. |
| 74 void Print() const; |
| 75 // Accessors |
| 76 const uint64 total_nanoseconds() const { return total_ns_; } |
| 77 const uint64 per_run_nanoseconds() const { return per_run_ns_; } |
| 78 const uint64 scaffold_total_nanoseconds() const |
| 79 { return scaffold_total_ns_; } |
| 80 const uint64 scaffold_per_run_nanoseconds() const |
| 81 { return scaffold_per_run_ns_; } |
| 82 const uint64 runs() const { return runs_; } |
| 83 |
| 84 //// Test code to be implemented by the class consumer. |
| 85 virtual const char *name() const = 0; |
| 86 // Called automatically before the benchmark. |
| 87 virtual void Setup(uint64 runs) = 0; |
| 88 // Should execute the test to benchmark once. |
| 89 virtual void SingleTest(bool scaffold_only) = 0; |
| 90 |
| 91 private: |
| 92 uint64 scaffold_total_ns_; |
| 93 uint64 scaffold_per_run_ns_; |
| 94 uint64 total_ns_; |
| 95 uint64 per_run_ns_; |
| 96 uint64 runs_; |
| 97 DISALLOW_COPY_AND_ASSIGN(Microbenchmark); |
| 98 }; |
| 99 |
| 100 } // chromeos |
| 101 #endif // __CHROMEOS_MICROBENCHMARK_MICROBENCHMARK_H |
OLD | NEW |