OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 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 | 5 |
| 6 class TimelineBasedMetricException(Exception): |
| 7 """Exception that can be thrown from metrics that implements |
| 8 TimelineBasedMetric to indicate a problem arised when computing the metric. |
| 9 """ |
| 10 |
| 11 |
| 12 def _TimeRangesHasOverlap(iterable_time_ranges): |
| 13 """ Returns True if there is are overlapped ranges in time ranges. |
| 14 iterable_time_ranges: an iterable of time ranges. Each time range is a |
| 15 tuple (start time, end time). |
| 16 """ |
| 17 # Sort the ranges by the start time |
| 18 sorted_time_ranges = sorted(iterable_time_ranges) |
| 19 last_range = sorted_time_ranges[0] |
| 20 for current_range in sorted_time_ranges[1:]: |
| 21 start_current_range = current_range[0] |
| 22 end_last_range = last_range[1] |
| 23 if start_current_range < end_last_range: |
| 24 return True |
| 25 last_range = current_range |
| 26 return False |
| 27 |
| 28 |
6 class TimelineBasedMetric(object): | 29 class TimelineBasedMetric(object): |
7 def __init__(self): | 30 def __init__(self): |
8 """Computes metrics from a telemetry.core.timeline Model and a range | 31 """Computes metrics from a telemetry.core.timeline Model and a range |
9 | 32 |
10 """ | 33 """ |
11 super(TimelineBasedMetric, self).__init__() | 34 super(TimelineBasedMetric, self).__init__() |
12 | 35 |
13 def AddResults(self, model, renderer_thread, interaction_records, results): | 36 def AddResults(self, model, renderer_thread, interaction_records, results): |
14 """Computes and adds metrics for the interaction_records' time ranges. | 37 """Computes and adds metrics for the interaction_records' time ranges. |
15 | 38 |
16 The override of this method should compute results on the data **only** | 39 The override of this method should compute results on the data **only** |
17 within the interaction_records' start and end time ranges. | 40 within the interaction_records' start and end time ranges. |
18 | 41 |
19 model is an instance of telemetry.core.timeline.model.TimelineModel. | 42 model is an instance of telemetry.core.timeline.model.TimelineModel. |
20 interaction_records is a list of instances of TimelineInteractionRecord. | 43 interaction_records is a list of instances of TimelineInteractionRecord. |
21 results is an instance of page.PageTestResults. | 44 results is an instance of page.PageTestResults. |
22 | 45 |
23 """ | 46 """ |
24 raise NotImplementedError() | 47 raise NotImplementedError() |
| 48 |
| 49 def VerifyNonOverlappedRecords(self, interaction_records): |
| 50 if _TimeRangesHasOverlap(((r.start, r.end) for r in interaction_records)): |
| 51 raise TimelineBasedMetricException( |
| 52 'This metric does not support interaction records with overlapped time ' |
| 53 'range.') |
| 54 if _TimeRangesHasOverlap(((r.thread_start, r.thread_end) |
| 55 for r in interaction_records)): |
| 56 raise TimelineBasedMetricException( |
| 57 'This metric does not support interaction records with overlapped ' |
| 58 'thread time range.') |
OLD | NEW |