Index: perf_insights/perf_insights/map_single_trace.py |
diff --git a/perf_insights/perf_insights/map_single_trace.py b/perf_insights/perf_insights/map_single_trace.py |
index 8ac71f2c46309acc8659fc065ab7b237bdb65396..e7ef3a494d6085d05593c2a3c639757e28d17d26 100644 |
--- a/perf_insights/perf_insights/map_single_trace.py |
+++ b/perf_insights/perf_insights/map_single_trace.py |
@@ -1,6 +1,7 @@ |
# Copyright (c) 2015 The Chromium Authors. All rights reserved. |
# Use of this source code is governed by a BSD-style license that can be |
# found in the LICENSE file. |
+import codecs |
import json |
import os |
import re |
@@ -10,6 +11,8 @@ import traceback |
from perf_insights import value as value_module |
+ |
+from py_vulcanize import generate |
import perf_insights_project |
import vinn |
@@ -64,71 +67,100 @@ _FAILURE_NAME_TO_FAILURE_CONSTRUCTOR = { |
'NoResultsAddedError': NoResultsAddedErrorValue |
} |
-def MapSingleTrace(results, trace_handle, map_function_handle): |
- project = perf_insights_project.PerfInsightsProject() |
- |
- all_source_paths = list(project.source_paths) |
- |
- pi_path = os.path.abspath(os.path.join(os.path.dirname(__file__), |
- '..')) |
- all_source_paths.append(pi_path) |
- run_info = trace_handle.run_info |
- |
- trace_file = trace_handle.Open() |
- if not trace_file: |
- results.AddValue(value_module.FailureValue( |
- run_info, |
- 'Error', 'error while opening trace', |
- 'error while opening trace', 'Unknown stack')) |
- return |
- |
- try: |
- js_args = [ |
- json.dumps(run_info.AsDict()), |
- json.dumps(map_function_handle.AsDict()), |
- os.path.abspath(trace_file.name), |
- json.dumps(run_info.metadata) |
- ] |
- |
- res = vinn.RunFile( |
- os.path.join(pi_path, 'perf_insights', 'map_single_trace_cmdline.html'), |
- source_paths=all_source_paths, |
- js_args=js_args) |
- finally: |
- trace_file.close() |
- |
- if res.returncode != 0: |
+class SingleTraceMapper(object): |
+ def __init__(self, vulcanize=True): |
+ self._vulcanize = vulcanize |
+ self._vulcanized_map_single_trace = None |
+ if vulcanize: |
+ self._vulcanized_map_single_trace = self._VulcanizeMapSingleTrace() |
+ |
+ def CleanUp(self): |
+ if self._vulcanized_map_single_trace: |
+ os.remove(self._vulcanized_map_single_trace) |
+ |
+ def _VulcanizeMapSingleTrace(self): |
+ project = perf_insights_project.PerfInsightsProject() |
+ vulcanizer = project.CreateVulcanizer() |
+ module_names = ['perf_insights.map_single_trace_cmdline'] |
+ load_sequence = vulcanizer.CalcLoadSequenceForModuleNames( |
+ module_names) |
+ output_filename = 'vulcanized_map_single_trace.js' |
+ with codecs.open(output_filename, 'w', encoding='utf-8') as f: |
+ generate.GenerateJSToFile(f, load_sequence=load_sequence, minify=False, |
+ load_polymer_script=False) |
+ return os.path.abspath(output_filename) |
+ |
+ def MapSingleTrace(self, results, trace_handle, map_function_handle): |
+ project = perf_insights_project.PerfInsightsProject() |
+ |
+ all_source_paths = list(project.source_paths) |
+ |
+ pi_path = os.path.abspath(os.path.join(os.path.dirname(__file__), |
+ '..')) |
+ all_source_paths.append(pi_path) |
+ run_info = trace_handle.run_info |
+ |
+ trace_file = trace_handle.Open() |
+ if not trace_file: |
+ results.AddValue(value_module.FailureValue( |
+ run_info, |
+ 'Error', 'error while opening trace', |
+ 'error while opening trace', 'Unknown stack')) |
+ return |
+ |
try: |
- sys.stderr.write(res.stdout) |
- except Exception: |
- pass |
- results.AddValue(value_module.FailureValue( |
- run_info, |
- 'Error', 'vinn runtime error while mapping trace.', |
- 'vinn runtime error while mapping trace.', 'Unknown stack')) |
- return |
- |
- |
- found_at_least_one_result=False |
- for line in res.stdout.split('\n'): |
- m = re.match('^MAP_RESULT_VALUE: (.+)', line, re.DOTALL) |
- if m: |
- found_dict = json.loads(m.group(1)) |
- if found_dict['type'] == 'failure': |
- cls = _FAILURE_NAME_TO_FAILURE_CONSTRUCTOR.get(found_dict['name'], None) |
- if not cls: |
- cls = value_module.FailureValue |
+ js_args = [ |
+ json.dumps(run_info.AsDict()), |
+ json.dumps(map_function_handle.AsDict()), |
+ os.path.abspath(trace_file.name), |
+ json.dumps(run_info.metadata) |
+ ] |
+ if self._vulcanize: |
+ res = vinn.RunFile( |
+ self._vulcanized_map_single_trace, source_paths=all_source_paths, |
+ js_args=js_args) |
else: |
- cls = value_module.Value |
- found_value = cls.FromDict(run_info, found_dict) |
+ res = vinn.RunFile( |
+ os.path.join(pi_path, 'perf_insights', |
+ 'map_single_trace_cmdline.html'), |
+ source_paths=all_source_paths, |
+ js_args=js_args) |
+ finally: |
+ trace_file.close() |
+ |
+ if res.returncode != 0: |
+ try: |
+ sys.stderr.write(res.stdout) |
+ except Exception: |
+ pass |
+ results.AddValue(value_module.FailureValue( |
+ run_info, |
+ 'Error', 'vinn runtime error while mapping trace.', |
+ 'vinn runtime error while mapping trace.', 'Unknown stack')) |
+ return |
+ |
+ |
+ found_at_least_one_result=False |
+ for line in res.stdout.split('\n'): |
+ m = re.match('^MAP_RESULT_VALUE: (.+)', line, re.DOTALL) |
+ if m: |
+ found_dict = json.loads(m.group(1)) |
+ if found_dict['type'] == 'failure': |
+ cls = _FAILURE_NAME_TO_FAILURE_CONSTRUCTOR.get( |
+ found_dict['name'], None) |
+ if not cls: |
+ cls = value_module.FailureValue |
+ else: |
+ cls = value_module.Value |
+ found_value = cls.FromDict(run_info, found_dict) |
+ |
+ results.AddValue(found_value) |
+ found_at_least_one_result = True |
- results.AddValue(found_value) |
- found_at_least_one_result = True |
- |
- else: |
- if len(line) > 0: |
- sys.stderr.write(line) |
- sys.stderr.write('\n') |
+ else: |
+ if len(line) > 0: |
+ sys.stderr.write(line) |
+ sys.stderr.write('\n') |
- if found_at_least_one_result == False: |
- raise InternalMapError('Internal error: No results were produced!') |
+ if found_at_least_one_result == False: |
+ raise InternalMapError('Internal error: No results were produced!') |