| // 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 { |
| namespace { |
| |
| bool Match(const Event& event, const EventSpec& spec) { |
| return event.name == spec.name && event.categories == spec.categories; |
| } |
| |
| } // namespace |
| |
| EventSpec::EventSpec() {} |
| |
| EventSpec::EventSpec(std::string name, std::string categories) |
| : name(name), categories(categories) {} |
| |
| EventSpec::~EventSpec() {} |
| |
| Measurement::Measurement() {} |
| |
| Measurement::Measurement(MeasurementType type, |
| std::string target_name, |
| std::string target_categories) |
| : type(type), target_event(target_name, target_categories) {} |
| |
| Measurement::~Measurement() {} |
| |
| Measurements::Measurements(std::vector<Event> events, |
| base::TimeTicks time_origin) |
| : events_(events), time_origin_(time_origin) {} |
| |
| Measurements::~Measurements() {} |
| |
| double Measurements::Measure(const Measurement& measurement) { |
| switch (measurement.type) { |
| case MeasurementType::TIME_UNTIL: |
| return TimeUntil(measurement.target_event); |
| case MeasurementType::AVG_DURATION: |
| return AvgDuration(measurement.target_event); |
| default: |
| NOTREACHED(); |
| return double(); |
| } |
| } |
| |
| double Measurements::TimeUntil(const EventSpec& event_spec) { |
| base::TimeTicks earliest; |
| bool found = false; |
| for (const Event& event : events_) { |
| if (!Match(event, event_spec)) |
| continue; |
| |
| if (found) { |
| earliest = std::min(earliest, event.timestamp); |
| } else { |
| earliest = event.timestamp; |
| found = true; |
| } |
| } |
| if (!found) |
| return -1.0; |
| return (earliest - time_origin_).InMillisecondsF(); |
| } |
| |
| double Measurements::AvgDuration(const EventSpec& event_spec) { |
| double sum = 0.0; |
| int count = 0; |
| for (const Event& event : events_) { |
| if (event.type != EventType::COMPLETE) |
| continue; |
| |
| if (!Match(event, event_spec)) |
| continue; |
| |
| sum += event.duration.InMillisecondsF(); |
| count += 1; |
| } |
| |
| if (!count) |
| return -1.0; |
| return sum / count; |
| } |
| |
| } // namespace benchmark |