OLD | NEW |
1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 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 os | 5 import os |
6 import sys | 6 import sys |
7 import unittest | 7 import unittest |
8 | 8 |
9 import dag | 9 import dag |
10 import loading_model | 10 import loading_model |
11 import request_track | 11 import request_track |
12 import request_dependencies_lens | 12 import request_dependencies_lens |
13 import test_utils | 13 import test_utils |
14 | 14 |
15 | 15 |
16 class SimpleLens(object): | |
17 def __init__(self, trace): | |
18 self._trace = trace | |
19 | |
20 def GetRequestDependencies(self): | |
21 url_to_rq = {} | |
22 deps = [] | |
23 for rq in self._trace.request_track.GetEvents(): | |
24 assert rq.url not in url_to_rq | |
25 url_to_rq[rq.url] = rq | |
26 for rq in self._trace.request_track.GetEvents(): | |
27 initiating_url = rq.initiator['url'] | |
28 if initiating_url in url_to_rq: | |
29 deps.append((url_to_rq[initiating_url], rq, rq.initiator['type'])) | |
30 return deps | |
31 | |
32 | |
33 class LoadingModelTestCase(unittest.TestCase): | 16 class LoadingModelTestCase(unittest.TestCase): |
34 | |
35 def setUp(self): | |
36 self.old_lens = request_dependencies_lens.RequestDependencyLens | |
37 request_dependencies_lens.RequestDependencyLens = SimpleLens | |
38 | |
39 def tearDown(self): | |
40 request_dependencies_lens.RequestDependencyLens = self.old_lens | |
41 | |
42 def MakeGraph(self, requests): | |
43 return loading_model.ResourceGraph( | |
44 test_utils.LoadingTraceFromEvents(requests)) | |
45 | |
46 def SortedIndicies(self, graph): | 17 def SortedIndicies(self, graph): |
47 return [n.Index() for n in dag.TopologicalSort(graph._nodes)] | 18 return [n.Index() for n in dag.TopologicalSort(graph._nodes)] |
48 | 19 |
49 def SuccessorIndicies(self, node): | 20 def SuccessorIndicies(self, node): |
50 return [c.Index() for c in node.SortedSuccessors()] | 21 return [c.Index() for c in node.SortedSuccessors()] |
51 | 22 |
52 def test_DictConstruction(self): | 23 def test_DictConstruction(self): |
53 graph = loading_model.ResourceGraph( | 24 graph = test_utils.TestResourceGraph( |
54 {'request_track': { | 25 {'request_track': { |
55 'events': [ | 26 'events': [ |
56 test_utils.MakeRequest(0, 'null', 100, 100.5, 101).ToJsonDict(), | 27 test_utils.MakeRequest(0, 'null', 100, 100.5, 101).ToJsonDict(), |
57 test_utils.MakeRequest(1, 0, 102, 102.5, 103).ToJsonDict(), | 28 test_utils.MakeRequest(1, 0, 102, 102.5, 103).ToJsonDict(), |
58 test_utils.MakeRequest(2, 0, 102, 102.5, 103).ToJsonDict(), | 29 test_utils.MakeRequest(2, 0, 102, 102.5, 103).ToJsonDict(), |
59 test_utils.MakeRequest(3, 2, 104, 114.5, 105).ToJsonDict()], | 30 test_utils.MakeRequest(3, 2, 104, 114.5, 105).ToJsonDict()], |
60 'metadata': { | 31 'metadata': { |
61 request_track.RequestTrack._DUPLICATES_KEY: 0, | 32 request_track.RequestTrack._DUPLICATES_KEY: 0, |
62 request_track.RequestTrack._INCONSISTENT_INITIATORS_KEY: 0}}, | 33 request_track.RequestTrack._INCONSISTENT_INITIATORS_KEY: 0}}, |
63 'url': 'foo.com', | 34 'url': 'foo.com', |
64 'tracing_track': {'events': []}, | 35 'tracing_track': {'events': []}, |
65 'page_track': {'events': []}, | 36 'page_track': {'events': []}, |
66 'metadata': {}}) | 37 'metadata': {}}) |
67 self.assertEqual(self.SuccessorIndicies(graph._nodes[0]), [1, 2]) | 38 self.assertEqual(self.SuccessorIndicies(graph._nodes[0]), [1, 2]) |
68 self.assertEqual(self.SuccessorIndicies(graph._nodes[1]), []) | 39 self.assertEqual(self.SuccessorIndicies(graph._nodes[1]), []) |
69 self.assertEqual(self.SuccessorIndicies(graph._nodes[2]), [3]) | 40 self.assertEqual(self.SuccessorIndicies(graph._nodes[2]), [3]) |
70 self.assertEqual(self.SuccessorIndicies(graph._nodes[3]), []) | 41 self.assertEqual(self.SuccessorIndicies(graph._nodes[3]), []) |
71 | 42 |
72 def test_Costing(self): | 43 def test_Costing(self): |
73 requests = [test_utils.MakeRequest(0, 'null', 100, 105, 110), | 44 requests = [test_utils.MakeRequest(0, 'null', 100, 105, 110), |
74 test_utils.MakeRequest(1, 0, 115, 117, 120), | 45 test_utils.MakeRequest(1, 0, 115, 117, 120), |
75 test_utils.MakeRequest(2, 0, 112, 116, 120), | 46 test_utils.MakeRequest(2, 0, 112, 116, 120), |
76 test_utils.MakeRequest(3, 1, 122, 124, 126), | 47 test_utils.MakeRequest(3, 1, 122, 124, 126), |
77 test_utils.MakeRequest(4, 3, 127, 127.5, 128), | 48 test_utils.MakeRequest(4, 3, 127, 127.5, 128), |
78 test_utils.MakeRequest(5, 'null', 100, 103, 105), | 49 test_utils.MakeRequest(5, 'null', 100, 103, 105), |
79 test_utils.MakeRequest(6, 5, 105, 107, 110)] | 50 test_utils.MakeRequest(6, 5, 105, 107, 110)] |
80 graph = self.MakeGraph(requests) | 51 graph = test_utils.TestResourceGraph.FromRequestList(requests) |
81 self.assertEqual(self.SuccessorIndicies(graph._nodes[0]), [1, 2]) | 52 self.assertEqual(self.SuccessorIndicies(graph._nodes[0]), [1, 2]) |
82 self.assertEqual(self.SuccessorIndicies(graph._nodes[1]), [3]) | 53 self.assertEqual(self.SuccessorIndicies(graph._nodes[1]), [3]) |
83 self.assertEqual(self.SuccessorIndicies(graph._nodes[2]), []) | 54 self.assertEqual(self.SuccessorIndicies(graph._nodes[2]), []) |
84 self.assertEqual(self.SuccessorIndicies(graph._nodes[3]), [4]) | 55 self.assertEqual(self.SuccessorIndicies(graph._nodes[3]), [4]) |
85 self.assertEqual(self.SuccessorIndicies(graph._nodes[4]), []) | 56 self.assertEqual(self.SuccessorIndicies(graph._nodes[4]), []) |
86 self.assertEqual(self.SuccessorIndicies(graph._nodes[5]), [6]) | 57 self.assertEqual(self.SuccessorIndicies(graph._nodes[5]), [6]) |
87 self.assertEqual(self.SuccessorIndicies(graph._nodes[6]), []) | 58 self.assertEqual(self.SuccessorIndicies(graph._nodes[6]), []) |
88 self.assertEqual(self.SortedIndicies(graph), [0, 5, 1, 2, 6, 3, 4]) | 59 self.assertEqual(self.SortedIndicies(graph), [0, 5, 1, 2, 6, 3, 4]) |
89 self.assertEqual(28, graph.Cost()) | 60 self.assertEqual(28, graph.Cost()) |
90 graph.Set(cache_all=True) | 61 graph.Set(cache_all=True) |
91 self.assertEqual(8, graph.Cost()) | 62 self.assertEqual(8, graph.Cost()) |
92 | 63 |
93 def test_MaxPath(self): | 64 def test_MaxPath(self): |
94 requests = [test_utils.MakeRequest(0, 'null', 100, 110, 111), | 65 requests = [test_utils.MakeRequest(0, 'null', 100, 110, 111), |
95 test_utils.MakeRequest(1, 0, 115, 120, 121), | 66 test_utils.MakeRequest(1, 0, 115, 120, 121), |
96 test_utils.MakeRequest(2, 0, 112, 120, 121), | 67 test_utils.MakeRequest(2, 0, 112, 120, 121), |
97 test_utils.MakeRequest(3, 1, 122, 126, 127), | 68 test_utils.MakeRequest(3, 1, 122, 126, 127), |
98 test_utils.MakeRequest(4, 3, 127, 128, 129), | 69 test_utils.MakeRequest(4, 3, 127, 128, 129), |
99 test_utils.MakeRequest(5, 'null', 100, 105, 106), | 70 test_utils.MakeRequest(5, 'null', 100, 105, 106), |
100 test_utils.MakeRequest(6, 5, 105, 110, 111)] | 71 test_utils.MakeRequest(6, 5, 105, 110, 111)] |
101 graph = self.MakeGraph(requests) | 72 graph = test_utils.TestResourceGraph.FromRequestList(requests) |
102 path_list = [] | 73 path_list = [] |
103 self.assertEqual(29, graph.Cost(path_list)) | 74 self.assertEqual(29, graph.Cost(path_list)) |
104 self.assertEqual([0, 1, 3, 4], [n.Index() for n in path_list]) | 75 self.assertEqual([0, 1, 3, 4], [n.Index() for n in path_list]) |
105 | 76 |
106 # More interesting would be a test when a node has multiple predecessors, | 77 # More interesting would be a test when a node has multiple predecessors, |
107 # but it's not possible for us to construct such a graph from requests yet. | 78 # but it's not possible for us to construct such a graph from requests yet. |
108 | 79 |
109 def test_TimingSplit(self): | 80 def test_TimingSplit(self): |
110 # Timing adds node 1 as a parent to 2 but not 3. | 81 # Timing adds node 1 as a parent to 2 but not 3. |
111 requests = [ | 82 requests = [ |
112 test_utils.MakeRequest(0, 'null', 100, 110, 110, | 83 test_utils.MakeRequest(0, 'null', 100, 110, 110, |
113 magic_content_type=True), | 84 magic_content_type=True), |
114 test_utils.MakeRequest(1, 0, 115, 120, 120, | 85 test_utils.MakeRequest(1, 0, 115, 120, 120, |
115 magic_content_type=True), | 86 magic_content_type=True), |
116 test_utils.MakeRequest(2, 0, 121, 122, 122, | 87 test_utils.MakeRequest(2, 0, 121, 122, 122, |
117 magic_content_type=True), | 88 magic_content_type=True), |
118 test_utils.MakeRequest(3, 0, 112, 119, 119, | 89 test_utils.MakeRequest(3, 0, 112, 119, 119, |
119 magic_content_type=True), | 90 magic_content_type=True), |
120 test_utils.MakeRequest(4, 2, 122, 126, 126), | 91 test_utils.MakeRequest(4, 2, 122, 126, 126), |
121 test_utils.MakeRequest(5, 2, 122, 126, 126)] | 92 test_utils.MakeRequest(5, 2, 122, 126, 126)] |
122 graph = self.MakeGraph(requests) | 93 graph = test_utils.TestResourceGraph.FromRequestList(requests) |
123 self.assertEqual(self.SuccessorIndicies(graph._nodes[0]), [1, 3]) | 94 self.assertEqual(self.SuccessorIndicies(graph._nodes[0]), [1, 3]) |
124 self.assertEqual(self.SuccessorIndicies(graph._nodes[1]), [2]) | 95 self.assertEqual(self.SuccessorIndicies(graph._nodes[1]), [2]) |
125 self.assertEqual(self.SuccessorIndicies(graph._nodes[2]), [4, 5]) | 96 self.assertEqual(self.SuccessorIndicies(graph._nodes[2]), [4, 5]) |
126 self.assertEqual(self.SuccessorIndicies(graph._nodes[3]), []) | 97 self.assertEqual(self.SuccessorIndicies(graph._nodes[3]), []) |
127 self.assertEqual(self.SuccessorIndicies(graph._nodes[4]), []) | 98 self.assertEqual(self.SuccessorIndicies(graph._nodes[4]), []) |
128 self.assertEqual(self.SuccessorIndicies(graph._nodes[5]), []) | 99 self.assertEqual(self.SuccessorIndicies(graph._nodes[5]), []) |
129 self.assertEqual(self.SortedIndicies(graph), [0, 1, 3, 2, 4, 5]) | 100 self.assertEqual(self.SortedIndicies(graph), [0, 1, 3, 2, 4, 5]) |
130 | 101 |
131 # Change node 1 so it is a parent of 3, which becomes the parent of 2. | 102 # Change node 1 so it is a parent of 3, which becomes the parent of 2. |
132 requests[1] = test_utils.MakeRequest( | 103 requests[1] = test_utils.MakeRequest( |
133 1, 0, 110, 111, 111, magic_content_type=True) | 104 1, 0, 110, 111, 111, magic_content_type=True) |
134 graph = self.MakeGraph(requests) | 105 graph = test_utils.TestResourceGraph.FromRequestList(requests) |
135 self.assertEqual(self.SuccessorIndicies(graph._nodes[0]), [1]) | 106 self.assertEqual(self.SuccessorIndicies(graph._nodes[0]), [1]) |
136 self.assertEqual(self.SuccessorIndicies(graph._nodes[1]), [3]) | 107 self.assertEqual(self.SuccessorIndicies(graph._nodes[1]), [3]) |
137 self.assertEqual(self.SuccessorIndicies(graph._nodes[2]), [4, 5]) | 108 self.assertEqual(self.SuccessorIndicies(graph._nodes[2]), [4, 5]) |
138 self.assertEqual(self.SuccessorIndicies(graph._nodes[3]), [2]) | 109 self.assertEqual(self.SuccessorIndicies(graph._nodes[3]), [2]) |
139 self.assertEqual(self.SuccessorIndicies(graph._nodes[4]), []) | 110 self.assertEqual(self.SuccessorIndicies(graph._nodes[4]), []) |
140 self.assertEqual(self.SuccessorIndicies(graph._nodes[5]), []) | 111 self.assertEqual(self.SuccessorIndicies(graph._nodes[5]), []) |
141 self.assertEqual(self.SortedIndicies(graph), [0, 1, 3, 2, 4, 5]) | 112 self.assertEqual(self.SortedIndicies(graph), [0, 1, 3, 2, 4, 5]) |
142 | 113 |
143 # Add an initiator dependence to 1 that will become the parent of 3. | 114 # Add an initiator dependence to 1 that will become the parent of 3. |
144 requests[1] = test_utils.MakeRequest( | 115 requests[1] = test_utils.MakeRequest( |
145 1, 0, 110, 111, 111, magic_content_type=True) | 116 1, 0, 110, 111, 111, magic_content_type=True) |
146 requests.append(test_utils.MakeRequest(6, 1, 111, 112, 112)) | 117 requests.append(test_utils.MakeRequest(6, 1, 111, 112, 112)) |
147 graph = self.MakeGraph(requests) | 118 graph = test_utils.TestResourceGraph.FromRequestList(requests) |
148 # Check it doesn't change until we change the content type of 6. | 119 # Check it doesn't change until we change the content type of 6. |
149 self.assertEqual(self.SuccessorIndicies(graph._nodes[6]), []) | 120 self.assertEqual(self.SuccessorIndicies(graph._nodes[6]), []) |
150 requests[6] = test_utils.MakeRequest(6, 1, 111, 112, 112, | 121 requests[6] = test_utils.MakeRequest(6, 1, 111, 112, 112, |
151 magic_content_type=True) | 122 magic_content_type=True) |
152 graph = self.MakeGraph(requests) | 123 graph = test_utils.TestResourceGraph.FromRequestList(requests) |
153 self.assertEqual(self.SuccessorIndicies(graph._nodes[0]), [1]) | 124 self.assertEqual(self.SuccessorIndicies(graph._nodes[0]), [1]) |
154 self.assertEqual(self.SuccessorIndicies(graph._nodes[1]), [6]) | 125 self.assertEqual(self.SuccessorIndicies(graph._nodes[1]), [6]) |
155 self.assertEqual(self.SuccessorIndicies(graph._nodes[2]), [4, 5]) | 126 self.assertEqual(self.SuccessorIndicies(graph._nodes[2]), [4, 5]) |
156 self.assertEqual(self.SuccessorIndicies(graph._nodes[3]), [2]) | 127 self.assertEqual(self.SuccessorIndicies(graph._nodes[3]), [2]) |
157 self.assertEqual(self.SuccessorIndicies(graph._nodes[4]), []) | 128 self.assertEqual(self.SuccessorIndicies(graph._nodes[4]), []) |
158 self.assertEqual(self.SuccessorIndicies(graph._nodes[5]), []) | 129 self.assertEqual(self.SuccessorIndicies(graph._nodes[5]), []) |
159 self.assertEqual(self.SuccessorIndicies(graph._nodes[6]), [3]) | 130 self.assertEqual(self.SuccessorIndicies(graph._nodes[6]), [3]) |
160 self.assertEqual(self.SortedIndicies(graph), [0, 1, 6, 3, 2, 4, 5]) | 131 self.assertEqual(self.SortedIndicies(graph), [0, 1, 6, 3, 2, 4, 5]) |
161 | 132 |
162 def test_TimingSplitImage(self): | 133 def test_TimingSplitImage(self): |
163 # If we're all image types, then we shouldn't split by timing. | 134 # If we're all image types, then we shouldn't split by timing. |
164 requests = [test_utils.MakeRequest(0, 'null', 100, 110, 110), | 135 requests = [test_utils.MakeRequest(0, 'null', 100, 110, 110), |
165 test_utils.MakeRequest(1, 0, 115, 120, 120), | 136 test_utils.MakeRequest(1, 0, 115, 120, 120), |
166 test_utils.MakeRequest(2, 0, 121, 122, 122), | 137 test_utils.MakeRequest(2, 0, 121, 122, 122), |
167 test_utils.MakeRequest(3, 0, 112, 119, 119), | 138 test_utils.MakeRequest(3, 0, 112, 119, 119), |
168 test_utils.MakeRequest(4, 2, 122, 126, 126), | 139 test_utils.MakeRequest(4, 2, 122, 126, 126), |
169 test_utils.MakeRequest(5, 2, 122, 126, 126)] | 140 test_utils.MakeRequest(5, 2, 122, 126, 126)] |
170 for r in requests: | 141 for r in requests: |
171 r.response_headers['Content-Type'] = 'image/gif' | 142 r.response_headers['Content-Type'] = 'image/gif' |
172 graph = self.MakeGraph(requests) | 143 graph = test_utils.TestResourceGraph.FromRequestList(requests) |
173 self.assertEqual(self.SuccessorIndicies(graph._nodes[0]), [1, 2, 3]) | 144 self.assertEqual(self.SuccessorIndicies(graph._nodes[0]), [1, 2, 3]) |
174 self.assertEqual(self.SuccessorIndicies(graph._nodes[1]), []) | 145 self.assertEqual(self.SuccessorIndicies(graph._nodes[1]), []) |
175 self.assertEqual(self.SuccessorIndicies(graph._nodes[2]), [4, 5]) | 146 self.assertEqual(self.SuccessorIndicies(graph._nodes[2]), [4, 5]) |
176 self.assertEqual(self.SuccessorIndicies(graph._nodes[3]), []) | 147 self.assertEqual(self.SuccessorIndicies(graph._nodes[3]), []) |
177 self.assertEqual(self.SuccessorIndicies(graph._nodes[4]), []) | 148 self.assertEqual(self.SuccessorIndicies(graph._nodes[4]), []) |
178 self.assertEqual(self.SuccessorIndicies(graph._nodes[5]), []) | 149 self.assertEqual(self.SuccessorIndicies(graph._nodes[5]), []) |
179 self.assertEqual(self.SortedIndicies(graph), [0, 1, 2, 3, 4, 5]) | 150 self.assertEqual(self.SortedIndicies(graph), [0, 1, 2, 3, 4, 5]) |
180 | 151 |
181 | 152 |
182 if __name__ == '__main__': | 153 if __name__ == '__main__': |
183 unittest.main() | 154 unittest.main() |
OLD | NEW |