| Index: tools/telemetry/telemetry/web_perf/metrics/timeline_based_metric.py
|
| diff --git a/tools/telemetry/telemetry/web_perf/metrics/timeline_based_metric.py b/tools/telemetry/telemetry/web_perf/metrics/timeline_based_metric.py
|
| index 0a4f56e7c152035ef981154f682986952c287687..e37a1f80a385e8bec0a7d22ba0e78ae7940c0318 100644
|
| --- a/tools/telemetry/telemetry/web_perf/metrics/timeline_based_metric.py
|
| +++ b/tools/telemetry/telemetry/web_perf/metrics/timeline_based_metric.py
|
| @@ -3,7 +3,31 @@
|
| # found in the LICENSE file.
|
|
|
|
|
| +class TimelineBasedMetricException(Exception):
|
| + """Exception that can be thrown from metrics that implements
|
| + TimelineBasedMetric to indicate a problem arised when computing the metric.
|
| + """
|
| +
|
| +
|
| +def _TimeRangesHasOverlap(iterable_time_ranges):
|
| + """ Returns True if there is are overlapped ranges in time ranges.
|
| + iterable_time_ranges: an iterable of time ranges. Each time range is a
|
| + tuple (start time, end time).
|
| + """
|
| + # Sort the ranges by the start time
|
| + sorted_time_ranges = sorted(iterable_time_ranges)
|
| + last_range = sorted_time_ranges[0]
|
| + for current_range in sorted_time_ranges[1:]:
|
| + start_current_range = current_range[0]
|
| + end_last_range = last_range[1]
|
| + if start_current_range < end_last_range:
|
| + return True
|
| + last_range = current_range
|
| + return False
|
| +
|
| +
|
| class TimelineBasedMetric(object):
|
| +
|
| def __init__(self):
|
| """Computes metrics from a telemetry.core.timeline Model and a range
|
|
|
| @@ -16,9 +40,20 @@ class TimelineBasedMetric(object):
|
| The override of this method should compute results on the data **only**
|
| within the interaction_records' start and end time ranges.
|
|
|
| - model is an instance of telemetry.core.timeline.model.TimelineModel.
|
| - interaction_records is a list of instances of TimelineInteractionRecord.
|
| - results is an instance of page.PageTestResults.
|
| + Args:
|
| + model: An instance of telemetry.core.timeline.model.TimelineModel.
|
| + interaction_records: A list of instances of TimelineInteractionRecord. If
|
| + the override of this method doesn't support overlapped ranges, use
|
| + VerifyNonOverlappedRecords to check that no records are overlapped.
|
| + results: An instance of page.PageTestResults.
|
|
|
| """
|
| raise NotImplementedError()
|
| +
|
| + def VerifyNonOverlappedRecords(self, interaction_records):
|
| + """This raises exceptions if interaction_records contain overlapped ranges.
|
| + """
|
| + if _TimeRangesHasOverlap(((r.start, r.end) for r in interaction_records)):
|
| + raise TimelineBasedMetricException(
|
| + 'This metric does not support interaction records with overlapped '
|
| + 'time range.')
|
|
|