Index: src/platform/microbenchmark/microbenchmark.cc |
diff --git a/src/platform/microbenchmark/microbenchmark.cc b/src/platform/microbenchmark/microbenchmark.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..4b0de8ce72ce9d67bd126999c5a35a85f9be68bd |
--- /dev/null |
+++ b/src/platform/microbenchmark/microbenchmark.cc |
@@ -0,0 +1,69 @@ |
+// 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. |
+ |
+#include "microbenchmark/microbenchmark.h" |
+ |
+namespace chromeos { |
+// Commandline switch used to override the default number of runs for all |
+// tests. |
+const char *Microbenchmark::kRunsSwitch = "microbenchmark-runs"; |
+ |
+void Microbenchmark::Run(uint64 number_of_runs) { |
+ runs_ = number_of_runs; |
+ Setup(number_of_runs); |
+ |
+ uint64 current_run = runs_; |
+ struct timespec start_time; |
+ struct timespec stop_time; |
+ // First we time the scaffolding. |
+ clock_gettime(CLOCK_REALTIME, &start_time); |
+ while (current_run--) { |
+ SingleTest(true); |
+ } |
+ clock_gettime(CLOCK_REALTIME, &stop_time); |
+ scaffold_total_ns_ += |
+ ((stop_time.tv_sec - start_time.tv_sec) * 1000000000ULL) + |
+ (stop_time.tv_nsec - start_time.tv_nsec); |
+ scaffold_per_run_ns_ = scaffold_total_ns_ / runs_; |
+ // Now the real deal. |
+ current_run = runs_; |
+ clock_gettime(CLOCK_REALTIME, &start_time); |
+ while (current_run--) { |
+ SingleTest(false); |
+ } |
+ clock_gettime(CLOCK_REALTIME, &stop_time); |
+ total_ns_ += ((stop_time.tv_sec - start_time.tv_sec) * 1000000000ULL) + |
+ (stop_time.tv_nsec - start_time.tv_nsec); |
+ per_run_ns_ = total_ns_ / runs_; |
+} |
+ |
+void Microbenchmark::Print() const { |
+ LOG(WARNING) << "All measurements in nanoseconds"; |
+ LOG(WARNING) << "Numbers may overflow and may not be statistically " |
+ << "meaningful."; |
+ std::cout << "MB:name,runs,total_ns,per_run_ns\nMB:" |
+ << name() << "-scaffold," |
+ << runs() << "," |
+ << scaffold_total_nanoseconds() << "," |
+ << scaffold_per_run_nanoseconds() << "\nMB:" |
+ << name() << "," |
+ << runs() << "," |
+ << total_nanoseconds() << "," |
+ << per_run_nanoseconds() << "\nMB:" |
+ << name() << "-adjusted," |
+ << runs() << "," |
+ << total_nanoseconds() - scaffold_total_nanoseconds() << "" |
+ << per_run_nanoseconds() - scaffold_per_run_nanoseconds() |
+ << "\n"; |
+} |
+ |
+// Hide away helper functions here. |
+namespace microbenchmark_helper { |
+// Empty setup function. |
+void NoSetup(uint64) { } |
+ |
+} // namespace microbenchmark_helper |
+} // namespace chromeos |
+ |