| 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 csv | 4 import csv |
| 5 import logging | 5 import logging |
| 6 import os | 6 import os |
| 7 import sys | 7 import sys |
| 8 import time | 8 import time |
| 9 import unittest | 9 import unittest |
| 10 | 10 |
| 11 import chrome_remote_control | 11 import chrome_remote_control |
| 12 import chrome_remote_control.browser_options | 12 import chrome_remote_control.browser_options |
| 13 import file_server | |
| 14 import page_set | 13 import page_set |
| 15 | 14 |
| 16 class MeasurementFailure(Exception): | 15 class MeasurementFailure(Exception): |
| 17 """Exception that can be thrown from MeasurePage to indicate an undisired but | 16 """Exception that can be thrown from MeasurePage to indicate an undisired but |
| 18 designed-for problem.""" | 17 designed-for problem.""" |
| 19 pass | 18 pass |
| 20 | 19 |
| 21 class MultiPageBenchmark(object): | 20 class MultiPageBenchmark(object): |
| 22 """Glue code for running a benchmark across a set of pages. | 21 """Glue code for running a benchmark across a set of pages. |
| 23 | 22 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 Put together: | 80 Put together: |
| 82 | 81 |
| 83 def MeasurePage(self, page, tab): | 82 def MeasurePage(self, page, tab): |
| 84 res = tab.runtime.Evaluate("2+2"); | 83 res = tab.runtime.Evaluate("2+2"); |
| 85 if res != 4: | 84 if res != 4: |
| 86 raise Exception("Oh, wow.") | 85 raise Exception("Oh, wow.") |
| 87 return {"two_plus_two": res} | 86 return {"two_plus_two": res} |
| 88 """ | 87 """ |
| 89 raise Exception('Should never be called') | 88 raise Exception('Should never be called') |
| 90 | 89 |
| 91 def Run(self, results_writer, possible_browser, options, ps): | 90 def Run(self, results_writer, browser, options, ps): |
| 92 """Runs the actual benchmark, starting the browser using outputting results | 91 """Runs the actual benchmark, starting the browser using outputting results |
| 93 to results_writer. | 92 to results_writer. |
| 94 | 93 |
| 95 If args is not specified, sys.argv[1:] is used. | 94 If args is not specified, sys.argv[1:] is used. |
| 96 """ | 95 """ |
| 97 self.options = options | 96 self.options = options |
| 98 self.field_names = None | 97 self.field_names = None |
| 99 | 98 |
| 100 with possible_browser.Create() as b: | 99 with browser.ConnectToNthTab(0) as tab: |
| 101 with b.ConnectToNthTab(0) as tab: | 100 for page in ps.pages: |
| 102 for page in ps.pages: | 101 self._RunPage(results_writer, page, tab) |
| 103 self._RunPage(results_writer, page, tab) | |
| 104 self.options = None | 102 self.options = None |
| 105 self.field_names = None | 103 self.field_names = None |
| 106 | 104 |
| 107 def _RunPage(self, results_writer, page, tab): | 105 def _RunPage(self, results_writer, page, tab): |
| 108 # Load the page. | 106 # Load the page. |
| 109 try: | 107 try: |
| 110 logging.debug('Loading %s...', page.url) | 108 logging.debug('Loading %s...', page.url) |
| 111 tab.page.Navigate(page.url) | 109 tab.page.Navigate(page.url) |
| 112 | 110 |
| 113 # TODO(dtu): Detect HTTP redirects. | 111 # TODO(dtu): Detect HTTP redirects. |
| 114 time.sleep(2) # Wait for unpredictable redirects. | 112 time.sleep(2) # Wait for unpredictable redirects. |
| 115 tab.WaitForDocumentReadyStateToBeInteractiveOrBetter() | 113 tab.WaitForDocumentReadyStateToBeInteractiveOrBetter() |
| 116 except chrome_remote_control.TimeoutException, ex: | 114 except chrome_remote_control.TimeoutException, ex: |
| 117 logging.warning('Timed out while loading: %s', page.url) | 115 logging.warning('Timed out while loading: %s', page.url) |
| 118 self.page_failures.append({"page": page, | 116 self.page_failures.append({"page": page, |
| 119 "exception": ex}) | 117 "exception": ex}) |
| 120 return | 118 return |
| 121 | 119 |
| 122 # Measure the page. | 120 # Measure the page. |
| 123 try: | 121 try: |
| 124 results = self.MeasurePage(page, tab) | 122 results = self.MeasurePage(page, tab) |
| 125 except MeasurementFailure, ex: | 123 except Exception, ex: |
| 126 logging.warning("%s failed: %s:", page.url, str(ex)) | 124 if isinstance(ex, MeasurementFailure): |
| 125 logging.info("%s failed: %s:", page.url, str(ex)) |
| 126 else: |
| 127 import traceback; traceback.print_exc() |
| 128 logging.warning("%s had unexpected failure: %s:", page.url, str(ex)) |
| 129 |
| 127 self.page_failures.append({"page": page, | 130 self.page_failures.append({"page": page, |
| 128 "exception": ex}) | 131 "exception": ex}) |
| 129 return | 132 return |
| 130 except Exception, ex: | |
| 131 import traceback; traceback.print_exc() | |
| 132 logging.warning("%s had unexpected failure: %s:", page.url, str(ex)) | |
| 133 self.page_failures.append({"page": page, | |
| 134 "exception": ex}) | |
| 135 return | |
| 136 | 133 |
| 137 # Output. | 134 # Output. |
| 138 assert 'url' not in results | 135 assert 'url' not in results |
| 139 | 136 |
| 140 if not self.field_names: | 137 if not self.field_names: |
| 141 self.field_names = list(results.keys()) | 138 self.field_names = list(results.keys()) |
| 142 self.field_names.sort() | 139 self.field_names.sort() |
| 143 self.field_names.insert(0, 'url') | 140 self.field_names.insert(0, 'url') |
| 144 results_writer.writerow(self.field_names) | 141 results_writer.writerow(self.field_names) |
| 145 | 142 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 ps.LoadFromFile(args[0]) | 177 ps.LoadFromFile(args[0]) |
| 181 | 178 |
| 182 benchmark.CustomizeBrowserOptions(options) | 179 benchmark.CustomizeBrowserOptions(options) |
| 183 possible_browser = chrome_remote_control.FindBrowser(options) | 180 possible_browser = chrome_remote_control.FindBrowser(options) |
| 184 if possible_browser == None: | 181 if possible_browser == None: |
| 185 sys.stderr.write( | 182 sys.stderr.write( |
| 186 "No browser found.\n" + | 183 "No browser found.\n" + |
| 187 "Use --browser=list to figure out which are available.\n") | 184 "Use --browser=list to figure out which are available.\n") |
| 188 sys.exit(1) | 185 sys.exit(1) |
| 189 | 186 |
| 190 res = benchmark.Run(csv.writer(sys.stdout), | 187 with possible_browser.Create() as browser: |
| 191 possible_browser, | 188 res = benchmark.Run(csv.writer(sys.stdout), |
| 192 options, | 189 browser, |
| 193 ps) | 190 options, |
| 191 ps) |
| 194 | 192 |
| 195 if len(benchmark.page_failures): | 193 if len(benchmark.page_failures): |
| 196 logging.warning("Failed pages: %s", '\n'.join( | 194 logging.warning("Failed pages: %s", '\n'.join( |
| 197 [failure["page"].url for failure in benchmark.page_failures])) | 195 [failure["page"].url for failure in benchmark.page_failures])) |
| 198 return len(benchmark.page_failures) | 196 return len(benchmark.page_failures) |
| 199 | 197 |
| 200 | 198 |
| 201 class MultiPageBenchmarkUnitTest(unittest.TestCase): | 199 class MultiPageBenchmarkUnitTest(unittest.TestCase): |
| 202 """unittest.TestCase-derived class to help in the construction of unit tests | 200 """unittest.TestCase-derived class to help in the construction of unit tests |
| 203 for a benchmark.""" | 201 for a benchmark.""" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 218 | 216 |
| 219 def RunBenchmark(self, benchmark, ps): | 217 def RunBenchmark(self, benchmark, ps): |
| 220 """Runs a benchmark against a pageset, returning the rows its outputs.""" | 218 """Runs a benchmark against a pageset, returning the rows its outputs.""" |
| 221 rows = [] | 219 rows = [] |
| 222 class LocalWriter(object): | 220 class LocalWriter(object): |
| 223 def writerow(self, row): | 221 def writerow(self, row): |
| 224 rows.append(row) | 222 rows.append(row) |
| 225 | 223 |
| 226 assert chrome_remote_control.browser_options.options_for_unittests | 224 assert chrome_remote_control.browser_options.options_for_unittests |
| 227 | 225 |
| 228 with file_server.FileServer(self.unittest_data_dir) as server: | 226 options = ( |
| 229 for page in ps.pages: | 227 chrome_remote_control.browser_options.options_for_unittests.Copy()) |
| 230 page.url = '%s/%s' % (server.url, page.url) | 228 benchmark.CustomizeBrowserOptions(options) |
| 231 options = ( | 229 possible_browser = chrome_remote_control.FindBrowser(options) |
| 232 chrome_remote_control.browser_options.options_for_unittests.Copy()) | 230 with possible_browser.Create() as browser: |
| 233 benchmark.CustomizeBrowserOptions(options) | 231 with browser.CreateTemporaryHTTPServer(self.unittest_data_dir) as server: |
| 234 possible_browser = chrome_remote_control.FindBrowser(options) | 232 for page in ps.pages: |
| 235 benchmark.Run(LocalWriter(), possible_browser, options, ps) | 233 page.url = '%s/%s' % (server.url, page.url) |
| 236 | 234 benchmark.Run(LocalWriter(), browser, options, ps) |
| 237 return rows | 235 return rows |
| OLD | NEW |