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
3 # found in the LICENSE file.
6 class TimelineBasedMetricException(Exception):
7 """Exception that can be thrown from metrics that implements
8 TimelineBasedMetric to indicate a problem arose when computing the metric.
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).
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
:
25 last_range
= current_range
29 def IsEventInInteractions(event
, interaction_records
):
30 """ Return True if event is in any of the interaction records' time range.
33 event: an instance of telemetry.timeline.event.TimelineEvent.
34 interaction_records: a list of interaction records, whereas each record is
36 telemetry.web_perf.timeline_interaction_record.TimelineInteractionRecord.
39 True if |event|'s start & end time is in any of the |interaction_records|'s
42 return any(ir
.start
<= event
.start
and ir
.end
>= event
.end
for ir
43 in interaction_records
)
46 class TimelineBasedMetric(object):
49 """Computes metrics from a telemetry.timeline Model and a range
52 super(TimelineBasedMetric
, self
).__init
__()
54 def AddResults(self
, model
, renderer_thread
, interaction_records
, results
):
55 """Computes and adds metrics for the interaction_records' time ranges.
57 The override of this method should compute results on the data **only**
58 within the interaction_records' start and end time ranges.
61 model: An instance of telemetry.timeline.model.TimelineModel.
62 interaction_records: A list of instances of TimelineInteractionRecord. If
63 the override of this method doesn't support overlapped ranges, use
64 VerifyNonOverlappedRecords to check that no records are overlapped.
65 results: An instance of page.PageTestResults.
68 raise NotImplementedError()
70 def AddWholeTraceResults(self
, model
, results
):
71 """Computes and adds metrics corresponding to the entire trace.
73 Override this method to compute results that correspond to the whole trace.
76 model: An instance of telemetry.timeline.model.TimelineModel.
77 results: An instance of page.PageTestResults.
81 def VerifyNonOverlappedRecords(self
, interaction_records
):
82 """This raises exceptions if interaction_records contain overlapped ranges.
84 if _TimeRangesHasOverlap(((r
.start
, r
.end
) for r
in interaction_records
)):
85 raise TimelineBasedMetricException(
86 'This metric does not support interaction records with overlapped '