OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium 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 |
| 5 #include "apps/benchmark/measurements.h" |
| 6 |
| 7 namespace benchmark { |
| 8 namespace { |
| 9 |
| 10 bool Match(const Event& event, const EventSpec& spec) { |
| 11 return event.name == spec.name && event.category == spec.category; |
| 12 } |
| 13 |
| 14 } // namespace |
| 15 |
| 16 EventSpec::EventSpec() {} |
| 17 |
| 18 EventSpec::EventSpec(std::string name, std::string category) |
| 19 : name(name), category(category) {} |
| 20 |
| 21 EventSpec::~EventSpec() {} |
| 22 |
| 23 Measurement::Measurement() {} |
| 24 |
| 25 Measurement::Measurement(MeasurementType type, |
| 26 std::string target_name, |
| 27 std::string target_category) |
| 28 : type(type), target_event(target_name, target_category) {} |
| 29 |
| 30 Measurement::~Measurement() {} |
| 31 |
| 32 Measurements::Measurements(std::vector<Event> events, |
| 33 base::TimeTicks time_origin) |
| 34 : events_(events), time_origin_(time_origin) {} |
| 35 |
| 36 Measurements::~Measurements() {} |
| 37 |
| 38 double Measurements::Measure(const Measurement& measurement) { |
| 39 switch (measurement.type) { |
| 40 case MeasurementType::TIME_UNTIL: |
| 41 return TimeUntil(measurement.target_event); |
| 42 case MeasurementType::AVG_DURATION: |
| 43 return AvgDuration(measurement.target_event); |
| 44 default: |
| 45 NOTREACHED(); |
| 46 return double(); |
| 47 } |
| 48 } |
| 49 |
| 50 double Measurements::TimeUntil(const EventSpec& event_spec) { |
| 51 base::TimeTicks earliest; |
| 52 bool found = false; |
| 53 for (const Event& event : events_) { |
| 54 if (event.category == "__metadata") |
| 55 continue; |
| 56 |
| 57 if (!Match(event, event_spec)) |
| 58 continue; |
| 59 |
| 60 if (found) { |
| 61 earliest = std::min(earliest, event.timestamp); |
| 62 } else { |
| 63 earliest = event.timestamp; |
| 64 found = true; |
| 65 } |
| 66 } |
| 67 if (!found) |
| 68 return -1.0; |
| 69 return (earliest - time_origin_).InMillisecondsF(); |
| 70 } |
| 71 |
| 72 double Measurements::AvgDuration(const EventSpec& event_spec) { |
| 73 double sum = 0.0; |
| 74 int count = 0; |
| 75 for (const Event& event : events_) { |
| 76 if (event.category == "__metadata") |
| 77 continue; |
| 78 |
| 79 if (!Match(event, event_spec)) |
| 80 continue; |
| 81 |
| 82 sum += event.duration.InMillisecondsF(); |
| 83 count += 1; |
| 84 } |
| 85 |
| 86 if (!count) |
| 87 return -1.0; |
| 88 return sum / count; |
| 89 } |
| 90 |
| 91 } // namespace benchmark |
OLD | NEW |