Commit b3931d51 authored by nednguyen@google.com's avatar nednguyen@google.com

Add check for overlapped record ranges.

BUG=345922

Review URL: https://codereview.chromium.org/296053017

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@272475 0039d316-1c4b-4281-b951-d872f2087c98
parent ef401eed
...@@ -13,6 +13,7 @@ class SmoothnessMetric(timeline_based_metric.TimelineBasedMetric): ...@@ -13,6 +13,7 @@ class SmoothnessMetric(timeline_based_metric.TimelineBasedMetric):
super(SmoothnessMetric, self).__init__() super(SmoothnessMetric, self).__init__()
def AddResults(self, model, renderer_thread, interaction_records, results): def AddResults(self, model, renderer_thread, interaction_records, results):
self.VerifyNonOverlappedRecords(interaction_records)
renderer_process = renderer_thread.parent renderer_process = renderer_thread.parent
stats = rendering_stats.RenderingStats( stats = rendering_stats.RenderingStats(
renderer_process, model.browser_process, renderer_process, model.browser_process,
......
...@@ -3,7 +3,31 @@ ...@@ -3,7 +3,31 @@
# found in the LICENSE file. # 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): class TimelineBasedMetric(object):
def __init__(self): def __init__(self):
"""Computes metrics from a telemetry.core.timeline Model and a range """Computes metrics from a telemetry.core.timeline Model and a range
...@@ -16,9 +40,20 @@ class TimelineBasedMetric(object): ...@@ -16,9 +40,20 @@ class TimelineBasedMetric(object):
The override of this method should compute results on the data **only** The override of this method should compute results on the data **only**
within the interaction_records' start and end time ranges. within the interaction_records' start and end time ranges.
model is an instance of telemetry.core.timeline.model.TimelineModel. Args:
interaction_records is a list of instances of TimelineInteractionRecord. model: An instance of telemetry.core.timeline.model.TimelineModel.
results is an instance of page.PageTestResults. 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() 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.')
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import unittest
import telemetry.web_perf.metrics.timeline_based_metric as tbm_module
class TimelineBasedMetricTest(unittest.TestCase):
# pylint: disable=W0212
def testTimeRangesHasOverlap(self):
# Test cases with overlap on one side
self.assertTrue(tbm_module._TimeRangesHasOverlap([(10, 20), (5, 15)]))
self.assertTrue(tbm_module._TimeRangesHasOverlap([(5, 15), (10, 20)]))
self.assertTrue(tbm_module._TimeRangesHasOverlap(
[(5, 15), (25, 30), (10, 20)]))
# Test cases with one range fall in the middle of other
self.assertTrue(tbm_module._TimeRangesHasOverlap([(10, 20), (15, 18)]))
self.assertTrue(tbm_module._TimeRangesHasOverlap([(15, 18), (10, 20)]))
self.assertTrue(tbm_module._TimeRangesHasOverlap(
[(15, 18), (40, 50), (10, 20)]))
self.assertFalse(tbm_module._TimeRangesHasOverlap([(15, 18), (20, 25)]))
self.assertFalse(tbm_module._TimeRangesHasOverlap(
[(1, 2), (2, 3), (0, 1)]))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment