OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 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 import base64 | 5 import base64 |
6 import gzip | 6 import gzip |
7 import hashlib | 7 import hashlib |
8 import io | 8 import io |
9 import logging | 9 import logging |
10 import zlib | 10 import zlib |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 return len(zlib.compress(body, 9)) | 91 return len(zlib.compress(body, 9)) |
92 else: | 92 else: |
93 raise NetworkMetricException, ( | 93 raise NetworkMetricException, ( |
94 'Unknown Content-Encoding %s for %s' % (encoding, resp.url)) | 94 'Unknown Content-Encoding %s for %s' % (encoding, resp.url)) |
95 | 95 |
96 def GetContentLength(self): | 96 def GetContentLength(self): |
97 cl = 0 | 97 cl = 0 |
98 try: | 98 try: |
99 cl = self.GetContentLengthFromBody() | 99 cl = self.GetContentLengthFromBody() |
100 except Exception, e: | 100 except Exception, e: |
| 101 logging.warning('Fail to get content length for %s from body: %s', |
| 102 self.response.url[:100], e) |
| 103 if cl == 0: |
101 resp = self.response | 104 resp = self.response |
102 logging.warning('Fail to get content length for %s from body: %s', | |
103 resp.url[:100], e) | |
104 cl_header = resp.GetHeader('Content-Length') | 105 cl_header = resp.GetHeader('Content-Length') |
105 if cl_header: | 106 if cl_header: |
106 cl = int(cl_header) | 107 cl = int(cl_header) |
107 else: | 108 else: |
108 body, _ = resp.GetBody() | 109 body, _ = resp.GetBody() |
109 if body: | 110 if body: |
110 cl = len(body) | 111 cl = len(body) |
111 return cl | 112 return cl |
112 | 113 |
113 @staticmethod | 114 @staticmethod |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 if resp.response.served_from_cache: | 164 if resp.response.served_from_cache: |
164 continue | 165 continue |
165 | 166 |
166 resource = resp.response.url | 167 resource = resp.response.url |
167 resource_signature = resp.url_signature | 168 resource_signature = resp.url_signature |
168 cl = resp.content_length | 169 cl = resp.content_length |
169 if resp.has_original_content_length: | 170 if resp.has_original_content_length: |
170 ocl = resp.original_content_length | 171 ocl = resp.original_content_length |
171 if ocl < cl: | 172 if ocl < cl: |
172 logging.warning('original content length (%d) is less than content ' | 173 logging.warning('original content length (%d) is less than content ' |
173 'lenght(%d) for resource %s', ocl, cl, resource) | 174 'length (%d) for resource %s', ocl, cl, resource) |
174 if self.add_result_for_resource: | 175 if self.add_result_for_resource: |
175 results.AddValue(scalar.ScalarValue( | 176 results.AddValue(scalar.ScalarValue( |
176 results.current_page, | 177 results.current_page, |
177 'resource_data_saving_' + resource_signature, 'percent', | 178 'resource_data_saving_' + resource_signature, 'percent', |
178 resp.data_saving_rate * 100)) | 179 resp.data_saving_rate * 100)) |
179 results.AddValue(scalar.ScalarValue( | 180 results.AddValue(scalar.ScalarValue( |
180 results.current_page, | 181 results.current_page, |
181 'resource_original_content_length_' + resource_signature, 'bytes', | 182 'resource_original_content_length_' + resource_signature, 'bytes', |
182 ocl)) | 183 ocl)) |
183 original_content_length += ocl | 184 original_content_length += ocl |
(...skipping 13 matching lines...) Expand all Loading... |
197 if self.compute_data_saving: | 198 if self.compute_data_saving: |
198 if (original_content_length > 0 and | 199 if (original_content_length > 0 and |
199 original_content_length >= content_length): | 200 original_content_length >= content_length): |
200 saving = (float(original_content_length-content_length) * 100 / | 201 saving = (float(original_content_length-content_length) * 100 / |
201 original_content_length) | 202 original_content_length) |
202 results.AddValue(scalar.ScalarValue( | 203 results.AddValue(scalar.ScalarValue( |
203 results.current_page, 'data_saving', 'percent', saving)) | 204 results.current_page, 'data_saving', 'percent', saving)) |
204 else: | 205 else: |
205 results.AddValue(scalar.ScalarValue( | 206 results.AddValue(scalar.ScalarValue( |
206 results.current_page, 'data_saving', 'percent', 0.0)) | 207 results.current_page, 'data_saving', 'percent', 0.0)) |
OLD | NEW |