Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(748)

Unified Diff: src/platform/microbenchmark/microbenchmark.h

Issue 481009: Add a simple microbenchmark framework, packaging, and sample tests. (Closed)
Patch Set: fix the bad comment Created 11 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/platform/microbenchmark/make_pkg.sh ('k') | src/platform/microbenchmark/microbenchmark.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/platform/microbenchmark/microbenchmark.h
diff --git a/src/platform/microbenchmark/microbenchmark.h b/src/platform/microbenchmark/microbenchmark.h
new file mode 100644
index 0000000000000000000000000000000000000000..ec4bf661927a7d516c3396aed956b4d5545eb08c
--- /dev/null
+++ b/src/platform/microbenchmark/microbenchmark.h
@@ -0,0 +1,101 @@
+// Copyright (c) 2009 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+// Some portions Copyright (c) 2009 The Chromium Authors.
+//
+// Simple microbenchmark framework
+
+#ifndef __CHROMEOS_MICROBENCHMARK_MICROBENCHMARK_H
+#define __CHROMEOS_MICROBENCHMARK_MICROBENCHMARK_H
+
+#include <errno.h>
+#include <time.h>
+
+#include <iostream>
+
+#include <base/basictypes.h>
+#include <base/command_line.h>
+#include <base/logging.h>
+#include <base/scoped_ptr.h>
+#include <gtest/gtest.h>
+
+#define CHROMEOS_MICROBENCHMARK_WITH_SETUP(_SETUP_NAME, _NAME, _RUNS) \
+class _NAME ## Class : public Microbenchmark { \
+ public: \
+ _NAME ## Class() {} \
+ ~_NAME ## Class() {} \
+ const char *name() const { return #_NAME; } \
+ void Setup(uint64 runs) { _SETUP_NAME(runs); } \
+ void SingleTest(bool scaffold_only) { _NAME(scaffold_only); } \
+}; \
+TEST(_NAME, Microbenchmark) { \
+ _NAME ## Class chromeos_benchmark; \
+ CommandLine *cl = CommandLine::ForCurrentProcess(); \
+ errno = 0; \
+ std::string runs_str = \
+ cl->GetSwitchValueASCII(chromeos::Microbenchmark::kRunsSwitch); \
+ unsigned long long runs = _RUNS; \
+ if (!runs_str.empty()) { \
+ errno = 0; \
+ runs = strtoull(runs_str.c_str(), NULL, 0); \
+ if (errno) \
+ runs = _RUNS; \
+ } \
+ chromeos_benchmark.Run(runs); \
+ chromeos_benchmark.Print(); \
+}
+
+#define CHROMEOS_MICROBENCHMARK(_NAME, _RUNS) \
+ CHROMEOS_MICROBENCHMARK_WITH_SETUP(chromeos::microbenchmark_helper::NoSetup, \
+ _NAME, \
+ _RUNS)
+namespace chromeos {
+
+namespace microbenchmark_helper {
+void NoSetup(uint64 runs);
+} // microbenchmark
+
+// A simple microbenchmarking abstract class.
+// This class is not thread-safe and should only be invoked
+// from one thread at a time.
+class Microbenchmark {
+ public:
+ Microbenchmark() : scaffold_total_ns_(0),
+ scaffold_per_run_ns_(0),
+ total_ns_(0),
+ per_run_ns_(0),
+ runs_(0) {}
+ virtual ~Microbenchmark() {}
+ // Switch to override the number of runs to perform.
+ static const char *kRunsSwitch;
+ // Performs the actual microbenchmarking.
+ void Run(uint64 number_of_runs);
+ // Outputs a standard format of the testing data to stdout.
+ void Print() const;
+ // Accessors
+ const uint64 total_nanoseconds() const { return total_ns_; }
+ const uint64 per_run_nanoseconds() const { return per_run_ns_; }
+ const uint64 scaffold_total_nanoseconds() const
+ { return scaffold_total_ns_; }
+ const uint64 scaffold_per_run_nanoseconds() const
+ { return scaffold_per_run_ns_; }
+ const uint64 runs() const { return runs_; }
+
+ //// Test code to be implemented by the class consumer.
+ virtual const char *name() const = 0;
+ // Called automatically before the benchmark.
+ virtual void Setup(uint64 runs) = 0;
+ // Should execute the test to benchmark once.
+ virtual void SingleTest(bool scaffold_only) = 0;
+
+ private:
+ uint64 scaffold_total_ns_;
+ uint64 scaffold_per_run_ns_;
+ uint64 total_ns_;
+ uint64 per_run_ns_;
+ uint64 runs_;
+ DISALLOW_COPY_AND_ASSIGN(Microbenchmark);
+};
+
+} // chromeos
+#endif // __CHROMEOS_MICROBENCHMARK_MICROBENCHMARK_H
« no previous file with comments | « src/platform/microbenchmark/make_pkg.sh ('k') | src/platform/microbenchmark/microbenchmark.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698