Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 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 """ | 5 """ |
| 6 Cache temperature specifies how the browser cache should be configured before | 6 Cache temperature specifies how the browser cache should be configured before |
| 7 the page run. | 7 the page run. |
| 8 | 8 |
| 9 See design doc for details: | 9 See design doc for details: |
| 10 https://docs.google.com/document/u/1/d/12D7tkhZi887g9d0U2askU9JypU_wYiEI7Lw0bfwx UgA | 10 https://docs.google.com/document/u/1/d/12D7tkhZi887g9d0U2askU9JypU_wYiEI7Lw0bfwx UgA |
| 11 """ | 11 """ |
| 12 | 12 |
| 13 import logging | 13 import logging |
| 14 | 14 |
| 15 import py_utils | |
| 16 | |
| 17 # Default Cache Temperature. The page doesn't care which browser cache state | 15 # Default Cache Temperature. The page doesn't care which browser cache state |
| 18 # it is run on. | 16 # it is run on. |
| 19 ANY = 'any' | 17 ANY = 'any' |
| 20 # Emulates cold runs. Clears various caches and data with using tab.ClearCache() | 18 # Emulates cold runs. Clears various caches and data with using tab.ClearCache() |
| 21 # and tab.ClearDataForOrigin(). | 19 # and tab.ClearDataForOrigin(). |
| 22 COLD = 'cold' | 20 COLD = 'cold' |
| 23 # Emulates warm runs. Ensures that the page was visited at least once just | 21 # Emulates warm runs. Ensures that the page was visited once before the run. |
| 24 # before the run. | |
| 25 WARM = 'warm' | 22 WARM = 'warm' |
| 23 # Emulates hot runs. Ensures that the page was visited at least twice before | |
| 24 # the run. | |
| 25 HOT = 'hot' | |
| 26 | 26 |
| 27 # These regacy states will be removed after chromium test scripts are adapted | 27 # These regacy states will be removed after chromium test scripts are adapted |
| 28 # to new states. | 28 # to new states. |
| 29 PCV1_COLD = COLD | 29 PCV1_COLD = COLD |
| 30 PCV1_WARM = WARM | 30 PCV1_WARM = WARM |
| 31 | 31 |
| 32 class MarkTelemetryInternal(object): | 32 class MarkTelemetryInternal(object): |
| 33 | 33 |
| 34 def __init__(self, browser, identifier): | 34 def __init__(self, browser, identifier): |
| 35 self.browser = browser | 35 self.browser = browser |
| 36 self.identifier = identifier | 36 self.identifier = identifier |
| 37 | 37 |
| 38 def __enter__(self): | 38 def __enter__(self): |
| 39 marker = 'telemetry.internal.%s.start' % self.identifier | 39 marker = 'telemetry.internal.%s.start' % self.identifier |
| 40 self.browser.tabs[0].ExecuteJavaScript( | 40 self.browser.tabs[0].ExecuteJavaScript( |
| 41 "console.time({{ marker }});", marker=marker) | 41 "console.time({{ marker }});", marker=marker) |
| 42 self.browser.tabs[0].ExecuteJavaScript( | 42 self.browser.tabs[0].ExecuteJavaScript( |
| 43 "console.timeEnd({{ marker }});", marker=marker) | 43 "console.timeEnd({{ marker }});", marker=marker) |
| 44 return self | 44 return self |
| 45 | 45 |
| 46 def __exit__(self, exception_type, exception_value, traceback): | 46 def __exit__(self, exception_type, exception_value, traceback): |
| 47 if exception_type: | 47 if exception_type: |
| 48 return True | 48 return True |
| 49 | |
| 50 marker = 'telemetry.internal.%s.end' % self.identifier | 49 marker = 'telemetry.internal.%s.end' % self.identifier |
| 51 self.browser.tabs[0].ExecuteJavaScript( | 50 self.browser.tabs[0].ExecuteJavaScript( |
| 52 "console.time({{ marker }});", marker=marker) | 51 "console.time({{ marker }});", marker=marker) |
| 53 self.browser.tabs[0].ExecuteJavaScript( | 52 self.browser.tabs[0].ExecuteJavaScript( |
| 54 "console.timeEnd({{ marker }});", marker=marker) | 53 "console.timeEnd({{ marker }});", marker=marker) |
| 55 return True | 54 return True |
| 56 | 55 |
| 57 def ClearCache(browser): | 56 def ClearCacheAndData(browser, url): |
| 58 tab = browser.tabs[0] | 57 tab = browser.tabs[0] |
| 59 tab.ClearCache(force=True) | 58 tab.ClearCache(force=True) |
| 59 tab.ClearDataForOrigin(url) | |
| 60 | 60 |
| 61 def WarmCache(page, browser): | 61 def WarmCache(page, browser, temperature): |
| 62 with MarkTelemetryInternal(browser, 'warm_cache'): | 62 with MarkTelemetryInternal(browser, 'warm_cache.%s' % temperature): |
| 63 tab = browser.tabs[0] | 63 tab = browser.tabs[0] |
| 64 tab.Navigate(page.url) | 64 page.RunNavigateSteps(tab.action_runner) |
| 65 py_utils.WaitFor(tab.HasReachedQuiescence, 60) | 65 page.RunPageInteractions(tab.action_runner) |
| 66 tab.WaitForDocumentReadyStateToBeComplete() | |
| 67 tab.Navigate("about:blank") | 66 tab.Navigate("about:blank") |
| 68 tab.WaitForDocumentReadyStateToBeComplete() | 67 tab.WaitForDocumentReadyStateToBeComplete() |
| 68 tab.StopAllServiceWorkers() | |
| 69 | 69 |
| 70 def EnsurePageCacheTemperature(page, browser, previous_page=None): | 70 def EnsurePageCacheTemperature(page, browser, previous_page=None): |
| 71 temperature = page.cache_temperature | 71 temperature = page.cache_temperature |
| 72 logging.info('PageCacheTemperature: %s', temperature) | 72 logging.info('PageCacheTemperature: %s', temperature) |
| 73 if temperature == ANY: | 73 if temperature == ANY: |
| 74 return | 74 return |
| 75 if temperature == COLD: | 75 if temperature == COLD: |
| 76 if previous_page is None: | 76 if previous_page is None: |
| 77 # DiskCache initialization is performed asynchronously on Chrome start-up. | 77 # DiskCache initialization is performed asynchronously on Chrome start-up. |
| 78 # Ensure that DiskCache is initialized before starting the measurement to | 78 # Ensure that DiskCache is initialized before starting the measurement to |
| 79 # avoid performance skew. | 79 # avoid performance skew. |
| 80 # This is done by navigating to an inexistent URL and then wait for the | 80 # This is done by navigating to an inexistent URL and then wait for the |
| 81 # navigation to complete. | 81 # navigation to complete. |
| 82 # TODO(kouhei) Consider moving this logic to PageCyclerStory | 82 # TODO(kouhei) Consider moving this logic to PageCyclerStory |
| 83 with MarkTelemetryInternal(browser, 'ensure_diskcache'): | 83 with MarkTelemetryInternal(browser, 'ensure_diskcache'): |
| 84 tab = browser.tabs[0] | 84 tab = browser.tabs[0] |
| 85 tab.Navigate("http://does.not.exist") | 85 tab.Navigate("http://does.not.exist") |
| 86 tab.WaitForDocumentReadyStateToBeComplete() | 86 tab.WaitForDocumentReadyStateToBeComplete() |
| 87 ClearCache(browser) | 87 ClearCacheAndData(browser, page.url) |
| 88 elif temperature == WARM: | 88 elif temperature == WARM: |
| 89 if (previous_page is not None and | 89 if (previous_page is not None and |
| 90 previous_page.url == page.url and | 90 previous_page.url == page.url and |
| 91 (previous_page.cache_temperature == COLD or | 91 previous_page.cache_temperature == COLD): |
| 92 previous_page.cache_temperature == WARM)): | |
| 93 if '#' in page.url: | 92 if '#' in page.url: |
| 94 # Navigate to inexistent URL to avoid in-page hash navigation. | 93 # Navigate to inexistent URL to avoid in-page hash navigation. |
| 95 # Note: Unlike PCv1, PCv2 iterates the same URL for different cache | 94 # Note: Unlike PCv1, PCv2 iterates the same URL for different cache |
| 96 # configurations. This may issue blink in-page hash navigations, | 95 # configurations. This may issue blink in-page hash navigations, |
| 97 # which isn't intended here. | 96 # which isn't intended here. |
| 98 with MarkTelemetryInternal(browser, 'avoid_double_hash_navigation'): | 97 with MarkTelemetryInternal(browser, 'avoid_double_hash_navigation'): |
| 99 tab = browser.tabs[0] | 98 tab = browser.tabs[0] |
| 100 tab.Navigate("http://does.not.exist") | 99 tab.Navigate("http://does.not.exist") |
| 101 tab.WaitForDocumentReadyStateToBeComplete() | 100 tab.WaitForDocumentReadyStateToBeComplete() |
| 102 return | 101 browser.tabs[0].StopAllServiceWorkers() |
|
kouhei (in TOK)
2017/09/26 04:40:52
Would you add a comment why this is needed?
yukiy
2017/09/26 05:23:32
Done.
Also added a comment for StopAllServiceWorke
| |
| 103 WarmCache(page, browser) | 102 else: |
| 103 ClearCacheAndData(browser, page.url) | |
| 104 WarmCache(page, browser, WARM) | |
| 105 elif temperature == HOT: | |
| 106 if (previous_page is not None and | |
| 107 previous_page.url == page.url and | |
| 108 previous_page.cache_temperature != ANY): | |
| 109 if previous_page.cache_temperature == COLD: | |
| 110 WarmCache(page, browser, HOT) | |
| 111 else: | |
| 112 if '#' in page.url: | |
| 113 # Navigate to inexistent URL to avoid in-page hash navigation. | |
| 114 # Note: Unlike PCv1, PCv2 iterates the same URL for different cache | |
| 115 # configurations. This may issue blink in-page hash navigations, | |
| 116 # which isn't intended here. | |
| 117 with MarkTelemetryInternal(browser, 'avoid_double_hash_navigation'): | |
| 118 tab = browser.tabs[0] | |
| 119 tab.Navigate("http://does.not.exist") | |
| 120 tab.WaitForDocumentReadyStateToBeComplete() | |
| 121 browser.tabs[0].StopAllServiceWorkers() | |
| 122 else: | |
| 123 ClearCacheAndData(browser, page.url) | |
| 124 WarmCache(page, browser, WARM) | |
| 125 WarmCache(page, browser, HOT) | |
| OLD | NEW |