OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 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 import os | 4 from chrome_remote_control import interaction |
5 | |
6 from chrome_remote_control import multi_page_benchmark | 5 from chrome_remote_control import multi_page_benchmark |
7 from chrome_remote_control import util | |
8 | 6 |
9 class DidNotScrollException(multi_page_benchmark.MeasurementFailure): | 7 class DidNotScrollException(multi_page_benchmark.MeasurementFailure): |
10 def __init__(self): | 8 def __init__(self): |
11 super(DidNotScrollException, self).__init__('Page did not scroll') | 9 super(DidNotScrollException, self).__init__('Page did not scroll') |
12 | 10 |
13 def CalcScrollResults(rendering_stats_deltas): | 11 def CalcScrollResults(rendering_stats_deltas): |
14 num_frames_sent_to_screen = rendering_stats_deltas['numFramesSentToScreen'] | 12 num_frames_sent_to_screen = rendering_stats_deltas['numFramesSentToScreen'] |
15 | 13 |
16 mean_frame_time_seconds = ( | 14 mean_frame_time_seconds = ( |
17 rendering_stats_deltas['totalTimeInSeconds'] / | 15 rendering_stats_deltas['totalTimeInSeconds'] / |
18 float(num_frames_sent_to_screen)) | 16 float(num_frames_sent_to_screen)) |
19 | 17 |
20 dropped_percent = ( | 18 dropped_percent = ( |
21 rendering_stats_deltas['droppedFrameCount'] / | 19 rendering_stats_deltas['droppedFrameCount'] / |
22 float(num_frames_sent_to_screen)) | 20 float(num_frames_sent_to_screen)) |
23 | 21 |
24 return { | 22 return { |
25 'mean_frame_time_ms': round(mean_frame_time_seconds * 1000, 3), | 23 'mean_frame_time_ms': round(mean_frame_time_seconds * 1000, 3), |
26 'dropped_percent': round(dropped_percent * 100, 1) | 24 'dropped_percent': round(dropped_percent * 100, 1) |
27 } | 25 } |
28 | 26 |
29 class ScrollingBenchmark(multi_page_benchmark.MultiPageBenchmark): | 27 class ScrollingBenchmark(multi_page_benchmark.MultiPageBenchmark): |
30 def __init__(self): | 28 def __init__(self): |
31 super(ScrollingBenchmark, self).__init__() | 29 super(ScrollingBenchmark, self).__init__() |
| 30 self._interaction = None |
32 | 31 |
33 def AddOptions(self, parser): | 32 def AddOptions(self, parser): |
34 parser.add_option('--no-gpu-benchmarking-extension', action='store_true', | 33 parser.add_option('--no-gpu-benchmarking-extension', action='store_true', |
35 dest='no_gpu_benchmarking_extension', | 34 dest='no_gpu_benchmarking_extension', |
36 help='Disable the chrome.gpuBenchmarking extension.') | 35 help='Disable the chrome.gpuBenchmarking extension.') |
37 parser.add_option('--report-all-results', dest='report_all_results', | 36 parser.add_option('--report-all-results', dest='report_all_results', |
38 action='store_true', | 37 action='store_true', |
39 help='Reports all data collected, not just FPS') | 38 help='Reports all data collected, not just FPS') |
40 | 39 |
41 @staticmethod | |
42 def ScrollPageFully(page, tab): | |
43 scroll_js_path = os.path.join(os.path.dirname(__file__), 'scroll.js') | |
44 scroll_js = open(scroll_js_path, 'r').read() | |
45 | |
46 # Run scroll test. | |
47 tab.runtime.Execute(scroll_js) | |
48 | |
49 start_scroll_js = """ | |
50 window.__renderingStatsDeltas = null; | |
51 new __ScrollTest(function(rendering_stats_deltas) { | |
52 window.__renderingStatsDeltas = rendering_stats_deltas; | |
53 }).start(element); | |
54 """ | |
55 # scrollable_element_function is a function that passes the scrollable | |
56 # element on the page to a callback. For example: | |
57 # function (callback) { | |
58 # callback(document.getElementById('foo')); | |
59 # } | |
60 if hasattr(page, 'scrollable_element_function'): | |
61 tab.runtime.Execute('(%s)(function(element) { %s });' % | |
62 (page.scrollable_element_function, start_scroll_js)) | |
63 else: | |
64 tab.runtime.Execute('(function() { var element = document.body; %s})();' % | |
65 start_scroll_js) | |
66 | |
67 # Poll for scroll benchmark completion. | |
68 util.WaitFor(lambda: tab.runtime.Evaluate( | |
69 'window.__renderingStatsDeltas'), 60) | |
70 | |
71 rendering_stats_deltas = tab.runtime.Evaluate( | |
72 'window.__renderingStatsDeltas') | |
73 | |
74 if not (rendering_stats_deltas['numFramesSentToScreen'] > 0): | |
75 raise DidNotScrollException() | |
76 return rendering_stats_deltas | |
77 | |
78 def CustomizeBrowserOptions(self, options): | 40 def CustomizeBrowserOptions(self, options): |
79 if not options.no_gpu_benchmarking_extension: | 41 self._interaction = interaction.FindClassWithName('scroll')( |
80 options.extra_browser_args.append('--enable-gpu-benchmarking') | 42 not options.no_gpu_benchmarking_extension) |
| 43 self._interaction.CustomizeBrowserOptions(options) |
81 | 44 |
82 def MeasurePage(self, page, tab): | 45 def MeasurePage(self, page, tab): |
83 rendering_stats_deltas = self.ScrollPageFully(page, tab) | 46 self._interaction.PerformInteraction(tab, page) |
| 47 |
| 48 def DidPerformInteraction(self, scroll, |
| 49 page, tab, rendering_stats_deltas): |
84 scroll_results = CalcScrollResults(rendering_stats_deltas) | 50 scroll_results = CalcScrollResults(rendering_stats_deltas) |
85 if self.options.report_all_results: | 51 if self.options.report_all_results: |
86 all_results = {} | 52 all_results = {} |
87 all_results.update(rendering_stats_deltas) | 53 all_results.update(rendering_stats_deltas) |
88 all_results.update(scroll_results) | 54 all_results.update(scroll_results) |
89 return all_results | 55 return all_results |
90 return scroll_results | 56 return scroll_results |
91 | 57 |
92 | 58 |
93 | 59 |
94 def Main(): | 60 def Main(): |
95 return multi_page_benchmark.Main(ScrollingBenchmark()) | 61 return multi_page_benchmark.Main(ScrollingBenchmark()) |
OLD | NEW |