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 """Handles Chrome's configuration.""" | 5 """Handles Chrome's configuration.""" |
6 | 6 |
7 import contextlib | 7 import contextlib |
8 import json | 8 import json |
9 import shutil | 9 import shutil |
10 import subprocess | 10 import subprocess |
11 import tempfile | 11 import tempfile |
12 import time | 12 import time |
13 | 13 |
14 import devtools_monitor | 14 import devtools_monitor |
15 from options import OPTIONS | 15 from options import OPTIONS |
16 | 16 |
17 | 17 |
18 # Copied from | 18 # Copied from |
19 # WebKit/Source/devtools/front_end/network/NetworkConditionsSelector.js | 19 # WebKit/Source/devtools/front_end/network/NetworkConditionsSelector.js |
20 _NETWORK_CONDITIONS = { | 20 # Units: |
21 'Offline': { | 21 # download/upload: byte/s |
22 'download': 0 * 1024 / 8, 'upload': 0 * 1024 / 8, 'latency': 0}, | 22 # latency: ms |
| 23 NETWORK_CONDITIONS = { |
23 'GPRS': { | 24 'GPRS': { |
24 'download': 50 * 1024 / 8, 'upload': 20 * 1024 / 8, 'latency': 500}, | 25 'download': 50 * 1024 / 8, 'upload': 20 * 1024 / 8, 'latency': 500}, |
25 'Regular 2G': { | 26 'Regular 2G': { |
26 'download': 250 * 1024 / 8, 'upload': 50 * 1024 / 8, 'latency': 300}, | 27 'download': 250 * 1024 / 8, 'upload': 50 * 1024 / 8, 'latency': 300}, |
27 'Good 2G': { | 28 'Good 2G': { |
28 'download': 450 * 1024 / 8, 'upload': 150 * 1024 / 8, 'latency': 150}, | 29 'download': 450 * 1024 / 8, 'upload': 150 * 1024 / 8, 'latency': 150}, |
29 'Regular 3G': { | 30 'Regular 3G': { |
30 'download': 750 * 1024 / 8, 'upload': 250 * 1024 / 8, 'latency': 100}, | 31 'download': 750 * 1024 / 8, 'upload': 250 * 1024 / 8, 'latency': 100}, |
31 'Good 3G': { | 32 'Good 3G': { |
32 'download': 1.5 * 1024 * 1024 / 8, 'upload': 750 * 1024 / 8, | 33 'download': 1.5 * 1024 * 1024 / 8, 'upload': 750 * 1024 / 8, |
33 'latency': 40}, | 34 'latency': 40}, |
34 'Regular 4G': { | 35 'Regular 4G': { |
35 'download': 4 * 1024 * 1024 / 8, 'upload': 3 * 1024 * 1024 / 8, | 36 'download': 4 * 1024 * 1024 / 8, 'upload': 3 * 1024 * 1024 / 8, |
36 'latency': 20}, | 37 'latency': 20}, |
37 'DSL': { | 38 'DSL': { |
38 'download': 2 * 1024 * 1024 / 8, 'upload': 1 * 1024 * 1024 / 8, | 39 'download': 2 * 1024 * 1024 / 8, 'upload': 1 * 1024 * 1024 / 8, |
39 'latency': 5}, | 40 'latency': 5}, |
40 'WiFi': { | 41 'WiFi': { |
41 'download': 30 * 1024 * 1024 / 8, 'upload': 15 * 1024 * 1024 / 8, | 42 'download': 30 * 1024 * 1024 / 8, 'upload': 15 * 1024 * 1024 / 8, |
42 'latency': 2} | 43 'latency': 2} |
43 } | 44 } |
44 | 45 |
45 | 46 |
| 47 def BandwidthToString(bandwidth): |
| 48 """Converts a bandwidth to string. |
| 49 |
| 50 Args: |
| 51 bandwidth: The bandwidth to convert in byte/s. Must be a multiple of 1024/8. |
| 52 |
| 53 Returns: |
| 54 A string compatible with wpr --{up,down} command line flags. |
| 55 """ |
| 56 assert type(bandwidth) == int |
| 57 assert bandwidth % (1024/8) == 0 |
| 58 bandwidth_kbps = (bandwidth * 8) / 1024 |
| 59 if bandwidth_kbps % 1024: |
| 60 return '{}Kbit/s'.format(bandwidth_kbps) |
| 61 return '{}Mbit/s'.format(bandwidth_kbps / 1024) |
| 62 |
| 63 |
46 @contextlib.contextmanager | 64 @contextlib.contextmanager |
47 def DevToolsConnectionForLocalBinary(flags): | 65 def DevToolsConnectionForLocalBinary(flags): |
48 """Returns a DevToolsConnection context manager for a local binary. | 66 """Returns a DevToolsConnection context manager for a local binary. |
49 | 67 |
50 Args: | 68 Args: |
51 flags: ([str]) List of flags to pass to the browser. | 69 flags: ([str]) List of flags to pass to the browser. |
52 | 70 |
53 Returns: | 71 Returns: |
54 A DevToolsConnection context manager. | 72 A DevToolsConnection context manager. |
55 """ | 73 """ |
(...skipping 17 matching lines...) Expand all Loading... |
73 | 91 |
74 | 92 |
75 def SetUpEmulationAndReturnMetadata(connection, emulated_device_name, | 93 def SetUpEmulationAndReturnMetadata(connection, emulated_device_name, |
76 emulated_network_name): | 94 emulated_network_name): |
77 """Sets up the device and network emulation and returns the trace metadata. | 95 """Sets up the device and network emulation and returns the trace metadata. |
78 | 96 |
79 Args: | 97 Args: |
80 connection: (DevToolsConnection) | 98 connection: (DevToolsConnection) |
81 emulated_device_name: (str) Key in the dict returned by | 99 emulated_device_name: (str) Key in the dict returned by |
82 _LoadEmulatedDevices(). | 100 _LoadEmulatedDevices(). |
83 emulated_network_name: (str) Key in _NETWORK_CONDITIONS. | 101 emulated_network_name: (str) Key in NETWORK_CONDITIONS. |
84 | 102 |
85 Returns: | 103 Returns: |
86 A metadata dict {'deviceEmulation': params, 'networkEmulation': params}. | 104 A metadata dict {'deviceEmulation': params, 'networkEmulation': params}. |
87 """ | 105 """ |
88 result = {'deviceEmulation': {}, 'networkEmulation': {}} | 106 result = {'deviceEmulation': {}, 'networkEmulation': {}} |
89 if emulated_device_name: | 107 if emulated_device_name: |
90 devices = _LoadEmulatedDevices(OPTIONS.devices_file) | 108 devices = _LoadEmulatedDevices(OPTIONS.devices_file) |
91 emulated_device = devices[emulated_device_name] | 109 emulated_device = devices[emulated_device_name] |
92 emulation_params = _SetUpDeviceEmulationAndReturnMetadata( | 110 emulation_params = _SetUpDeviceEmulationAndReturnMetadata( |
93 connection, emulated_device) | 111 connection, emulated_device) |
94 result['deviceEmulation'] = emulation_params | 112 result['deviceEmulation'] = emulation_params |
95 if emulated_network_name: | 113 if emulated_network_name: |
96 params = _NETWORK_CONDITIONS[emulated_network_name] | 114 params = NETWORK_CONDITIONS[emulated_network_name] |
97 _SetUpNetworkEmulation( | 115 _SetUpNetworkEmulation( |
98 connection, params['latency'], params['download'], params['upload']) | 116 connection, params['latency'], params['download'], params['upload']) |
99 result['networkEmulation'] = params | 117 result['networkEmulation'] = params |
100 return result | 118 return result |
101 | 119 |
102 | 120 |
103 def _LoadEmulatedDevices(filename): | 121 def _LoadEmulatedDevices(filename): |
104 """Loads a list of emulated devices from the DevTools JSON registry. | 122 """Loads a list of emulated devices from the DevTools JSON registry. |
105 | 123 |
106 Args: | 124 Args: |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 latency: (float) Latency in ms. | 178 latency: (float) Latency in ms. |
161 download: (float) Download speed (Bytes / s). | 179 download: (float) Download speed (Bytes / s). |
162 upload: (float) Upload speed (Bytes / s). | 180 upload: (float) Upload speed (Bytes / s). |
163 """ | 181 """ |
164 res = connection.SyncRequest('Network.canEmulateNetworkConditions') | 182 res = connection.SyncRequest('Network.canEmulateNetworkConditions') |
165 assert res['result'], 'Cannot set network emulation.' | 183 assert res['result'], 'Cannot set network emulation.' |
166 connection.SyncRequestNoResponse( | 184 connection.SyncRequestNoResponse( |
167 'Network.emulateNetworkConditions', | 185 'Network.emulateNetworkConditions', |
168 {'offline': False, 'latency': latency, 'downloadThroughput': download, | 186 {'offline': False, 'latency': latency, 'downloadThroughput': download, |
169 'uploadThroughput': upload}) | 187 'uploadThroughput': upload}) |
OLD | NEW |