Index: chrome/test/functional/perf_endure.py |
diff --git a/chrome/test/functional/perf_endure.py b/chrome/test/functional/perf_endure.py |
index fed79f499f00ed363d487cb579e2d3ff11c9f4d2..ff09fdbcbe0d728af06e320958e6b4e870451ed4 100755 |
--- a/chrome/test/functional/perf_endure.py |
+++ b/chrome/test/functional/perf_endure.py |
@@ -14,6 +14,12 @@ This module accepts the following environment variable inputs: |
DEEP_MEMORY_PROFILE_INTERVAL: The number of seconds to wait in-between each |
sampling for the Deep Memory Profiler. |
DEEP_MEMORY_PROFILE_SAVE: Don't clean up dump files if it's set to 'True'. |
+ |
+ ENDURE_NO_WPR: Run tests without Web Page Replay if it's set. |
+ WPR_RECORD: Run tests in record mode. If you want to make a fresh |
+ archive, please make sure to delete the old one, otherwise |
+ it will append to the old one. |
Nirnimesh
2012/08/07 17:17:34
remove 'please'
fdeng
2012/08/07 21:24:19
Done.
|
+ WPR_ARCHIVE_PATH: an alternative archive file to use. |
""" |
from datetime import datetime |
@@ -33,12 +39,14 @@ import pyauto_utils |
import remote_inspector_client |
import selenium.common.exceptions |
from selenium.webdriver.support.ui import WebDriverWait |
+import webpagereplay |
class NotSupportedEnvironmentError(RuntimeError): |
"""Represent an error raised since the environment (OS) is not supported.""" |
pass |
+ |
class ChromeEndureBaseTest(perf.BasePerfTest): |
"""Implements common functionality for all Chrome Endure tests. |
@@ -62,6 +70,9 @@ class ChromeEndureBaseTest(perf.BasePerfTest): |
_CHROME_BIN_PATH = os.path.join(perf.BasePerfTest.BrowserPath(), 'chrome') |
def setUp(self): |
+ # Parse the environment variable for the usage of Web Page Replay. |
+ # It must be parsed before perf.BasePerfTest.setUp() |
slamm_google
2012/08/07 20:00:43
Better?
# The Web Page Replay environment variabl
fdeng
2012/08/07 21:24:19
Done.
|
+ self._ParseReplayEnv() |
# The environment variables for the Deep Memory Profiler must be set |
# before perf.BasePerfTest.setUp() to inherit them to Chrome. |
self._deep_memory_profile = self._GetDeepMemoryProfileEnv( |
@@ -117,6 +128,7 @@ class ChromeEndureBaseTest(perf.BasePerfTest): |
self._deep_memory_profile_json_file = None |
self._deep_memory_profile_last_json_filename = '' |
self._deep_memory_profile_proc = None |
+ self._StartReplayServerIfNecessary() |
def tearDown(self): |
logging.info('Terminating connection to remote inspector...') |
@@ -139,6 +151,50 @@ class ChromeEndureBaseTest(perf.BasePerfTest): |
not self._deep_memory_profile_save and |
self._deep_tempdir): |
pyauto_utils.RemovePath(self._deep_tempdir) |
+ # Must be done after perf.BasePerfTest.tearDown() |
+ self._StopReplayServerIfNecessary() |
+ |
+ def _GetArchiveName(self): |
+ """Return the Web Page Replay archive name that corresponds to a test. |
+ |
+ Override this function to return the name of an archive that |
+ corresponds to the test, e.g "ChromeEndureGmailTest.wpr". |
+ |
+ Returns: |
+ None, by default no archive name is provided. |
+ """ |
+ return None |
+ |
+ def _ParseReplayEnv(self): |
+ """Parse Web Page Replay related envrionment variables.""" |
+ if 'ENDURE_NO_WPR' in os.environ: |
+ self._use_wpr = False |
+ logging.info('Skipping Web Page Replay since ENDURE_NO_WPR is set.') |
+ else: |
+ self._archive_path = None |
+ if 'WPR_ARCHIVE_PATH' in os.environ: |
+ self._archive_path = os.environ.get('WPR_ARCHIVE_PATH') |
+ else: |
+ if self._GetArchiveName(): |
+ self._archive_path = ChromeEndureReplay.Path( |
+ 'archive', archive_name=self._GetArchiveName()) |
+ self._is_record_mode = 'WPR_RECORD' in os.environ |
+ if self._is_record_mode: |
+ if self._archive_path: |
+ self._use_wpr = True |
fdeng1
2012/08/07 02:56:57
In record mode, we just need an valid archive file
|
+ else: |
+ self._use_wpr = False |
+ logging.info('Fail to record since a valid archive path can not ' + |
+ 'be generated. Did you implement ' + |
+ '_GetArchiveName() in your test?') |
+ else: |
+ if self._archive_path and os.path.exists(self._archive_path): |
+ self._use_wpr = True |
fdeng1
2012/08/07 02:56:57
In replay mode, we need an valid archive file path
|
+ else: |
+ self._use_wpr = False |
+ logging.info( |
+ 'Skipping Web Page Replay since archive file %sdoes not exist.', |
+ self._archive_path+' ' if self._archive_path else '') |
dennis_jeffrey
2012/08/07 19:42:09
add a space right before and right after the +
fdeng
2012/08/07 21:24:19
Done.
|
def _GetDeepMemoryProfileEnv(self, env_name, converter, default): |
"""Returns a converted environment variable for the Deep Memory Profiler. |
@@ -180,7 +236,9 @@ class ChromeEndureBaseTest(perf.BasePerfTest): |
extra_flags = ['--remote-debugging-port=9222'] |
if deep_memory_profile: |
extra_flags.append('--no-sandbox') |
- return (perf.BasePerfTest.ExtraChromeFlags(self) + extra_flags) |
+ if self._use_wpr: |
+ extra_flags.extend(ChromeEndureReplay.CHROME_FLAGS) |
+ return perf.BasePerfTest.ExtraChromeFlags(self) + extra_flags |
def _OnTimelineEvent(self, event_info): |
"""Invoked by the Remote Inspector Client when a timeline event occurs. |
@@ -540,6 +598,30 @@ class ChromeEndureBaseTest(perf.BasePerfTest): |
return True |
+ def _StartReplayServerIfNecessary(self): |
+ """Start replay server if necessary. |
+ |
+ This method needs to be called BEFORE any connection (which is supposed |
+ to go through the Web Page Replay server) occurs. |
slamm_google
2012/08/07 20:00:43
Does this need to be said?
fdeng
2012/08/07 21:24:19
Deleted.
On 2012/08/07 20:00:43, slamm wrote:
|
+ """ |
+ if self._use_wpr: |
+ mode = 'record' if self._is_record_mode else 'replay' |
+ self._wpr_server = ChromeEndureReplay.ReplayServer(self._archive_path) |
+ self._wpr_server.StartServer() |
+ logging.info('Web Page Replay server has started in %s mode.', mode) |
+ |
+ def _StopReplayServerIfNecessary(self): |
+ """Stop the Web Page Replay server if necessary. |
+ |
+ This method has to be called AFTER all network connections which go |
+ through Web Page Replay server have shut down. Otherwise the |
+ Web Page Replay server will hang to wait for them. A good |
+ place is to call it at the end of the teardown process. |
+ """ |
+ if self._use_wpr: |
+ self._wpr_server.StopServer() |
+ logging.info('The Web Page Replay server stopped.') |
+ |
class ChromeEndureControlTest(ChromeEndureBaseTest): |
"""Control tests for Chrome Endure.""" |
@@ -623,6 +705,10 @@ class ChromeEndureGmailTest(ChromeEndureBaseTest): |
self.WaitForDomNode('//a[starts-with(@title, "Inbox")]', |
frame_xpath=self._FRAME_XPATH) |
+ def _GetArchiveName(self): |
+ """Return Web Page Replay archive name.""" |
+ return 'ChromeEndureGmailTest.wpr' |
+ |
def _SwitchToCanvasFrame(self, driver): |
"""Switch the WebDriver to Gmail's 'canvas_frame', if it's available. |
@@ -950,6 +1036,10 @@ class ChromeEndureDocsTest(ChromeEndureBaseTest): |
self._driver = self.NewWebDriver() |
+ def _GetArchiveName(self): |
+ """Return Web Page Replay archive name.""" |
+ return 'ChromeEndureDocsTest.wpr' |
+ |
def testDocsAlternatelyClickLists(self): |
"""Alternates between two different document lists. |
@@ -1020,6 +1110,10 @@ class ChromeEndurePlusTest(ChromeEndureBaseTest): |
self._driver = self.NewWebDriver() |
+ def _GetArchiveName(self): |
+ """Return Web Page Replay archive name.""" |
+ return 'ChromeEndurePlusTest.wpr' |
+ |
def testPlusAlternatelyClickStreams(self): |
"""Alternates between two different streams. |
@@ -1123,5 +1217,34 @@ class IndexedDBOfflineTest(ChromeEndureBaseTest): |
test_description, scenario) |
+class ChromeEndureReplay(object): |
+ """Run Chrome Endure tests with network simulation via Web Page Replay.""" |
+ |
+ _PATHS = { |
+ 'archive': |
+ 'src/chrome/test/data/pyauto_private/webpagereplay/{archive_name}', |
+ 'scripts': |
+ 'src/chrome/test/data/chrome_endure/webpagereplay/wpr_deterministic.js', |
+ } |
+ CHROME_FLAGS = webpagereplay.CHROME_FLAGS |
+ |
+ @classmethod |
+ def Path(cls, key, **kwargs): |
+ return perf.FormatChromePath(cls._PATHS[key], **kwargs) |
+ |
+ @classmethod |
+ def ReplayServer(cls, archive_path): |
+ """Creat a replay server.""" |
slamm_google
2012/08/07 20:00:43
Creat -> Create
fdeng
2012/08/07 21:24:19
Done.
|
+ # Inject customized scripts for Google webapps. |
+ # See the javascript file for details. |
+ scripts = cls.Path('scripts') |
+ if not os.path.exists(scripts): |
+ raise webpagereplay.ReplayNotFoundError('injected scripts', scripts) |
slamm_google
2012/08/07 20:00:43
Instead of using a custom exception from webpagere
fdeng
2012/08/07 21:24:19
Change to IOError
On 2012/08/07 20:00:43, slamm wr
|
+ replay_options = ['--inject_scripts', scripts] |
+ if 'WPR_RECORD' in os.environ: |
+ replay_options.append('--append') |
+ return webpagereplay.ReplayServer(archive_path, replay_options) |
+ |
+ |
if __name__ == '__main__': |
pyauto_functional.Main() |