Index: apps/benchmark/measurements.cc |
diff --git a/apps/benchmark/measurements.cc b/apps/benchmark/measurements.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c8d63ab51beb9582579f87f208449d4f3a7210b4 |
--- /dev/null |
+++ b/apps/benchmark/measurements.cc |
@@ -0,0 +1,74 @@ |
+// Copyright 2015 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. |
+ |
+#include "apps/benchmark/measurements.h" |
+ |
+namespace benchmark { |
+ |
+bool Match(const Event& event, const EventSpec& spec) { |
+ return event.name == spec.name && event.category == spec.category; |
+} |
+ |
+Measurements::Measurements(std::vector<Event> events, EventSpec reference_spec) |
+ : events_(events), reference_spec_(reference_spec) {} |
+ |
+Measurements::~Measurements() {} |
+ |
+double Measurements::Measure(const Measurement& measurement) { |
+ switch (measurement.type) { |
+ case Measurement::TIME_UNTIL: |
+ return TimeUntil(measurement.target_event); |
+ case Measurement::AVG_DURATION: |
+ return AvgDuration(measurement.target_event); |
+ default: |
+ return -1.0; |
+ } |
+} |
+ |
+double Measurements::TimeUntil(const EventSpec& event_spec) { |
+ base::TimeTicks target = FindEarliest(event_spec); |
+ base::TimeTicks reference = FindEarliest(reference_spec_); |
+ return (target - reference).InMillisecondsF(); |
+} |
+ |
+double Measurements::AvgDuration(const EventSpec& event_spec) { |
+ double sum = 0.0; |
+ int count = 0; |
+ for (const Event& event : events_) { |
+ if (event.category == "__metadata") |
+ continue; |
+ |
+ if (!Match(event, event_spec)) |
+ continue; |
+ |
+ sum += event.duration.InMillisecondsF(); |
+ count += 1; |
+ } |
+ |
+ if (!count) |
+ return -1.0; |
+ return sum / count; |
+} |
+ |
+base::TimeTicks Measurements::FindEarliest(const EventSpec& spec) { |
+ base::TimeTicks earliest; |
+ bool found = false; |
+ for (const Event& event : events_) { |
+ if (event.category == "__metadata") |
+ continue; |
+ |
+ if (!Match(event, spec)) |
+ continue; |
+ |
+ if (found) { |
+ earliest = std::min(earliest, event.timestamp); |
+ } else { |
+ earliest = event.timestamp; |
+ found = true; |
+ } |
+ } |
+ return earliest; |
+} |
+ |
+} // namespace benchmark |