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 #include <algorithm> |
| 8 |
7 namespace benchmark { | 9 namespace benchmark { |
8 namespace { | 10 namespace { |
9 | 11 |
10 bool Match(const Event& event, const EventSpec& spec) { | 12 static bool Match(const Event& event, const EventSpec& spec) { |
11 return event.name == spec.name && event.categories == spec.categories; | 13 return event.name == spec.name && event.categories == spec.categories; |
12 } | 14 } |
13 | 15 |
14 } // namespace | 16 } // namespace |
15 | 17 |
16 EventSpec::EventSpec() {} | 18 EventSpec::EventSpec() {} |
17 | 19 |
18 EventSpec::EventSpec(std::string name, std::string categories) | 20 EventSpec::EventSpec(std::string name, std::string categories) |
19 : name(name), categories(categories) {} | 21 : name(name), categories(categories) {} |
20 | 22 |
(...skipping 10 matching lines...) Expand all Loading... |
31 : type(type), target_event(target_event), second_event(second_event) {} | 33 : type(type), target_event(target_event), second_event(second_event) {} |
32 | 34 |
33 Measurement::~Measurement() {} | 35 Measurement::~Measurement() {} |
34 | 36 |
35 Measurements::Measurements(std::vector<Event> events, | 37 Measurements::Measurements(std::vector<Event> events, |
36 base::TimeTicks time_origin) | 38 base::TimeTicks time_origin) |
37 : events_(events), time_origin_(time_origin) {} | 39 : events_(events), time_origin_(time_origin) {} |
38 | 40 |
39 Measurements::~Measurements() {} | 41 Measurements::~Measurements() {} |
40 | 42 |
41 double Measurements::Measure(const Measurement& measurement) { | 43 void Measurements::Measure(const Measurement& measurement, |
| 44 std::vector<double>* results) const { |
42 switch (measurement.type) { | 45 switch (measurement.type) { |
43 case MeasurementType::TIME_UNTIL: | 46 case MeasurementType::TIME_UNTIL: |
44 return TimeUntil(measurement.target_event); | 47 results->push_back(TimeUntil(measurement.target_event)); |
| 48 break; |
45 case MeasurementType::TIME_BETWEEN: | 49 case MeasurementType::TIME_BETWEEN: |
46 return TimeBetween(measurement.target_event, measurement.second_event); | 50 results->push_back( |
| 51 TimeBetween(measurement.target_event, measurement.second_event)); |
| 52 break; |
47 case MeasurementType::AVG_DURATION: | 53 case MeasurementType::AVG_DURATION: |
48 return AvgDuration(measurement.target_event); | 54 results->push_back(AvgDuration(measurement.target_event)); |
| 55 break; |
| 56 case MeasurementType::MEDIAN_DURATION: { |
| 57 bool status = Percentiles(measurement.target_event, {0.5}, results); |
| 58 if (!status) { |
| 59 results->push_back(-1.0); |
| 60 } |
| 61 break; |
| 62 } |
| 63 case MeasurementType::DIST_DURATION: { |
| 64 bool status = |
| 65 Percentiles(measurement.target_event, {0.1, 0.5, 0.9}, results); |
| 66 if (!status) { |
| 67 results->push_back(-1.0); |
| 68 } |
| 69 break; |
| 70 } |
49 default: | 71 default: |
50 NOTREACHED(); | 72 NOTREACHED(); |
51 return double(); | 73 return; |
52 } | 74 } |
53 } | 75 } |
54 | 76 |
55 bool Measurements::EarliestOccurence(const EventSpec& event_spec, | 77 bool Measurements::EarliestOccurence(const EventSpec& event_spec, |
56 base::TimeTicks* earliest) { | 78 base::TimeTicks* earliest) const { |
57 base::TimeTicks result; | 79 base::TimeTicks result; |
58 bool found = false; | 80 bool found = false; |
59 for (const Event& event : events_) { | 81 for (const Event& event : events_) { |
60 if (!Match(event, event_spec)) | 82 if (!Match(event, event_spec)) |
61 continue; | 83 continue; |
62 | 84 |
63 if (found) { | 85 if (found) { |
64 result = std::min(result, event.timestamp); | 86 result = std::min(result, event.timestamp); |
65 } else { | 87 } else { |
66 result = event.timestamp; | 88 result = event.timestamp; |
67 found = true; | 89 found = true; |
68 } | 90 } |
69 } | 91 } |
70 if (!found) | 92 if (!found) |
71 return false; | 93 return false; |
72 *earliest = result; | 94 *earliest = result; |
73 return true; | 95 return true; |
74 } | 96 } |
75 | 97 |
76 double Measurements::TimeUntil(const EventSpec& event_spec) { | 98 double Measurements::TimeUntil(const EventSpec& event_spec) const { |
77 base::TimeTicks earliest; | 99 base::TimeTicks earliest; |
78 if (!EarliestOccurence(event_spec, &earliest)) | 100 if (!EarliestOccurence(event_spec, &earliest)) |
79 return -1.0; | 101 return -1.0; |
80 return (earliest - time_origin_).InMillisecondsF(); | 102 return (earliest - time_origin_).InMillisecondsF(); |
81 } | 103 } |
82 | 104 |
83 double Measurements::TimeBetween(const EventSpec& first_event_spec, | 105 double Measurements::TimeBetween(const EventSpec& first_event_spec, |
84 const EventSpec& second_event_spec) { | 106 const EventSpec& second_event_spec) const { |
85 base::TimeTicks earliest_first_event; | 107 base::TimeTicks earliest_first_event; |
86 if (!EarliestOccurence(first_event_spec, &earliest_first_event)) | 108 if (!EarliestOccurence(first_event_spec, &earliest_first_event)) |
87 return -1.0; | 109 return -1.0; |
88 base::TimeTicks earliest_second_event; | 110 base::TimeTicks earliest_second_event; |
89 if (!EarliestOccurence(second_event_spec, &earliest_second_event)) | 111 if (!EarliestOccurence(second_event_spec, &earliest_second_event)) |
90 return -1.0; | 112 return -1.0; |
91 if (earliest_second_event < earliest_first_event) | 113 if (earliest_second_event < earliest_first_event) |
92 return -1.0; | 114 return -1.0; |
93 return (earliest_second_event - earliest_first_event).InMillisecondsF(); | 115 return (earliest_second_event - earliest_first_event).InMillisecondsF(); |
94 } | 116 } |
95 | 117 |
96 double Measurements::AvgDuration(const EventSpec& event_spec) { | 118 double Measurements::AvgDuration(const EventSpec& event_spec) const { |
97 double sum = 0.0; | 119 double sum = 0.0; |
98 int count = 0; | 120 int count = 0; |
99 for (const Event& event : events_) { | 121 for (const Event& event : events_) { |
100 if (event.type != EventType::COMPLETE) | 122 if (event.type != EventType::COMPLETE) |
101 continue; | 123 continue; |
102 | 124 |
103 if (!Match(event, event_spec)) | 125 if (!Match(event, event_spec)) |
104 continue; | 126 continue; |
105 | 127 |
106 sum += event.duration.InMillisecondsF(); | 128 sum += event.duration.InMillisecondsF(); |
107 count += 1; | 129 count += 1; |
108 } | 130 } |
109 | 131 |
110 if (!count) | 132 if (!count) |
111 return -1.0; | 133 return -1.0; |
112 return sum / count; | 134 return sum / count; |
113 } | 135 } |
114 | 136 |
| 137 bool Measurements::Percentiles(const EventSpec& event_spec, |
| 138 const std::vector<double>& percentiles, |
| 139 std::vector<double>* result) const { |
| 140 std::vector<double> durations; |
| 141 for (const Event& event : events_) { |
| 142 if (event.type != EventType::COMPLETE) |
| 143 continue; |
| 144 |
| 145 if (!Match(event, event_spec)) |
| 146 continue; |
| 147 |
| 148 durations.push_back(event.duration.InMillisecondsF()); |
| 149 } |
| 150 if (durations.size() == 0) { |
| 151 return false; |
| 152 } |
| 153 |
| 154 if (!std::is_sorted(percentiles.begin(), percentiles.end())) { |
| 155 LOG(ERROR) << "Requested percentiles should be sorted in increasing order"; |
| 156 return false; |
| 157 } |
| 158 |
| 159 for (double percentile : percentiles) { |
| 160 size_t index = |
| 161 static_cast<size_t>(static_cast<double>(durations.size()) * percentile); |
| 162 std::nth_element(durations.begin(), durations.begin() + index, |
| 163 durations.end()); |
| 164 result->push_back(durations[index]); |
| 165 } |
| 166 |
| 167 return true; |
| 168 } |
| 169 |
115 } // namespace benchmark | 170 } // namespace benchmark |
OLD | NEW |