OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 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 """Constrained network server (CNS) test base.""" | 5 """Constrained network server (CNS) test base.""" |
6 | 6 |
7 import logging | 7 import logging |
8 import os | 8 import os |
9 import Queue | 9 import Queue |
10 import subprocess | 10 import subprocess |
11 import sys | 11 import sys |
12 import threading | 12 import threading |
13 import urllib2 | 13 import urllib2 |
14 | 14 |
15 import pyauto | 15 import pyauto |
16 import pyauto_paths | 16 import pyauto_paths |
17 | 17 |
| 18 WINDOWS = 'win32' in sys.platform |
| 19 |
18 # List of commonly used network constraints settings. | 20 # List of commonly used network constraints settings. |
19 # Each setting is a tuppe of the form: | 21 # Each setting is a tuppe of the form: |
20 # ('TEST_NAME', [BANDWIDTH_Kbps, LATENCY_ms, PACKET_LOSS_%]) | 22 # ('TEST_NAME', [BANDWIDTH_Kbps, LATENCY_ms, PACKET_LOSS_%]) |
21 # | 23 # |
22 # Note: The test name should satisfy the regex [\w\.-]+ (check | 24 # Note: The test name should satisfy the regex [\w\.-]+ (check |
23 # tools/perf_expectations/tests/perf_expectations_unittest.py for details). It | 25 # tools/perf_expectations/tests/perf_expectations_unittest.py for details). It |
24 # is used to name the result graphs on the dashboards. | 26 # is used to name the result graphs on the dashboards. |
25 # | 27 # |
26 # The WiFi, DSL, and Cable settings were taken from webpagetest.org as | 28 # The WiFi, DSL, and Cable settings were taken from webpagetest.org as |
27 # approximations of their respective real world networks. The settings were | 29 # approximations of their respective real world networks. The settings were |
28 # based on 2011 FCC Broadband Data report (http://www.fcc.gov/document/ | 30 # based on 2011 FCC Broadband Data report (http://www.fcc.gov/document/ |
29 # measuring-broadband-america-report-consumer-broadband-performance-us). | 31 # measuring-broadband-america-report-consumer-broadband-performance-us). |
30 DialUp = ('DialUp', [56, 120, 5]) | 32 DialUp = ('DialUp', [56, 120, 5]) |
31 Slow = ('Slow', [256, 105, 1]) | 33 Slow = ('Slow', [256, 105, 1]) |
32 Wifi = ('Wifi', [1024, 60, 0]) | 34 Wifi = ('Wifi', [1024, 60, 0]) |
33 DSL = ('DSL', [1541, 50, 0]) | 35 DSL = ('DSL', [1541, 50, 0]) |
34 Cable = ('Cable', [5120, 28, 0]) | 36 Cable = ('Cable', [5120, 28, 0]) |
35 NoConstraints = ('NoConstraints', [0, 0, 0]) | 37 NoConstraints = ('NoConstraints', [0, 0, 0]) |
36 | 38 |
37 # Path to CNS executable relative to source root. | 39 # Path to CNS executable relative to source root. |
38 _CNS_PATH = os.path.join( | 40 _CNS_PATH = os.path.join( |
39 'media', 'tools', 'constrained_network_server', 'cns.py') | 41 'media', 'tools', 'constrained_network_server', 'cns.py') |
40 | 42 |
41 # Port to start the CNS on. | 43 # Port to start the CNS on. |
42 _CNS_PORT = 9000 | 44 _CNS_PORT = 9000 |
43 | 45 |
44 # Base CNS URL, only requires & separated parameter names appended. | 46 # Base CNS URL, only requires & separated parameter names appended. |
45 CNS_BASE_URL = 'http://127.0.0.1:%d/ServeConstrained?' % _CNS_PORT | 47 if WINDOWS: |
| 48 CNS_BASE_URL = 'http://chromeperf34.chrome:%d/ServeConstrained?' % _CNS_PORT |
| 49 else: |
| 50 CNS_BASE_URL = 'http://127.0.0.1:%d/ServeConstrained?' % _CNS_PORT |
46 | 51 |
47 # Used for server sanity check. | 52 # Used for server sanity check. |
48 _TEST_VIDEO = 'roller.webm' | 53 _TEST_VIDEO = 'roller.webm' |
49 | 54 |
50 # Directory root to serve files from. | 55 # Directory root to serve files from. |
51 _ROOT_PATH = os.path.join(pyauto.PyUITest.DataDir(), 'pyauto_private', 'media') | 56 _ROOT_PATH = os.path.join(pyauto.PyUITest.DataDir(), 'pyauto_private', 'media') |
52 | 57 |
53 | 58 |
54 class CNSTestBase(pyauto.PyUITest): | 59 class CNSTestBase(pyauto.PyUITest): |
55 """CNS test base hadles startup and teardown of CNS server.""" | 60 """CNS test base hadles startup and teardown of CNS server.""" |
56 | 61 |
57 def __init__(self, *args, **kwargs): | 62 def __init__(self, *args, **kwargs): |
58 """Initialize CNSTestBase by setting the arguments for CNS server. | 63 """Initialize CNSTestBase by setting the arguments for CNS server. |
59 | 64 |
60 Args: | 65 Args: |
61 Check cns.py command line argument list for details. | 66 Check cns.py command line argument list for details. |
62 """ | 67 """ |
63 self._port = kwargs.get('port', _CNS_PORT) | 68 self._port = kwargs.get('port', _CNS_PORT) |
64 self._interface = kwargs.get('interface', 'lo') | 69 self._interface = kwargs.get('interface', 'lo') |
65 self._www_root = kwargs.get('www_root', _ROOT_PATH) | 70 self._www_root = kwargs.get('www_root', _ROOT_PATH) |
66 self._verbose = kwargs.get('verbose', True) | 71 self._verbose = kwargs.get('verbose', True) |
67 self._expiry_time = kwargs.get('expiry_time', 0) | 72 self._expiry_time = kwargs.get('expiry_time', 0) |
68 self._socket_timeout = kwargs.get('socket_timeout') | 73 self._socket_timeout = kwargs.get('socket_timeout') |
69 pyauto.PyUITest.__init__(self, *args, **kwargs) | 74 pyauto.PyUITest.__init__(self, *args, **kwargs) |
70 | 75 |
71 def setUp(self): | 76 def setUp(self): |
72 """Starts the Constrained Network Server (CNS).""" | 77 """Ensures the Constrained Network Server (CNS) server is up and running.""" |
| 78 if WINDOWS: |
| 79 self._SetUpWin() |
| 80 else: |
| 81 self._SetUpLinux() |
| 82 |
| 83 def _SetUpWin(self): |
| 84 """Ensures the test can connect to the external CNS server.""" |
| 85 if self.WaitUntil(self._CanAccessServer, retry_sleep=3, timeout=30, |
| 86 debug=False): |
| 87 pyauto.PyUITest.setUp(self) |
| 88 else: |
| 89 self.fail('Failed to connect to CNS.') |
| 90 |
| 91 def _SetUpLinux(self): |
| 92 """Starts the CNS server locally.""" |
73 cmd = [sys.executable, os.path.join(pyauto_paths.GetSourceDir(), _CNS_PATH), | 93 cmd = [sys.executable, os.path.join(pyauto_paths.GetSourceDir(), _CNS_PATH), |
74 '--port', str(self._port), | 94 '--port', str(self._port), |
75 '--interface', self._interface, | 95 '--interface', self._interface, |
76 '--www-root', self._www_root, | 96 '--www-root', self._www_root, |
77 '--expiry-time', str(self._expiry_time)] | 97 '--expiry-time', str(self._expiry_time)] |
78 | 98 |
79 if self._socket_timeout: | 99 if self._socket_timeout: |
80 cmd.extend(['--socket-timeout', str(self._socket_timeout)]) | 100 cmd.extend(['--socket-timeout', str(self._socket_timeout)]) |
81 if self._verbose: | 101 if self._verbose: |
82 cmd.append('-v') | 102 cmd.append('-v') |
(...skipping 13 matching lines...) Expand all Loading... |
96 """Checks if the CNS server can serve a file with no network constraints.""" | 116 """Checks if the CNS server can serve a file with no network constraints.""" |
97 test_url = ''.join([CNS_BASE_URL, 'f=', _TEST_VIDEO]) | 117 test_url = ''.join([CNS_BASE_URL, 'f=', _TEST_VIDEO]) |
98 try: | 118 try: |
99 return urllib2.urlopen(test_url) is not None | 119 return urllib2.urlopen(test_url) is not None |
100 except Exception: | 120 except Exception: |
101 return False | 121 return False |
102 | 122 |
103 def tearDown(self): | 123 def tearDown(self): |
104 """Stops the Constrained Network Server (CNS).""" | 124 """Stops the Constrained Network Server (CNS).""" |
105 pyauto.PyUITest.tearDown(self) | 125 pyauto.PyUITest.tearDown(self) |
106 logging.debug('Stopping CNS server.') | 126 if not WINDOWS: |
107 # Do not use process.kill(), it will not clean up cns. | 127 logging.debug('Stopping CNS server.') |
108 self.Kill(self._cns_process.pid) | 128 # Do not use process.kill(), it will not clean up cns. |
109 # Need to wait since the process logger has a lock on the process stderr. | 129 self.Kill(self._cns_process.pid) |
110 self._cns_process.wait() | 130 # Need to wait since the process logger has a lock on the process stderr. |
111 self.assertFalse(self._cns_process.returncode is None) | 131 self._cns_process.wait() |
112 logging.debug('CNS server stopped.') | 132 self.assertFalse(self._cns_process.returncode is None) |
| 133 logging.debug('CNS server stopped.') |
113 | 134 |
114 | 135 |
115 class ProcessLogger(threading.Thread): | 136 class ProcessLogger(threading.Thread): |
116 """A thread to log a process's stderr output.""" | 137 """A thread to log a process's stderr output.""" |
117 | 138 |
118 def __init__(self, process): | 139 def __init__(self, process): |
119 """Starts the process logger thread. | 140 """Starts the process logger thread. |
120 | 141 |
121 Args: | 142 Args: |
122 process: The process to log. | 143 process: The process to log. |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 """ | 182 """ |
162 # Convert relative test path into an absolute path. | 183 # Convert relative test path into an absolute path. |
163 tasks = Queue.Queue() | 184 tasks = Queue.Queue() |
164 for file_name in test_media_files: | 185 for file_name in test_media_files: |
165 for series_name, settings in network_constraints_settings: | 186 for series_name, settings in network_constraints_settings: |
166 logging.debug('Add test: %s\tSettings: %s\tMedia: %s', series_name, | 187 logging.debug('Add test: %s\tSettings: %s\tMedia: %s', series_name, |
167 settings, file_name) | 188 settings, file_name) |
168 tasks.put((series_name, settings, file_name)) | 189 tasks.put((series_name, settings, file_name)) |
169 | 190 |
170 return tasks | 191 return tasks |
OLD | NEW |