| 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): | 6 class TimelineBasedMetricException(Exception): |
| 7 """Exception that can be thrown from metrics that implements | 7 """Exception that can be thrown from metrics that implements |
| 8 TimelineBasedMetric to indicate a problem arised when computing the metric. | 8 TimelineBasedMetric to indicate a problem arised when computing the metric. |
| 9 """ | 9 """ |
| 10 | 10 |
| 11 | 11 |
| 12 def _TimeRangesHasOverlap(iterable_time_ranges): | 12 def _TimeRangesHasOverlap(iterable_time_ranges): |
| 13 """ Returns True if there is are overlapped ranges in 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 | 14 iterable_time_ranges: an iterable of time ranges. Each time range is a |
| 15 tuple (start time, end time). | 15 tuple (start time, end time). |
| 16 """ | 16 """ |
| 17 # Sort the ranges by the start time | 17 # Sort the ranges by the start time |
| 18 sorted_time_ranges = sorted(iterable_time_ranges) | 18 sorted_time_ranges = sorted(iterable_time_ranges) |
| 19 last_range = sorted_time_ranges[0] | 19 last_range = sorted_time_ranges[0] |
| 20 for current_range in sorted_time_ranges[1:]: | 20 for current_range in sorted_time_ranges[1:]: |
| 21 start_current_range = current_range[0] | 21 start_current_range = current_range[0] |
| 22 end_last_range = last_range[1] | 22 end_last_range = last_range[1] |
| 23 if start_current_range < end_last_range: | 23 if start_current_range < end_last_range: |
| 24 return True | 24 return True |
| 25 last_range = current_range | 25 last_range = current_range |
| 26 return False | 26 return False |
| 27 | 27 |
| 28 | 28 |
| 29 def IsEventInInteractions(event, interaction_records): |
| 30 """ Return True if event is in any of the interaction records' time range. |
| 31 |
| 32 Args: |
| 33 event: an instance of telemetry.timeline.event.TimelineEvent. |
| 34 interaction_records: a list of interaction records, whereas each record is |
| 35 an instance of |
| 36 telemetry.web_perf.timeline_interaction_record.TimelineInteractionRecord. |
| 37 |
| 38 Returns: |
| 39 True if |event|'s start & end time is in any of the |interaction_records|'s |
| 40 time range. |
| 41 """ |
| 42 return any(ir.start <= event.start and ir.end >= event.end for ir |
| 43 in interaction_records) |
| 44 |
| 45 |
| 29 class TimelineBasedMetric(object): | 46 class TimelineBasedMetric(object): |
| 30 | 47 |
| 31 def __init__(self): | 48 def __init__(self): |
| 32 """Computes metrics from a telemetry.timeline Model and a range | 49 """Computes metrics from a telemetry.timeline Model and a range |
| 33 | 50 |
| 34 """ | 51 """ |
| 35 super(TimelineBasedMetric, self).__init__() | 52 super(TimelineBasedMetric, self).__init__() |
| 36 | 53 |
| 37 def AddResults(self, model, renderer_thread, interaction_records, results): | 54 def AddResults(self, model, renderer_thread, interaction_records, results): |
| 38 """Computes and adds metrics for the interaction_records' time ranges. | 55 """Computes and adds metrics for the interaction_records' time ranges. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 50 """ | 67 """ |
| 51 raise NotImplementedError() | 68 raise NotImplementedError() |
| 52 | 69 |
| 53 def VerifyNonOverlappedRecords(self, interaction_records): | 70 def VerifyNonOverlappedRecords(self, interaction_records): |
| 54 """This raises exceptions if interaction_records contain overlapped ranges. | 71 """This raises exceptions if interaction_records contain overlapped ranges. |
| 55 """ | 72 """ |
| 56 if _TimeRangesHasOverlap(((r.start, r.end) for r in interaction_records)): | 73 if _TimeRangesHasOverlap(((r.start, r.end) for r in interaction_records)): |
| 57 raise TimelineBasedMetricException( | 74 raise TimelineBasedMetricException( |
| 58 'This metric does not support interaction records with overlapped ' | 75 'This metric does not support interaction records with overlapped ' |
| 59 'time range.') | 76 'time range.') |
| OLD | NEW |