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 from telemetry.core import util | 14 from telemetry.core import util |
15 | 15 |
16 # Default Cache Temperature. The page doesn't care which browser cache state | 16 # Default Cache Temperature. The page doesn't care which browser cache state |
17 # it is run on. | 17 # it is run on. |
18 ANY = 'any' | 18 ANY = 'any' |
19 # Emulates PageCycler V1 cold runs. Clears system DNS cache, browser DiskCache, | 19 # Emulates PageCycler V1 cold runs. Clears system DNS cache, browser DiskCache, |
20 # net/ predictor cache, and net/ host resolver cache. | 20 # net/ predictor cache, and net/ host resolver cache. |
21 PCV1_COLD = 'pcv1-cold' | 21 PCV1_COLD = 'pcv1-cold' |
22 # Emulates PageCycler V1 warm runs. Ensures that the page was visited at least | 22 # Emulates PageCycler V1 warm runs. Ensures that the page was visited at least |
23 # once just before the run. | 23 # once just before the run. |
24 PCV1_WARM = 'pcv1-warm' | 24 PCV1_WARM = 'pcv1-warm' |
25 | 25 |
26 class MarkTelemetryInternal(object): | |
27 | |
28 def __init__(self, browser, identifier): | |
29 self.browser = browser | |
30 self.identifier = identifier | |
31 | |
32 def __enter__(self): | |
33 self.browser.tabs[0].ExecuteJavaScript( | |
34 """console.time('telemetry.{0}.warmCache.start');""".format( | |
35 self.identifier)) | |
36 self.browser.tabs[0].ExecuteJavaScript( | |
37 """console.timeEnd('telemetry.{0}.warmCache.start');""".format( | |
38 self.identifier)) | |
39 return self | |
40 | |
41 def __exit__(self, exception_type, exception_value, traceback): | |
42 if exception_type: | |
43 return True | |
44 | |
45 self.browser.tabs[0].ExecuteJavaScript( | |
46 """console.time('telemetry.{0}.warmCache.end');""".format( | |
47 self.identifier)) | |
48 self.browser.tabs[0].ExecuteJavaScript( | |
49 """console.timeEnd('telemetry.{0}.warmCache.end');""".format( | |
50 self.identifier)) | |
51 return True | |
26 | 52 |
27 def EnsurePageCacheTemperature(page, browser, previous_page=None): | 53 def EnsurePageCacheTemperature(page, browser, previous_page=None): |
28 temperature = page.cache_temperature | 54 temperature = page.cache_temperature |
29 logging.info('PageCacheTemperature: %s', temperature) | 55 logging.info('PageCacheTemperature: %s', temperature) |
30 | 56 |
31 if temperature == ANY: | 57 if temperature == ANY: |
32 return | 58 return |
33 elif temperature == PCV1_COLD: | 59 |
34 any_valid_tab = browser.tabs[0] | 60 if temperature == PCV1_COLD: |
35 any_valid_tab.ClearCache(force=True) | 61 if previous_page is None: |
62 with MarkTelemetryInternal(browser, 'ensure_diskcache'): | |
63 tab = browser.tabs[0] | |
64 tab.Navigate("http://does.not.exist") | |
nednguyen
2016/05/18 01:51:13
Err I forgot to this. Can you add some comment exp
| |
65 tab.WaitForDocumentReadyStateToBeComplete() | |
66 | |
67 any_tab = browser.tabs[0] | |
68 any_tab.ClearCache(force=True) | |
36 elif temperature == PCV1_WARM: | 69 elif temperature == PCV1_WARM: |
37 if (previous_page is not None and | 70 if (previous_page is not None and |
38 previous_page.url == page.url and | 71 previous_page.url == page.url and |
39 (previous_page.cache_temperature == PCV1_COLD or | 72 (previous_page.cache_temperature == PCV1_COLD or |
40 previous_page.cache_temperature == PCV1_WARM)): | 73 previous_page.cache_temperature == PCV1_WARM)): |
41 return | 74 return |
42 | 75 |
43 tab = browser.tabs[0] | 76 with MarkTelemetryInternal(browser, 'warmCache'): |
44 tab.ExecuteJavaScript( | 77 tab = browser.tabs[0] |
45 """console.time('telemetry.internal.warmCache.start');""") | 78 tab.Navigate(page.url) |
46 tab.ExecuteJavaScript( | 79 util.WaitFor(tab.HasReachedQuiescence, 60) |
47 """console.timeEnd('telemetry.internal.warmCache.start');""") | 80 tab.WaitForDocumentReadyStateToBeComplete() |
48 tab.Navigate(page.url) | 81 tab.Navigate("about:blank") |
49 util.WaitFor(tab.HasReachedQuiescence, 60) | 82 tab.WaitForDocumentReadyStateToBeComplete() |
50 tab.WaitForDocumentReadyStateToBeComplete() | |
51 tab.Navigate("about:blank") | |
52 tab.WaitForDocumentReadyStateToBeComplete() | |
53 tab.ExecuteJavaScript( | |
54 """console.time('telemetry.internal.warmCache.end');""") | |
55 tab.ExecuteJavaScript( | |
56 """console.timeEnd('telemetry.internal.warmCache.end');""") | |
OLD | NEW |