| OLD | NEW |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 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 from metrics import Metric | 5 from metrics import Metric |
| 6 from metrics import rendering_stats | 6 from metrics import rendering_stats |
| 7 from metrics import statistics | 7 from metrics import statistics |
| 8 from telemetry.core.timeline.model import MarkerMismatchError | 8 from telemetry.core.timeline.model import MarkerMismatchError |
| 9 from telemetry.core.timeline.model import MarkerOverlapError | 9 from telemetry.core.timeline.model import MarkerOverlapError |
| 10 from telemetry.page import page_measurement | 10 from telemetry.page import page_measurement |
| 11 | 11 |
| 12 TIMELINE_MARKER = 'Smoothness' | 12 TIMELINE_MARKER = 'SmoothnessMetric' |
| 13 | 13 |
| 14 | 14 |
| 15 class NotEnoughFramesError(page_measurement.MeasurementFailure): | 15 class NotEnoughFramesError(page_measurement.MeasurementFailure): |
| 16 def __init__(self): | 16 def __init__(self): |
| 17 super(NotEnoughFramesError, self).__init__( | 17 super(NotEnoughFramesError, self).__init__( |
| 18 'Page output less than two frames') | 18 'Page output less than two frames') |
| 19 | 19 |
| 20 | 20 |
| 21 class NoSupportedActionError(page_measurement.MeasurementFailure): | 21 class NoSupportedActionError(page_measurement.MeasurementFailure): |
| 22 def __init__(self): | 22 def __init__(self): |
| 23 super(NoSupportedActionError, self).__init__( | 23 super(NoSupportedActionError, self).__init__( |
| 24 'None of the actions is supported by smoothness measurement') | 24 'None of the actions is supported by smoothness measurement') |
| 25 | 25 |
| 26 | 26 |
| 27 def GetTimelineMarkerNamesFromAction(compound_action): | 27 def GetTimelineMarkerLabelsFromAction(compound_action): |
| 28 timeline_marker_names = [] | 28 timeline_marker_labels = [] |
| 29 if not isinstance(compound_action, list): | 29 if not isinstance(compound_action, list): |
| 30 compound_action = [compound_action] | 30 compound_action = [compound_action] |
| 31 for action in compound_action: | 31 for action in compound_action: |
| 32 if action.GetTimelineMarkerName(): | 32 if action.GetTimelineMarkerLabel(): |
| 33 timeline_marker_names.append(action.GetTimelineMarkerName()) | 33 timeline_marker_labels.append(action.GetTimelineMarkerLabel()) |
| 34 if not timeline_marker_names: | 34 if not timeline_marker_labels: |
| 35 raise NoSupportedActionError() | 35 raise NoSupportedActionError() |
| 36 return timeline_marker_names | 36 return timeline_marker_labels |
| 37 | 37 |
| 38 | 38 |
| 39 class SmoothnessMetric(Metric): | 39 class SmoothnessMetric(Metric): |
| 40 def __init__(self, compound_action): | 40 def __init__(self, compound_action): |
| 41 super(SmoothnessMetric, self).__init__() | 41 super(SmoothnessMetric, self).__init__() |
| 42 self._stats = None | 42 self._stats = None |
| 43 self._compound_action = compound_action | 43 self._compound_action = compound_action |
| 44 | 44 |
| 45 def Start(self, page, tab): | 45 def Start(self, page, tab): |
| 46 # TODO(ermst): Remove "webkit" category after Blink r157377 is picked up by | 46 # TODO(ermst): Remove "webkit" category after Blink r157377 is picked up by |
| 47 # the reference builds. | 47 # the reference builds. |
| 48 tab.browser.StartTracing('webkit,webkit.console,benchmark', 60) | 48 tab.browser.StartTracing('webkit,webkit.console,benchmark', 60) |
| 49 tab.ExecuteJavaScript('console.time("' + TIMELINE_MARKER + '")') | 49 tab.ExecuteJavaScript('console.time("' + TIMELINE_MARKER + '")') |
| 50 | 50 |
| 51 def Stop(self, page, tab): | 51 def Stop(self, page, tab): |
| 52 tab.ExecuteJavaScript('console.timeEnd("' + TIMELINE_MARKER + '")') | 52 tab.ExecuteJavaScript('console.timeEnd("' + TIMELINE_MARKER + '")') |
| 53 timeline_model = tab.browser.StopTracing().AsTimelineModel() | 53 timeline_model = tab.browser.StopTracing().AsTimelineModel() |
| 54 smoothness_marker = timeline_model.FindTimelineMarkers(TIMELINE_MARKER) | 54 render_process_marker = timeline_model.FindTimelineMarkers(TIMELINE_MARKER) |
| 55 timeline_marker_names = GetTimelineMarkerNamesFromAction( | 55 timeline_marker_labels = GetTimelineMarkerLabelsFromAction( |
| 56 self._compound_action) | 56 self._compound_action) |
| 57 try: | 57 try: |
| 58 timeline_markers = timeline_model.FindTimelineMarkers( | 58 timeline_markers = timeline_model.FindTimelineMarkers( |
| 59 timeline_marker_names) | 59 timeline_marker_labels) |
| 60 except MarkerMismatchError: | 60 except MarkerMismatchError: |
| 61 # TODO(ernstm): re-raise exception as MeasurementFailure when the | 61 # TODO(ernstm): re-raise exception as MeasurementFailure when the |
| 62 # reference build was updated. | 62 # reference build was updated. |
| 63 timeline_markers = smoothness_marker | 63 timeline_markers = render_process_marker |
| 64 except MarkerOverlapError as e: | 64 except MarkerOverlapError as e: |
| 65 raise page_measurement.MeasurementFailure(str(e)) | 65 raise page_measurement.MeasurementFailure(str(e)) |
| 66 | 66 |
| 67 renderer_process = timeline_model.GetRendererProcessFromTab(tab) | |
| 68 self._stats = rendering_stats.RenderingStats( | 67 self._stats = rendering_stats.RenderingStats( |
| 69 renderer_process, timeline_markers) | 68 render_process_marker, timeline_markers) |
| 70 | 69 |
| 71 if not self._stats.frame_times: | 70 if not self._stats.frame_times: |
| 72 raise NotEnoughFramesError() | 71 raise NotEnoughFramesError() |
| 73 | 72 |
| 74 def SetStats(self, stats): | |
| 75 """ Pass in a RenderingStats object directly. For unittests that don't call | |
| 76 Start/Stop. | |
| 77 """ | |
| 78 self._stats = stats | |
| 79 | 73 |
| 80 def AddResults(self, tab, results): | 74 def AddResults(self, tab, results): |
| 81 # List of raw frame times. | 75 # List of raw frame times. |
| 82 results.Add('frame_times', 'ms', self._stats.frame_times) | 76 results.Add('frame_times', 'ms', self._stats.frame_times) |
| 83 | 77 |
| 84 # Arithmetic mean of frame times. | 78 # Arithmetic mean of frame times. |
| 85 mean_frame_time = statistics.ArithmeticMean(self._stats.frame_times, | 79 mean_frame_time = statistics.ArithmeticMean(self._stats.frame_times, |
| 86 len(self._stats.frame_times)) | 80 len(self._stats.frame_times)) |
| 87 results.Add('mean_frame_time', 'ms', round(mean_frame_time, 3)) | 81 results.Add('mean_frame_time', 'ms', round(mean_frame_time, 3)) |
| 88 | 82 |
| 89 # Absolute discrepancy of frame time stamps. | 83 # Absolute discrepancy of frame time stamps. |
| 90 jank = statistics.FrameDiscrepancy(self._stats.frame_timestamps) | 84 jank = statistics.FrameDiscrepancy(self._stats.frame_timestamps) |
| 91 results.Add('jank', '', round(jank, 4)) | 85 results.Add('jank', '', round(jank, 4)) |
| 92 | 86 |
| 93 # Are we hitting 60 fps for 95 percent of all frames? | 87 # Are we hitting 60 fps for 95 percent of all frames? |
| 94 # We use 17ms as a slightly looser threshold, instead of 1000.0/60.0. | 88 # We use 17ms as a slightly looser threshold, instead of 1000.0/60.0. |
| 95 percentile_95 = statistics.Percentile(self._stats.frame_times, 95.0) | 89 percentile_95 = statistics.Percentile(self._stats.frame_times, 95.0) |
| 96 results.Add('mostly_smooth', '', 1.0 if percentile_95 < 17.0 else 0.0) | 90 results.Add('mostly_smooth', '', 1.0 if percentile_95 < 17.0 else 0.0) |
| OLD | NEW |