Index: chrome/test/functional/perf_endure.py |
diff --git a/chrome/test/functional/perf_endure.py b/chrome/test/functional/perf_endure.py |
index 134c9dfdd13c3b82b20b82cdbd9a1453acb11c41..80a5e24e7a1979d1219bd49e96235e5e637d4af7 100755 |
--- a/chrome/test/functional/perf_endure.py |
+++ b/chrome/test/functional/perf_endure.py |
@@ -33,12 +33,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 +64,14 @@ class ChromeEndureBaseTest(perf.BasePerfTest): |
_CHROME_BIN_PATH = os.path.join(perf.BasePerfTest.BrowserPath(), 'chrome') |
def setUp(self): |
+ # The environment variable for the usage of Web Page Replay. |
+ # It must be parsed before perf.BasePerfTest.setUp() |
+ self._use_wpr = self._NeedReplayServer() |
+ # Do NOT mannually set WPR_RECORD for the purpose of |
+ # making recording archive for endure tests. Run record_endure.py instead. |
+ if self._use_wpr: |
+ self._is_replay_mode = 'WPR_RECORD' not in os.environ |
+ |
# 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 +127,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 +150,37 @@ 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() |
fdeng1
2012/08/02 22:57:30
StopReplayServerIfNecessary()/StartReplayServerIfN
|
+ |
+ def _NeedReplayServer(self): |
+ """Check whether the test needs to run against Web Page Replay server. |
+ |
+ Override this function to return False if you want to permanently |
+ turn off Web Page Replay for a test. |
+ |
+ Returns: |
+ True if need replay server otherwise False. |
+ |
+ Environment variable: |
+ ENDURE_NO_WPR: if set, do not need replay server |
+ """ |
+ if 'ENDURE_NO_WPR' in os.environ: |
+ return False |
+ else: |
+ return True |
+ |
fdeng1
2012/08/02 22:57:30
Two tests are offline and don't need WPR. These te
|
+ def _GenArchiveName(self): |
+ """Returns the Web Page Replay archive name that corresponds to a test. |
+ |
+ Use test class name as archive name, e.g. ChromeEndureGmailTest.wpr |
+ Override this function to allow other names. |
+ |
+ Returns: |
+ An archive name generated using test class name. |
+ """ |
+ # Use class name as archive name. |
+ return self.id().split('.')[1] + '.wpr' |
fdeng1
2012/08/02 22:57:30
I add a function called _GenArchiveName which uses
|
def _GetDeepMemoryProfileEnv(self, env_name, converter, default): |
"""Returns a converted environment variable for the Deep Memory Profiler. |
@@ -180,7 +222,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 +584,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. |
+ """ |
+ if self._use_wpr and self._is_replay_mode: |
+ archive_name = self._GenArchiveName() |
+ self._wpr_server = ChromeEndureReplay.ReplayServer(archive_name) |
+ self._wpr_server.StartServer() |
+ logging.info('Web Page Replay server has started in Replay 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 and self._is_replay_mode: |
+ self._wpr_server.StopServer() |
+ logging.info('The Web Page Replay server stopped.') |
+ |
class ChromeEndureControlTest(ChromeEndureBaseTest): |
"""Control tests for Chrome Endure.""" |
@@ -547,6 +615,10 @@ class ChromeEndureControlTest(ChromeEndureBaseTest): |
_WEBAPP_NAME = 'Control' |
_TAB_TITLE_SUBSTRING = 'Chrome Endure Control Test' |
+ def _NeedReplayServer(self): |
+ """Override parent's function to remove replay support.""" |
+ return False |
+ |
def testControlAttachDetachDOMTree(self): |
"""Continually attach and detach a DOM tree from a basic document.""" |
test_description = 'AttachDetachDOMTree' |
@@ -1086,6 +1158,10 @@ class IndexedDBOfflineTest(ChromeEndureBaseTest): |
self._driver = self.NewWebDriver() |
+ def _NeedReplayServer(self): |
+ """Override parent's function to remove replay support.""" |
+ return False |
+ |
def testOfflineOnline(self): |
"""Simulates user input while offline and sync while online. |
@@ -1124,5 +1200,33 @@ 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_name): |
+ """Creat a replay server.""" |
+ # Inject customized scripts for Google webapps. |
+ # See the java script file for details. |
+ scripts = cls.Path('scripts') |
+ if not os.path.exists(scripts): |
+ raise webpagereplay.ReplayNotFoundError('injected scripts', scripts) |
+ replay_options = ['--inject_scripts', scripts] |
+ archive_path = cls.Path('archive', archive_name=archive_name) |
+ return webpagereplay.ReplayServer(archive_path, replay_options) |
+ |
+ |
if __name__ == '__main__': |
pyauto_functional.Main() |