Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 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 time | 4 import time |
| 5 | 5 |
| 6 from telemetry.core import browser_finder | 6 from telemetry.core import browser_finder |
| 7 from telemetry.core import browser_finder_exceptions | 7 from telemetry.core import browser_finder_exceptions |
| 8 from telemetry.core import exceptions | 8 from telemetry.core import exceptions |
| 9 from telemetry.core import util | 9 from telemetry.core import util |
| 10 | 10 |
| 11 | 11 |
| 12 class FastNavigationProfileExtender(object): | 12 class FastNavigationProfileExtender(object): |
| 13 """Extends a Chrome profile. | 13 """Extends a Chrome profile. |
| 14 | 14 |
| 15 This class creates or extends an existing profile by performing a set of tab | 15 This class creates or extends an existing profile by performing a set of tab |
| 16 navigations in large batches. This is accomplished by opening a large number | 16 navigations in large batches. This is accomplished by opening a large number |
| 17 of tabs, simultaneously navigating all the tabs, and then waiting for all the | 17 of tabs, simultaneously navigating all the tabs, and then waiting for all the |
| 18 tabs to load. This provides two benefits: | 18 tabs to load. This provides two benefits: |
| 19 - Takes advantage of the high number of logical cores on modern CPUs. | 19 - Takes advantage of the high number of logical cores on modern CPUs. |
| 20 - The total time spent waiting for navigations to time out scales linearly | 20 - The total time spent waiting for navigations to time out scales linearly |
| 21 with the number of batches, but does not scale with the size of the | 21 with the number of batches, but does not scale with the size of the |
| 22 batch. | 22 batch. |
| 23 """ | 23 """ |
| 24 def __init__(self): | 24 def __init__(self, batch_size): |
| 25 """Initializer. | |
| 26 | |
| 27 Args: | |
| 28 batch_size: A positive integer indicating the number of tabs to | |
| 29 simultaneously perform navigations. | |
| 30 """ | |
| 25 super(FastNavigationProfileExtender, self).__init__() | 31 super(FastNavigationProfileExtender, self).__init__() |
| 26 | 32 |
| 33 # The path of the profile that the browser will use while it's running. | |
| 34 # This member is initialized during SetUp(). | |
| 35 self.profile_path = None | |
|
nednguyen
2015/02/19 00:33:32
If this doesn't need to be accessed from outside,
erikchen
2015/02/19 01:56:03
It needs to be accessed from subclasses. I added a
| |
| 36 | |
| 27 # A reference to the browser that will be performing all of the tab | 37 # A reference to the browser that will be performing all of the tab |
| 28 # navigations. | 38 # navigations. |
| 39 # This member is initialized during SetUp(). | |
| 29 self._browser = None | 40 self._browser = None |
| 30 | 41 |
| 31 # A static copy of the urls that this class is going to navigate to. | |
| 32 self._navigation_urls = None | |
| 33 | |
| 34 # The number of tabs to use. | 42 # The number of tabs to use. |
| 35 self._NUM_TABS = 15 | 43 self._NUM_TABS = batch_size |
| 36 | |
| 37 # The number of pages to load in parallel. | |
| 38 self._NUM_PARALLEL_PAGES = 15 | |
| 39 | |
| 40 assert self._NUM_PARALLEL_PAGES <= self._NUM_TABS, (' the batch size can\'t' | |
| 41 ' be larger than the number of available tabs') | |
| 42 | 44 |
| 43 # The amount of time to wait for a batch of pages to finish loading. | 45 # The amount of time to wait for a batch of pages to finish loading. |
| 44 self._BATCH_PAGE_LOAD_TIMEOUT_IN_SECONDS = 10 | 46 self._BATCH_PAGE_LOAD_TIMEOUT_IN_SECONDS = 10 |
| 45 | 47 |
| 46 # The default amount of time to wait for the retrieval of the URL of a tab. | 48 # The default amount of time to wait for the retrieval of the URL of a tab. |
| 47 self._TAB_URL_RETRIEVAL_TIMEOUT_IN_SECONDS = 1 | 49 self._TAB_URL_RETRIEVAL_TIMEOUT_IN_SECONDS = 1 |
| 48 | 50 |
| 49 def Run(self, finder_options): | 51 def Run(self, finder_options): |
| 50 """Extends the profile. | 52 """Extends the profile. |
| 51 | 53 |
| 52 Args: | 54 Args: |
| 53 finder_options: An instance of BrowserFinderOptions that contains the | 55 finder_options: An instance of BrowserFinderOptions that contains the |
| 54 directory of the input profile, the directory to place the output | 56 directory of the input profile, the directory to place the output |
| 55 profile, and sufficient information to choose a specific browser binary. | 57 profile, and sufficient information to choose a specific browser binary. |
| 56 """ | 58 """ |
| 57 try: | 59 try: |
| 58 self._navigation_urls = self.GetUrlsToNavigate() | 60 self.SetUp(finder_options) |
| 59 self._SetUpBrowser(finder_options) | |
| 60 self._PerformNavigations() | 61 self._PerformNavigations() |
| 61 finally: | 62 finally: |
| 62 self._TearDownBrowser() | 63 self.TearDown() |
| 63 | 64 |
| 64 def GetUrlsToNavigate(self): | 65 def GetUrlsToNavigate(self, maximum_batch_size): |
| 65 """Returns a list of urls to be navigated to. | 66 """Gets URLs for the browser to navigate to. |
| 67 | |
| 68 Intended for subclass override. | |
| 69 | |
| 70 Args: | |
| 71 maximum_batch_size: A positive integer indicating the maximum allowed | |
| 72 length of the returned list. | |
| 73 Returns: | |
| 74 A list of urls to be navigated to. | |
| 75 """ | |
| 76 raise NotImplementedError() | |
| 77 | |
| 78 def ShouldExitAfterBatchNavigation(self): | |
| 79 """Returns a boolean indicating whether profile extension is finished. | |
| 66 | 80 |
| 67 Intended for subclass override. | 81 Intended for subclass override. |
| 68 """ | 82 """ |
| 69 raise NotImplementedError() | 83 raise NotImplementedError() |
| 70 | 84 |
| 85 def SetUp(self, finder_options): | |
| 86 """Finds the browser, starts the browser, and opens the requisite number of | |
| 87 tabs. | |
| 88 | |
| 89 Can be overridden by subclasses. Subclasses must call the super class | |
| 90 implementation. | |
| 91 """ | |
| 92 self.profile_path = finder_options.output_profile_path | |
| 93 possible_browser = self._GetPossibleBrowser(finder_options) | |
| 94 | |
| 95 assert possible_browser.supports_tab_control | |
| 96 assert possible_browser.platform.GetOSName() in ["win", "mac", "linux"] | |
|
nednguyen
2015/02/19 00:33:32
Is the what we want here "possible_browser.platfor
erikchen
2015/02/19 01:56:03
I replaced possible_browser.platform with platform
| |
| 97 self._browser = possible_browser.Create(finder_options) | |
| 98 | |
| 99 while(len(self._browser.tabs) < self._NUM_TABS): | |
| 100 self._browser.tabs.New() | |
| 101 | |
| 102 def TearDown(self): | |
| 103 """Teardown that is guaranteed to be executed before the instance is | |
| 104 destroyed. | |
| 105 | |
| 106 Can be overridden by subclasses. Subclasses must call the super class | |
| 107 implementation. | |
| 108 """ | |
| 109 if self._browser: | |
| 110 self._browser.Close() | |
| 111 self._browser = None | |
| 112 | |
| 113 def CleanUpAfterBatchNavigation(self): | |
| 114 """A hook for subclasses to perform cleanup after each batch of | |
| 115 navigations. | |
| 116 | |
| 117 Can be overridden by subclasses. | |
| 118 """ | |
| 119 pass | |
| 71 | 120 |
| 72 def _GetPossibleBrowser(self, finder_options): | 121 def _GetPossibleBrowser(self, finder_options): |
| 73 """Return a possible_browser with the given options.""" | 122 """Return a possible_browser with the given options.""" |
| 74 possible_browser = browser_finder.FindBrowser(finder_options) | 123 possible_browser = browser_finder.FindBrowser(finder_options) |
| 75 if not possible_browser: | 124 if not possible_browser: |
| 76 raise browser_finder_exceptions.BrowserFinderException( | 125 raise browser_finder_exceptions.BrowserFinderException( |
| 77 'No browser found.\n\nAvailable browsers:\n%s\n' % | 126 'No browser found.\n\nAvailable browsers:\n%s\n' % |
| 78 '\n'.join(browser_finder.GetAllAvailableBrowserTypes(finder_options))) | 127 '\n'.join(browser_finder.GetAllAvailableBrowserTypes(finder_options))) |
| 79 finder_options.browser_options.browser_type = ( | 128 finder_options.browser_options.browser_type = ( |
| 80 possible_browser.browser_type) | 129 possible_browser.browser_type) |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 155 | 204 |
| 156 seconds_to_wait = end_time - time.time() | 205 seconds_to_wait = end_time - time.time() |
| 157 seconds_to_wait = max(0, seconds_to_wait) | 206 seconds_to_wait = max(0, seconds_to_wait) |
| 158 | 207 |
| 159 try: | 208 try: |
| 160 tab.WaitForDocumentReadyStateToBeComplete(seconds_to_wait) | 209 tab.WaitForDocumentReadyStateToBeComplete(seconds_to_wait) |
| 161 except (util.TimeoutException, exceptions.DevtoolsTargetCrashException): | 210 except (util.TimeoutException, exceptions.DevtoolsTargetCrashException): |
| 162 # Ignore time outs and web page crashes. | 211 # Ignore time outs and web page crashes. |
| 163 pass | 212 pass |
| 164 | 213 |
| 165 def _SetUpBrowser(self, finder_options): | |
| 166 """Finds the browser, starts the browser, and opens the requisite number of | |
| 167 tabs.""" | |
| 168 possible_browser = self._GetPossibleBrowser(finder_options) | |
| 169 self._browser = possible_browser.Create(finder_options) | |
| 170 | |
| 171 for _ in range(self._NUM_TABS): | |
| 172 self._browser.tabs.New() | |
| 173 | |
| 174 def _PerformNavigations(self): | 214 def _PerformNavigations(self): |
| 175 """Performs the navigations specified by |_navigation_urls| in large | 215 """Repeatedly fetches a batch of urls, and navigates to those urls. This |
| 176 batches.""" | 216 will run until an empty batch is returned, or |
| 177 # The index of the first url that has not yet been navigated to. | 217 ShouldExitAfterBatchNavigation() returns True. |
| 178 navigation_url_index = 0 | 218 """ |
| 179 while True: | 219 while True: |
| 180 # Generate the next batch of navigations. | 220 # Generate the next batch of navigations. |
| 221 urls = self.GetUrlsToNavigate(self._NUM_TABS) | |
|
nednguyen
2015/02/19 00:33:32
I don't quite like the NUM_TABS param because it r
erikchen
2015/02/19 01:56:03
I like your suggestion. The result is cleaner. Don
| |
| 222 if len(urls) == 0: | |
| 223 break | |
| 224 | |
| 181 batch = [] | 225 batch = [] |
| 182 max_index = min(navigation_url_index + self._NUM_PARALLEL_PAGES, | 226 for i in range(len(urls)): |
| 183 len(self._navigation_urls)) | 227 url = urls[i] |
| 184 for i in range(navigation_url_index, max_index): | 228 tab = self._browser.tabs[i] |
| 185 url = self._navigation_urls[i] | |
| 186 tab = self._browser.tabs[i % self._NUM_TABS] | |
| 187 batch.append((tab, url)) | 229 batch.append((tab, url)) |
| 188 navigation_url_index = max_index | |
| 189 | 230 |
| 190 queued_tabs = self._BatchNavigateTabs(batch) | 231 queued_tabs = self._BatchNavigateTabs(batch) |
| 191 self._WaitForQueuedTabsToLoad(queued_tabs) | 232 self._WaitForQueuedTabsToLoad(queued_tabs) |
| 192 | 233 |
| 193 if navigation_url_index == len(self._navigation_urls): | 234 self.CleanUpAfterBatchNavigation() |
| 235 | |
| 236 if self.ShouldExitAfterBatchNavigation(): | |
| 194 break | 237 break |
| 195 | |
| 196 def _TearDownBrowser(self): | |
| 197 """Teardown that is guaranteed to be executed before the instance is | |
| 198 destroyed.""" | |
| 199 if self._browser: | |
| 200 self._browser.Close() | |
| 201 self._browser = None | |
| OLD | NEW |