OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2017 The Chromium Authors. All rights reserved. | 2 # Copyright 2017 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import contextlib | 6 import contextlib |
7 import copy | 7 import copy |
8 import difflib | 8 import difflib |
9 import glob | 9 import glob |
10 import itertools | 10 import itertools |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 else: | 114 else: |
115 args += ['--no-source-paths'] | 115 args += ['--no-source-paths'] |
116 if use_elf: | 116 if use_elf: |
117 args += ['--elf-file', _TEST_ELF_PATH] | 117 args += ['--elf-file', _TEST_ELF_PATH] |
118 _RunApp('archive', args, debug_measures=debug_measures) | 118 _RunApp('archive', args, debug_measures=debug_measures) |
119 size_info = archive.LoadAndPostProcessSizeInfo(temp_file.name) | 119 size_info = archive.LoadAndPostProcessSizeInfo(temp_file.name) |
120 # Check that saving & loading is the same as directly parsing the .map. | 120 # Check that saving & loading is the same as directly parsing the .map. |
121 expected_size_info = self._CloneSizeInfo( | 121 expected_size_info = self._CloneSizeInfo( |
122 use_output_directory=use_output_directory, use_elf=use_elf) | 122 use_output_directory=use_output_directory, use_elf=use_elf) |
123 self.assertEquals(expected_size_info.metadata, size_info.metadata) | 123 self.assertEquals(expected_size_info.metadata, size_info.metadata) |
124 expected = list(describe.GenerateLines(expected_size_info.Cluster())) | 124 expected = list(describe.GenerateLines(expected_size_info.Clustered())) |
125 actual = list(describe.GenerateLines(size_info.Cluster())) | 125 actual = list(describe.GenerateLines(size_info.Clustered())) |
126 self.assertEquals(expected, actual) | 126 self.assertEquals(expected, actual) |
127 | 127 |
128 sym_strs = (repr(sym) for sym in size_info.symbols) | 128 sym_strs = (repr(sym) for sym in size_info.symbols) |
129 stats = describe.DescribeSizeInfoCoverage(size_info) | 129 stats = describe.DescribeSizeInfoCoverage(size_info) |
130 if size_info.metadata: | 130 if size_info.metadata: |
131 metadata = describe.DescribeMetadata(size_info.metadata) | 131 metadata = describe.DescribeMetadata(size_info.metadata) |
132 else: | 132 else: |
133 metadata = [] | 133 metadata = [] |
134 return itertools.chain(metadata, stats, sym_strs) | 134 return itertools.chain(metadata, stats, sym_strs) |
135 | 135 |
(...skipping 16 matching lines...) Expand all Loading... |
152 @_CompareWithGolden() | 152 @_CompareWithGolden() |
153 def test_Console(self): | 153 def test_Console(self): |
154 with tempfile.NamedTemporaryFile(suffix='.size') as size_file, \ | 154 with tempfile.NamedTemporaryFile(suffix='.size') as size_file, \ |
155 tempfile.NamedTemporaryFile(suffix='.txt') as output_file: | 155 tempfile.NamedTemporaryFile(suffix='.txt') as output_file: |
156 file_format.SaveSizeInfo(self._CloneSizeInfo(), size_file.name) | 156 file_format.SaveSizeInfo(self._CloneSizeInfo(), size_file.name) |
157 query = [ | 157 query = [ |
158 'ShowExamples()', | 158 'ShowExamples()', |
159 'ExpandRegex("_foo_")', | 159 'ExpandRegex("_foo_")', |
160 'canned_queries.CategorizeGenerated()', | 160 'canned_queries.CategorizeGenerated()', |
161 'canned_queries.CategorizeByChromeComponent()', | 161 'canned_queries.CategorizeByChromeComponent()', |
| 162 'canned_queries.TemplatesByName()', |
162 'Print(size_info, to_file=%r)' % output_file.name, | 163 'Print(size_info, to_file=%r)' % output_file.name, |
163 ] | 164 ] |
164 ret = _RunApp('console', [size_file.name, '--query', '; '.join(query)]) | 165 ret = _RunApp('console', [size_file.name, '--query', '; '.join(query)]) |
165 with open(output_file.name) as f: | 166 with open(output_file.name) as f: |
166 ret.extend(l.rstrip() for l in f) | 167 ret.extend(l.rstrip() for l in f) |
167 return ret | 168 return ret |
168 | 169 |
169 @_CompareWithGolden() | 170 @_CompareWithGolden() |
170 def test_Diff_NullDiff(self): | 171 def test_Diff_NullDiff(self): |
171 with tempfile.NamedTemporaryFile(suffix='.size') as temp_file: | 172 with tempfile.NamedTemporaryFile(suffix='.size') as temp_file: |
172 file_format.SaveSizeInfo(self._CloneSizeInfo(), temp_file.name) | 173 file_format.SaveSizeInfo(self._CloneSizeInfo(), temp_file.name) |
173 return _RunApp('diff', [temp_file.name, temp_file.name]) | 174 return _RunApp('diff', [temp_file.name, temp_file.name]) |
174 | 175 |
175 @_CompareWithGolden() | 176 @_CompareWithGolden() |
176 def test_Diff_Basic(self): | 177 def test_Diff_Basic(self): |
177 size_info1 = self._CloneSizeInfo(use_elf=False) | 178 size_info1 = self._CloneSizeInfo(use_elf=False) |
178 size_info2 = self._CloneSizeInfo(use_elf=False) | 179 size_info2 = self._CloneSizeInfo(use_elf=False) |
179 size_info1.metadata = {"foo": 1, "bar": [1,2,3], "baz": "yes"} | 180 size_info1.metadata = {"foo": 1, "bar": [1,2,3], "baz": "yes"} |
180 size_info2.metadata = {"foo": 1, "bar": [1,3], "baz": "yes"} | 181 size_info2.metadata = {"foo": 1, "bar": [1,3], "baz": "yes"} |
181 size_info1.symbols -= size_info1.symbols[:2] | 182 size_info1.symbols -= size_info1.symbols[:2] |
182 size_info2.symbols -= size_info2.symbols[-3:] | 183 size_info2.symbols -= size_info2.symbols[-3:] |
183 size_info1.symbols[1].size -= 10 | 184 size_info1.symbols[1].size -= 10 |
184 d = diff.Diff(size_info1, size_info2) | 185 d = diff.Diff(size_info1, size_info2) |
185 d.symbols = d.symbols.Cluster().Sorted() | 186 d.symbols = d.symbols.Clustered().Sorted() |
186 return describe.GenerateLines(d, verbose=True) | 187 return describe.GenerateLines(d, verbose=True) |
187 | 188 |
188 def test_Diff_Aliases1(self): | 189 def test_Diff_Aliases1(self): |
189 size_info1 = self._CloneSizeInfo() | 190 size_info1 = self._CloneSizeInfo() |
190 size_info2 = self._CloneSizeInfo() | 191 size_info2 = self._CloneSizeInfo() |
191 | 192 |
192 # Removing 1 alias should not change the size. | 193 # Removing 1 alias should not change the size. |
193 a1, _, _ = ( | 194 a1, _, _ = ( |
194 size_info2.symbols.Filter(lambda s: s.num_aliases == 3)[0].aliases) | 195 size_info2.symbols.Filter(lambda s: s.num_aliases == 3)[0].aliases) |
195 size_info2.symbols -= [a1] | 196 size_info2.symbols -= [a1] |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 models.Symbol(S, 55, name='.L__bar_1195', object_path='b'), # 5 | 252 models.Symbol(S, 55, name='.L__bar_1195', object_path='b'), # 5 |
252 ] | 253 ] |
253 size_info2.symbols += [ | 254 size_info2.symbols += [ |
254 models.Symbol(S, 33, name='.L__unnamed_2195', object_path='b'), # 3 | 255 models.Symbol(S, 33, name='.L__unnamed_2195', object_path='b'), # 3 |
255 models.Symbol(S, 11, name='.L__unnamed_2194', object_path='a'), # 1 | 256 models.Symbol(S, 11, name='.L__unnamed_2194', object_path='a'), # 1 |
256 models.Symbol(S, 22, name='.L__unnamed_2193', object_path='a'), # 2 | 257 models.Symbol(S, 22, name='.L__unnamed_2193', object_path='a'), # 2 |
257 models.Symbol(S, 44, name='.L__bar_2195', object_path='b'), # 4 | 258 models.Symbol(S, 44, name='.L__bar_2195', object_path='b'), # 4 |
258 models.Symbol(S, 55, name='.L__bar_295', object_path='b'), # 5 | 259 models.Symbol(S, 55, name='.L__bar_295', object_path='b'), # 5 |
259 ] | 260 ] |
260 d = diff.Diff(size_info1, size_info2) | 261 d = diff.Diff(size_info1, size_info2) |
261 d.symbols = d.symbols.Cluster().Sorted() | 262 d.symbols = d.symbols.Clustered().Sorted() |
262 self.assertEquals(d.symbols.added_count, 0) | 263 self.assertEquals(d.symbols.added_count, 0) |
263 self.assertEquals(d.symbols.size, 0) | 264 self.assertEquals(d.symbols.size, 0) |
264 | 265 |
265 | 266 |
266 @_CompareWithGolden() | 267 @_CompareWithGolden() |
267 def test_FullDescription(self): | 268 def test_FullDescription(self): |
268 return describe.GenerateLines(self._CloneSizeInfo().Cluster(), | 269 return describe.GenerateLines(self._CloneSizeInfo().Clustered(), |
269 recursive=True, verbose=True) | 270 recursive=True, verbose=True) |
270 | 271 |
271 @_CompareWithGolden() | 272 @_CompareWithGolden() |
272 def test_SymbolGroupMethods(self): | 273 def test_SymbolGroupMethods(self): |
273 all_syms = self._CloneSizeInfo().symbols | 274 all_syms = self._CloneSizeInfo().symbols |
274 global_syms = all_syms.WhereNameMatches('GLOBAL') | 275 global_syms = all_syms.WhereNameMatches('GLOBAL') |
275 # Tests Filter(), Inverted(), and __sub__(). | 276 # Tests Filter(), Inverted(), and __sub__(). |
276 non_global_syms = global_syms.Inverted() | 277 non_global_syms = global_syms.Inverted() |
277 self.assertEqual(non_global_syms, (all_syms - global_syms)) | 278 self.assertEqual(non_global_syms, (all_syms - global_syms)) |
278 # Tests Sorted() and __add__(). | 279 # Tests Sorted() and __add__(). |
279 self.assertEqual(all_syms.Sorted(), | 280 self.assertEqual(all_syms.Sorted(), |
280 (global_syms + non_global_syms).Sorted()) | 281 (global_syms + non_global_syms).Sorted()) |
281 # Tests GroupByNamespace() and __len__(). | 282 # Tests GroupedByName() and __len__(). |
282 return itertools.chain( | 283 return itertools.chain( |
283 ['GroupByNamespace()'], | 284 ['GroupedByName()'], |
284 describe.GenerateLines(all_syms.GroupByNamespace()), | 285 describe.GenerateLines(all_syms.GroupedByName()), |
285 ['GroupByNamespace(depth=1)'], | 286 ['GroupedByName(depth=1)'], |
286 describe.GenerateLines(all_syms.GroupByNamespace(depth=1)), | 287 describe.GenerateLines(all_syms.GroupedByName(depth=1)), |
287 ['GroupByNamespace(depth=1, fallback=None)'], | 288 ['GroupedByName(depth=-1)'], |
288 describe.GenerateLines(all_syms.GroupByNamespace(depth=1, | 289 describe.GenerateLines(all_syms.GroupedByName(depth=-1)), |
289 fallback=None)), | 290 ['GroupedByName(depth=1, min_count=2)'], |
290 ['GroupByNamespace(depth=1, min_count=2)'], | 291 describe.GenerateLines(all_syms.GroupedByName(depth=1, min_count=2)), |
291 describe.GenerateLines(all_syms.GroupByNamespace(depth=1, min_count=2)), | |
292 ) | 292 ) |
293 | 293 |
294 | 294 |
295 def main(): | 295 def main(): |
296 argv = sys.argv | 296 argv = sys.argv |
297 if len(argv) > 1 and argv[1] == '--update': | 297 if len(argv) > 1 and argv[1] == '--update': |
298 argv.pop(0) | 298 argv.pop(0) |
299 global update_goldens | 299 global update_goldens |
300 update_goldens = True | 300 update_goldens = True |
301 for f in glob.glob(os.path.join(_TEST_DATA_DIR, '*.golden')): | 301 for f in glob.glob(os.path.join(_TEST_DATA_DIR, '*.golden')): |
302 os.unlink(f) | 302 os.unlink(f) |
303 | 303 |
304 unittest.main(argv=argv, verbosity=2) | 304 unittest.main(argv=argv, verbosity=2) |
305 | 305 |
306 | 306 |
307 if __name__ == '__main__': | 307 if __name__ == '__main__': |
308 main() | 308 main() |
OLD | NEW |