OLD | NEW |
1 # Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 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 import codecs |
4 import json | 5 import json |
5 import os | 6 import os |
6 import re | 7 import re |
7 import sys | 8 import sys |
8 import tempfile | 9 import tempfile |
9 import traceback | 10 import traceback |
10 | 11 |
11 | 12 |
12 from perf_insights import value as value_module | 13 from perf_insights import value as value_module |
| 14 |
| 15 from py_vulcanize import generate |
13 import perf_insights_project | 16 import perf_insights_project |
14 import vinn | 17 import vinn |
15 | 18 |
16 | 19 |
17 class TemporaryMapScript(object): | 20 class TemporaryMapScript(object): |
18 def __init__(self, js): | 21 def __init__(self, js): |
19 self.file = tempfile.NamedTemporaryFile() | 22 self.file = tempfile.NamedTemporaryFile() |
20 self.file.write(""" | 23 self.file.write(""" |
21 <!DOCTYPE html> | 24 <!DOCTYPE html> |
22 <link rel="import" href="/perf_insights/value/value.html"> | 25 <link rel="import" href="/perf_insights/value/value.html"> |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 pass | 60 pass |
58 | 61 |
59 _FAILURE_NAME_TO_FAILURE_CONSTRUCTOR = { | 62 _FAILURE_NAME_TO_FAILURE_CONSTRUCTOR = { |
60 'MapFunctionLoadingError': MapFunctionLoadingErrorValue, | 63 'MapFunctionLoadingError': MapFunctionLoadingErrorValue, |
61 'MapFunctionNotDefinedError': MapFunctionNotDefinedErrorValue, | 64 'MapFunctionNotDefinedError': MapFunctionNotDefinedErrorValue, |
62 'TraceImportError': TraceImportErrorValue, | 65 'TraceImportError': TraceImportErrorValue, |
63 'MapFunctionError': MapFunctionErrorValue, | 66 'MapFunctionError': MapFunctionErrorValue, |
64 'NoResultsAddedError': NoResultsAddedErrorValue | 67 'NoResultsAddedError': NoResultsAddedErrorValue |
65 } | 68 } |
66 | 69 |
67 def MapSingleTrace(results, trace_handle, map_function_handle): | 70 class SingleTraceMapper(object): |
68 project = perf_insights_project.PerfInsightsProject() | 71 def __init__(self, vulcanize=True): |
| 72 self._vulcanize = vulcanize |
| 73 self._vulcanized_map_single_trace = None |
| 74 if vulcanize: |
| 75 self._vulcanized_map_single_trace = self._VulcanizeMapSingleTrace() |
69 | 76 |
70 all_source_paths = list(project.source_paths) | 77 def CleanUp(self): |
| 78 if self._vulcanized_map_single_trace: |
| 79 os.remove(self._vulcanized_map_single_trace) |
71 | 80 |
72 pi_path = os.path.abspath(os.path.join(os.path.dirname(__file__), | 81 def _VulcanizeMapSingleTrace(self): |
73 '..')) | 82 project = perf_insights_project.PerfInsightsProject() |
74 all_source_paths.append(pi_path) | 83 vulcanizer = project.CreateVulcanizer() |
75 run_info = trace_handle.run_info | 84 module_names = ['perf_insights.map_single_trace_cmdline'] |
| 85 load_sequence = vulcanizer.CalcLoadSequenceForModuleNames( |
| 86 module_names) |
| 87 output_filename = 'vulcanized_map_single_trace.js' |
| 88 with codecs.open(output_filename, 'w', encoding='utf-8') as f: |
| 89 generate.GenerateJSToFile(f, load_sequence=load_sequence, minify=False, |
| 90 load_polymer_script=False) |
| 91 return os.path.abspath(output_filename) |
76 | 92 |
77 trace_file = trace_handle.Open() | 93 def MapSingleTrace(self, results, trace_handle, map_function_handle): |
78 if not trace_file: | 94 project = perf_insights_project.PerfInsightsProject() |
79 results.AddValue(value_module.FailureValue( | |
80 run_info, | |
81 'Error', 'error while opening trace', | |
82 'error while opening trace', 'Unknown stack')) | |
83 return | |
84 | 95 |
85 try: | 96 all_source_paths = list(project.source_paths) |
86 js_args = [ | |
87 json.dumps(run_info.AsDict()), | |
88 json.dumps(map_function_handle.AsDict()), | |
89 os.path.abspath(trace_file.name), | |
90 json.dumps(run_info.metadata) | |
91 ] | |
92 | 97 |
93 res = vinn.RunFile( | 98 pi_path = os.path.abspath(os.path.join(os.path.dirname(__file__), |
94 os.path.join(pi_path, 'perf_insights', 'map_single_trace_cmdline.html'), | 99 '..')) |
95 source_paths=all_source_paths, | 100 all_source_paths.append(pi_path) |
96 js_args=js_args) | 101 run_info = trace_handle.run_info |
97 finally: | |
98 trace_file.close() | |
99 | 102 |
100 if res.returncode != 0: | 103 trace_file = trace_handle.Open() |
| 104 if not trace_file: |
| 105 results.AddValue(value_module.FailureValue( |
| 106 run_info, |
| 107 'Error', 'error while opening trace', |
| 108 'error while opening trace', 'Unknown stack')) |
| 109 return |
| 110 |
101 try: | 111 try: |
102 sys.stderr.write(res.stdout) | 112 js_args = [ |
103 except Exception: | 113 json.dumps(run_info.AsDict()), |
104 pass | 114 json.dumps(map_function_handle.AsDict()), |
105 results.AddValue(value_module.FailureValue( | 115 os.path.abspath(trace_file.name), |
106 run_info, | 116 json.dumps(run_info.metadata) |
107 'Error', 'vinn runtime error while mapping trace.', | 117 ] |
108 'vinn runtime error while mapping trace.', 'Unknown stack')) | 118 if self._vulcanize: |
109 return | 119 res = vinn.RunFile( |
| 120 self._vulcanized_map_single_trace, source_paths=all_source_paths, |
| 121 js_args=js_args) |
| 122 else: |
| 123 res = vinn.RunFile( |
| 124 os.path.join(pi_path, 'perf_insights', |
| 125 'map_single_trace_cmdline.html'), |
| 126 source_paths=all_source_paths, |
| 127 js_args=js_args) |
| 128 finally: |
| 129 trace_file.close() |
| 130 |
| 131 if res.returncode != 0: |
| 132 try: |
| 133 sys.stderr.write(res.stdout) |
| 134 except Exception: |
| 135 pass |
| 136 results.AddValue(value_module.FailureValue( |
| 137 run_info, |
| 138 'Error', 'vinn runtime error while mapping trace.', |
| 139 'vinn runtime error while mapping trace.', 'Unknown stack')) |
| 140 return |
110 | 141 |
111 | 142 |
112 found_at_least_one_result=False | 143 found_at_least_one_result=False |
113 for line in res.stdout.split('\n'): | 144 for line in res.stdout.split('\n'): |
114 m = re.match('^MAP_RESULT_VALUE: (.+)', line, re.DOTALL) | 145 m = re.match('^MAP_RESULT_VALUE: (.+)', line, re.DOTALL) |
115 if m: | 146 if m: |
116 found_dict = json.loads(m.group(1)) | 147 found_dict = json.loads(m.group(1)) |
117 if found_dict['type'] == 'failure': | 148 if found_dict['type'] == 'failure': |
118 cls = _FAILURE_NAME_TO_FAILURE_CONSTRUCTOR.get(found_dict['name'], None) | 149 cls = _FAILURE_NAME_TO_FAILURE_CONSTRUCTOR.get( |
119 if not cls: | 150 found_dict['name'], None) |
120 cls = value_module.FailureValue | 151 if not cls: |
| 152 cls = value_module.FailureValue |
| 153 else: |
| 154 cls = value_module.Value |
| 155 found_value = cls.FromDict(run_info, found_dict) |
| 156 |
| 157 results.AddValue(found_value) |
| 158 found_at_least_one_result = True |
| 159 |
121 else: | 160 else: |
122 cls = value_module.Value | 161 if len(line) > 0: |
123 found_value = cls.FromDict(run_info, found_dict) | 162 sys.stderr.write(line) |
| 163 sys.stderr.write('\n') |
124 | 164 |
125 results.AddValue(found_value) | 165 if found_at_least_one_result == False: |
126 found_at_least_one_result = True | 166 raise InternalMapError('Internal error: No results were produced!') |
127 | |
128 else: | |
129 if len(line) > 0: | |
130 sys.stderr.write(line) | |
131 sys.stderr.write('\n') | |
132 | |
133 if found_at_least_one_result == False: | |
134 raise InternalMapError('Internal error: No results were produced!') | |
OLD | NEW |