OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "apps/benchmark/measurements.h" | 5 #include "apps/benchmark/measurements.h" |
6 | 6 |
7 namespace benchmark { | 7 namespace benchmark { |
8 namespace { | 8 namespace { |
9 | 9 |
10 bool Match(const Event& event, const EventSpec& spec) { | 10 bool Match(const Event& event, const EventSpec& spec) { |
11 return event.name == spec.name && event.categories == spec.categories; | 11 return event.name == spec.name && event.categories == spec.categories; |
12 } | 12 } |
13 | 13 |
14 } // namespace | 14 } // namespace |
15 | 15 |
16 EventSpec::EventSpec() {} | 16 EventSpec::EventSpec() {} |
17 | 17 |
18 EventSpec::EventSpec(std::string name, std::string categories) | 18 EventSpec::EventSpec(std::string name, std::string categories) |
19 : name(name), categories(categories) {} | 19 : name(name), categories(categories) {} |
20 | 20 |
21 EventSpec::~EventSpec() {} | 21 EventSpec::~EventSpec() {} |
22 | 22 |
23 Measurement::Measurement() {} | 23 Measurement::Measurement() {} |
24 | 24 |
| 25 Measurement::Measurement(MeasurementType type, EventSpec target_event) |
| 26 : type(type), target_event(target_event) {} |
| 27 |
25 Measurement::Measurement(MeasurementType type, | 28 Measurement::Measurement(MeasurementType type, |
26 std::string target_name, | 29 EventSpec target_event, |
27 std::string target_categories) | 30 EventSpec second_event) |
28 : type(type), target_event(target_name, target_categories) {} | 31 : type(type), target_event(target_event), second_event(second_event) {} |
29 | 32 |
30 Measurement::~Measurement() {} | 33 Measurement::~Measurement() {} |
31 | 34 |
32 Measurements::Measurements(std::vector<Event> events, | 35 Measurements::Measurements(std::vector<Event> events, |
33 base::TimeTicks time_origin) | 36 base::TimeTicks time_origin) |
34 : events_(events), time_origin_(time_origin) {} | 37 : events_(events), time_origin_(time_origin) {} |
35 | 38 |
36 Measurements::~Measurements() {} | 39 Measurements::~Measurements() {} |
37 | 40 |
38 double Measurements::Measure(const Measurement& measurement) { | 41 double Measurements::Measure(const Measurement& measurement) { |
39 switch (measurement.type) { | 42 switch (measurement.type) { |
40 case MeasurementType::TIME_UNTIL: | 43 case MeasurementType::TIME_UNTIL: |
41 return TimeUntil(measurement.target_event); | 44 return TimeUntil(measurement.target_event); |
| 45 case MeasurementType::TIME_BETWEEN: |
| 46 return TimeBetween(measurement.target_event, measurement.second_event); |
42 case MeasurementType::AVG_DURATION: | 47 case MeasurementType::AVG_DURATION: |
43 return AvgDuration(measurement.target_event); | 48 return AvgDuration(measurement.target_event); |
44 default: | 49 default: |
45 NOTREACHED(); | 50 NOTREACHED(); |
46 return double(); | 51 return double(); |
47 } | 52 } |
48 } | 53 } |
49 | 54 |
50 double Measurements::TimeUntil(const EventSpec& event_spec) { | 55 bool Measurements::EarliestOccurence(const EventSpec& event_spec, |
51 base::TimeTicks earliest; | 56 base::TimeTicks* earliest) { |
| 57 base::TimeTicks result; |
52 bool found = false; | 58 bool found = false; |
53 for (const Event& event : events_) { | 59 for (const Event& event : events_) { |
54 if (!Match(event, event_spec)) | 60 if (!Match(event, event_spec)) |
55 continue; | 61 continue; |
56 | 62 |
57 if (found) { | 63 if (found) { |
58 earliest = std::min(earliest, event.timestamp); | 64 result = std::min(result, event.timestamp); |
59 } else { | 65 } else { |
60 earliest = event.timestamp; | 66 result = event.timestamp; |
61 found = true; | 67 found = true; |
62 } | 68 } |
63 } | 69 } |
64 if (!found) | 70 if (!found) |
| 71 return false; |
| 72 *earliest = result; |
| 73 return true; |
| 74 } |
| 75 |
| 76 double Measurements::TimeUntil(const EventSpec& event_spec) { |
| 77 base::TimeTicks earliest; |
| 78 if (!EarliestOccurence(event_spec, &earliest)) |
65 return -1.0; | 79 return -1.0; |
66 return (earliest - time_origin_).InMillisecondsF(); | 80 return (earliest - time_origin_).InMillisecondsF(); |
67 } | 81 } |
68 | 82 |
| 83 double Measurements::TimeBetween(const EventSpec& first_event_spec, |
| 84 const EventSpec& second_event_spec) { |
| 85 base::TimeTicks earliest_first_event; |
| 86 if (!EarliestOccurence(first_event_spec, &earliest_first_event)) |
| 87 return -1.0; |
| 88 base::TimeTicks earliest_second_event; |
| 89 if (!EarliestOccurence(second_event_spec, &earliest_second_event)) |
| 90 return -1.0; |
| 91 if (earliest_second_event < earliest_first_event) |
| 92 return -1.0; |
| 93 return (earliest_second_event - earliest_first_event).InMillisecondsF(); |
| 94 } |
| 95 |
69 double Measurements::AvgDuration(const EventSpec& event_spec) { | 96 double Measurements::AvgDuration(const EventSpec& event_spec) { |
70 double sum = 0.0; | 97 double sum = 0.0; |
71 int count = 0; | 98 int count = 0; |
72 for (const Event& event : events_) { | 99 for (const Event& event : events_) { |
73 if (event.type != EventType::COMPLETE) | 100 if (event.type != EventType::COMPLETE) |
74 continue; | 101 continue; |
75 | 102 |
76 if (!Match(event, event_spec)) | 103 if (!Match(event, event_spec)) |
77 continue; | 104 continue; |
78 | 105 |
79 sum += event.duration.InMillisecondsF(); | 106 sum += event.duration.InMillisecondsF(); |
80 count += 1; | 107 count += 1; |
81 } | 108 } |
82 | 109 |
83 if (!count) | 110 if (!count) |
84 return -1.0; | 111 return -1.0; |
85 return sum / count; | 112 return sum / count; |
86 } | 113 } |
87 | 114 |
88 } // namespace benchmark | 115 } // namespace benchmark |
OLD | NEW |