Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / tools / chrome_proxy / live_tests / chrome_proxy_metrics_unittest.py
blob1bb9c4afb813a69f6b9c879922b823849dbd427e
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 chrome_proxy_metrics as common_metrics
9 from common import network_metrics_unittest as network_unittest
10 from live_tests import chrome_proxy_metrics as metrics
11 from telemetry.testing import test_page_test_results
13 TEST_EXTRA_VIA_HEADER = '1.1 EXTRA_VIA_HEADER'
15 # Timeline events used in tests.
16 # An HTML not via proxy.
17 EVENT_HTML_DIRECT = network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent(
18 url='http://test.html1',
19 response_headers={
20 'Content-Type': 'text/html',
21 'Content-Length': str(len(network_unittest.HTML_BODY)),
23 body=network_unittest.HTML_BODY)
25 # An HTML via proxy.
26 EVENT_HTML_PROXY_VIA = (
27 network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent(
28 url='http://test.html2',
29 response_headers={
30 'Content-Type': 'text/html',
31 'Content-Encoding': 'gzip',
32 'X-Original-Content-Length': str(len(network_unittest.HTML_BODY)),
33 'Via': '1.1 ' + common_metrics.CHROME_PROXY_VIA_HEADER,
35 body=network_unittest.HTML_BODY,
36 remote_port=443))
38 # An image via proxy with Via header.
39 EVENT_IMAGE_PROXY_VIA = (
40 network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent(
41 url='http://test.image',
42 response_headers={
43 'Content-Type': 'image/jpeg',
44 'Content-Encoding': 'gzip',
45 'X-Original-Content-Length': str(network_unittest.IMAGE_OCL),
46 'Via': '1.1 ' + common_metrics.CHROME_PROXY_VIA_HEADER,
48 body=base64.b64encode(network_unittest.IMAGE_BODY),
49 base64_encoded_body=True,
50 remote_port=443))
52 # An image via proxy with Via header and it is cached.
53 EVENT_IMAGE_PROXY_CACHED = (
54 network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent(
55 url='http://test.image',
56 response_headers={
57 'Content-Type': 'image/jpeg',
58 'Content-Encoding': 'gzip',
59 'X-Original-Content-Length': str(network_unittest.IMAGE_OCL),
60 'Via': '1.1 ' + common_metrics.CHROME_PROXY_VIA_HEADER,
62 body=base64.b64encode(network_unittest.IMAGE_BODY),
63 base64_encoded_body=True,
64 served_from_cache=True))
67 # An image fetched directly.
68 EVENT_IMAGE_DIRECT = (
69 network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent(
70 url='http://test.image',
71 response_headers={
72 'Content-Type': 'image/jpeg',
73 'Content-Encoding': 'gzip',
75 body=base64.b64encode(network_unittest.IMAGE_BODY),
76 base64_encoded_body=True))
79 class ChromeProxyMetricTest(unittest.TestCase):
81 def testChromeProxyMetricForDataSaving(self):
82 metric = metrics.ChromeProxyMetric()
83 events = [
84 EVENT_HTML_DIRECT,
85 EVENT_HTML_PROXY_VIA,
86 EVENT_IMAGE_PROXY_CACHED,
87 EVENT_IMAGE_DIRECT]
88 metric.SetEvents(events)
90 self.assertTrue(len(events), len(list(metric.IterResponses(None))))
91 results = test_page_test_results.TestPageTestResults(self)
93 metric.AddResultsForDataSaving(None, results)
94 results.AssertHasPageSpecificScalarValue('resources_via_proxy', 'count', 2)
95 results.AssertHasPageSpecificScalarValue('resources_from_cache', 'count', 1)
96 results.AssertHasPageSpecificScalarValue('resources_direct', 'count', 2)
98 # Passing in zero responses should cause a failure.
99 metric.SetEvents([])
100 no_responses_exception = False
101 try:
102 metric.AddResultsForDataSaving(None, results)
103 except common_metrics.ChromeProxyMetricException:
104 no_responses_exception = True
105 self.assertTrue(no_responses_exception)