Add ICU message format support
[chromium-blink-merge.git] / tools / chrome_proxy / common / network_metrics_unittest.py
blobfe1e48f035ffee0d7306fd600fb1a386549937c9
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
3 # found in the LICENSE file.
5 import base64
6 import unittest
8 from common import network_metrics
9 from telemetry.testing import test_page_test_results
10 from telemetry.timeline import event
13 HTML_BODY = """<!DOCTYPE HTML>
14 <html>
15 <head> </head>
16 <body>
17 <div id="test"> TEST HTML</div>
18 </body>
19 </html>"""
20 IMAGE_BODY = """fake image data"""
21 GZIPPED_HTML_LEN = network_metrics.HTTPResponse.GetGizppedBodyLength(HTML_BODY)
22 # Make up original content length for the image.
23 IMAGE_OCL = 3 * len(IMAGE_BODY)
26 class NetworkMetricTest(unittest.TestCase):
27 @staticmethod
28 def MakeNetworkTimelineEvent(
29 url, response_headers, body=None, base64_encoded_body=False,
30 served_from_cache=False, request_headers=None, status=200,
31 remote_port=None):
32 if not request_headers:
33 request_headers = {}
34 e = event.TimelineEvent('network', 'HTTPResponse', 0, 0)
35 e.args = {}
36 e.args['requestId'] = 0
37 e.args['response'] = {
38 'status': status,
39 'url': url,
40 'headers': response_headers,
41 'requestHeaders': request_headers,
42 'remotePort': remote_port,
44 e.args['body'] = body
45 e.args['base64_encoded_body'] = base64_encoded_body
46 e.args['served_from_cache'] = served_from_cache
47 return e
49 def testHTTPResponse(self):
50 url = 'http://test.url'
51 self.assertLess(GZIPPED_HTML_LEN, len(HTML_BODY))
53 # A plain text HTML response
54 resp = network_metrics.HTTPResponse(self.MakeNetworkTimelineEvent(
55 url=url,
56 response_headers={
57 'Content-Type': 'text/html',
58 'Content-Length': str(len(HTML_BODY)),
60 body=HTML_BODY))
61 self.assertEqual(url, resp.response.url)
62 body, base64_encoded = resp.response.GetBody()
63 self.assertEqual(HTML_BODY, body)
64 self.assertFalse(base64_encoded)
65 self.assertEqual('text/html', resp.response.GetHeader('Content-Type'))
67 self.assertEqual(len(HTML_BODY), resp.content_length)
68 self.assertEqual(None, resp.response.GetHeader('Content-Encoding'))
69 self.assertFalse(resp.has_original_content_length)
70 self.assertEqual(0.0, resp.data_saving_rate)
72 # A gzipped HTML response
73 resp = network_metrics.HTTPResponse(self.MakeNetworkTimelineEvent(
74 url=url,
75 response_headers={
76 'Content-Type': 'text/html',
77 'Content-Encoding': 'gzip',
78 'X-Original-Content-Length': str(len(HTML_BODY)),
80 body=HTML_BODY))
81 body, base64_encoded = resp.response.GetBody()
82 self.assertFalse(base64_encoded)
83 self.assertEqual(GZIPPED_HTML_LEN, resp.content_length)
84 self.assertEqual('gzip', resp.response.GetHeader('Content-Encoding'))
85 self.assertTrue(resp.has_original_content_length)
86 self.assertEqual(len(HTML_BODY), resp.original_content_length)
87 self.assertEqual(
88 float(len(HTML_BODY) - GZIPPED_HTML_LEN) / len(HTML_BODY),
89 resp.data_saving_rate)
91 # A JPEG image response.
92 resp = network_metrics.HTTPResponse(self.MakeNetworkTimelineEvent(
93 url='http://test.image',
94 response_headers={
95 'Content-Type': 'image/jpeg',
96 'Content-Encoding': 'gzip',
97 'X-Original-Content-Length': str(IMAGE_OCL),
99 body=base64.b64encode(IMAGE_BODY),
100 base64_encoded_body=True))
101 body, base64_encoded = resp.response.GetBody()
102 self.assertTrue(base64_encoded)
103 self.assertEqual(IMAGE_BODY, base64.b64decode(body))
104 self.assertEqual(len(IMAGE_BODY), resp.content_length)
105 self.assertTrue(resp.has_original_content_length)
106 self.assertEqual(IMAGE_OCL, resp.original_content_length)
107 self.assertFalse(resp.response.served_from_cache)
108 self.assertEqual(float(IMAGE_OCL - len(IMAGE_BODY)) / IMAGE_OCL,
109 resp.data_saving_rate)
111 # A JPEG image response from cache.
112 resp = network_metrics.HTTPResponse(self.MakeNetworkTimelineEvent(
113 url='http://test.image',
114 response_headers={
115 'Content-Type': 'image/jpeg',
116 'Content-Encoding': 'gzip',
117 'X-Original-Content-Length': str(IMAGE_OCL),
119 body=base64.b64encode(IMAGE_BODY),
120 base64_encoded_body=True,
121 served_from_cache=True))
122 self.assertEqual(len(IMAGE_BODY), resp.content_length)
123 self.assertTrue(resp.has_original_content_length)
124 self.assertEqual(IMAGE_OCL, resp.original_content_length)
125 # Cached resource has zero saving.
126 self.assertTrue(resp.response.served_from_cache)
127 self.assertEqual(0.0, resp.data_saving_rate)
129 def testNetworkMetricResults(self):
130 events = [
131 # A plain text HTML.
132 self.MakeNetworkTimelineEvent(
133 url='http://test.html1',
134 response_headers={
135 'Content-Type': 'text/html',
136 'Content-Length': str(len(HTML_BODY)),
138 body=HTML_BODY),
139 # A compressed HTML.
140 self.MakeNetworkTimelineEvent(
141 url='http://test.html2',
142 response_headers={
143 'Content-Type': 'text/html',
144 'Content-Encoding': 'gzip',
145 'X-Original-Content-Length': str(len(HTML_BODY)),
147 body=HTML_BODY),
148 # A base64 encoded image.
149 self.MakeNetworkTimelineEvent(
150 url='http://test.image',
151 response_headers={
152 'Content-Type': 'image/jpeg',
153 'Content-Encoding': 'gzip',
154 'X-Original-Content-Length': str(IMAGE_OCL),
156 body=base64.b64encode(IMAGE_BODY),
157 base64_encoded_body=True),
159 metric = network_metrics.NetworkMetric()
160 metric._events = events
161 metric.compute_data_saving = True
163 self.assertTrue(len(events), len(list(metric.IterResponses(None))))
164 results = test_page_test_results.TestPageTestResults(self)
165 metric.AddResults(None, results)
167 cl = len(HTML_BODY) + GZIPPED_HTML_LEN + len(IMAGE_BODY)
168 results.AssertHasPageSpecificScalarValue('content_length', 'bytes', cl)
170 ocl = len(HTML_BODY) + len(HTML_BODY) + IMAGE_OCL
171 results.AssertHasPageSpecificScalarValue(
172 'original_content_length', 'bytes', ocl)
174 saving_percent = float(ocl - cl) * 100/ ocl
175 results.AssertHasPageSpecificScalarValue(
176 'data_saving', 'percent', saving_percent)