def MapSingleTrace(trace_handle, job, extra_import_options=None): assert (type(extra_import_options) is types.NoneType or type(extra_import_options) is types.DictType), ( 'extra_import_options should be a dict or None.') project = perf_insights_project.PerfInsightsProject() all_source_paths = list(project.source_paths) all_source_paths.append(project.perf_insights_root_path) result = mre_result.MreResult() with trace_handle.PrepareFileForProcessing() as prepared_trace_handle: js_args = [ json.dumps(prepared_trace_handle.AsDict()), json.dumps(job.AsDict()), ] if extra_import_options: js_args.append(json.dumps(extra_import_options)) res = vinn.RunFile(_MAP_SINGLE_TRACE_CMDLINE_PATH, source_paths=all_source_paths, js_args=js_args) if res.returncode != 0: try: sys.stderr.write(res.stdout) except Exception: pass result.AddFailure( failure.Failure(job, trace_handle.canonical_url, 'Error', 'vinn runtime error while mapping trace.', 'vinn runtime error while mapping trace.', 'Unknown stack')) return result for line in res.stdout.split('\n'): m = re.match('^MRE_RESULT: (.+)', line, re.DOTALL) if m: found_dict = json.loads(m.group(1)) failures = [ failure.Failure.FromDict(f, job, _FAILURE_NAME_TO_FAILURE_CONSTRUCTOR) for f in found_dict['failures'] ] for f in failures: result.AddFailure(f) for k, v in found_dict['pairs'].iteritems(): result.AddPair(k, v) else: if len(line) > 0: sys.stderr.write(line) sys.stderr.write('\n') if not (len(result.pairs) or len(result.failures)): raise InternalMapError('Internal error: No results were produced!') return result
def PiReportToHTML(ofile, corpus_driver, pi_report_file, query, json_output=False, stop_on_error=False, jobs=1, quiet=False): project = perf_insights_project.PerfInsightsProject() with open(pi_report_file, 'r') as f: pi_report_file_contents = f.read() map_function_href, map_function_name, pi_report_element_name = ( _GetMapFunctionHrefFromPiReport(pi_report_file_contents)) map_file = project.GetAbsPathFromHRef(map_function_href) module = function_handle.ModuleToLoad(filename=map_file) map_function_handle = function_handle.FunctionHandle([module], map_function_name) job = job_module.Job(map_function_handle, None) if map_file == None: raise Exception('Could not find %s' % map_function_href) results = _MapTraces(corpus_driver, job, query, stop_on_error, jobs, quiet) if stop_on_error and results.had_failures: sys.stderr.write('There were mapping errors. Aborting.') return 255 if json_output: json.dump([result.AsDict() for result in results], ofile, indent=2) else: WriteResultsToFile(ofile, project, pi_report_file, pi_report_element_name, results) return 0
def Main(argv): parser = argparse.ArgumentParser( description='Local weather report generator') parser.add_argument('trace_directory') parser.add_argument('-j', '--jobs', type=int, default=1) parser.add_argument('-o', '--output-file') parser.add_argument('--json', action='store_true') parser.add_argument('-s', '--stop-on-error', action='store_true') args = parser.parse_args(argv[1:]) if not args.output_file: parser.error('Must provide -o') if not os.path.exists(args.trace_directory): parser.error('trace_directory does not exist') project = perf_insights_project.PerfInsightsProject() results = MapTracesWithWeatherReport(project, args.trace_directory, stop_on_error=args.stop_on_error, jobs=args.jobs) if args.stop_on_error and results.had_failures: sys.stderr.write('There were mapping errors. Aborting.') return 255 if args.json: with open(args.output_file, 'w') as ofile: json.dump(results.AsDict(), ofile, indent=2) else: with codecs.open(args.output_file, mode='w', encoding='utf-8') as ofile: WriteResultsToFile(ofile, project, results) return 0
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 with trace_handle.Open() as trace_file: 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) 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 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!')
def get(self, *args, **kwargs): # pylint: disable=unused-argument project = perf_insights_project.PerfInsightsProject() test_relpaths = ['/' + _RelPathToUnixPath(x) for x in project.FindAllTestModuleRelPaths()] tests = {'test_relpaths': test_relpaths} tests_as_json = json.dumps(tests) self.response.content_type = 'application/json' return self.response.write(tests_as_json)
def MapSingleTrace(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) result = mre_result.MreResult() with trace_handle.PrepareFileForProcessing() as prepared_trace_handle: js_args = [ json.dumps(prepared_trace_handle.AsDict()), json.dumps(map_function_handle.AsDict()) ] res = vinn.RunFile( os.path.join(pi_path, 'perf_insights', 'map_single_trace_cmdline.html'), source_paths=all_source_paths, js_args=js_args) if res.returncode != 0: try: sys.stderr.write(res.stdout) except Exception: pass result.AddFailure(failure.Failure( map_function_handle.AsUserFriendlyString(), trace_handle.canonical_url, 'Error', 'vinn runtime error while mapping trace.', 'vinn runtime error while mapping trace.', 'Unknown stack')) return for line in res.stdout.split('\n'): m = re.match('^MRE_RESULT: (.+)', line, re.DOTALL) if m: found_dict = json.loads(m.group(1)) failures = [failure.Failure.FromDict( f, _FAILURE_NAME_TO_FAILURE_CONSTRUCTOR) for f in found_dict['failures']] for f in failures: result.AddFailure(f) for k, v in found_dict['pairs'].iteritems(): result.AddPair(k, v) else: if len(line) > 0: sys.stderr.write(line) sys.stderr.write('\n') if not (len(result.pairs) or len(result.failures)): raise InternalMapError('Internal error: No results were produced!') return result
def ReduceMapResults(job_results, key, file_handle, job): project = perf_insights_project.PerfInsightsProject() all_source_paths = list(project.source_paths) all_source_paths.append(project.perf_insights_root_path) with file_handle.PrepareFileForProcessing() as prepared_file_handle: js_args = [ key, json.dumps(prepared_file_handle.AsDict()), json.dumps(job.AsDict()), ] res = vinn.RunFile(_REDUCE_MAP_RESULTS_CMDLINE_PATH, source_paths=all_source_paths, js_args=js_args) if res.returncode != 0: try: sys.stderr.write(res.stdout) except Exception: pass job_results.AddFailure( failure.Failure(job, job.map_function_handle, None, 'Error', 'vinn runtime error while reducing results.', 'Unknown stack')) return for line in res.stdout.split('\n'): m = re.match('^JOB_(RESULTS|FAILURE): (.+)', line, re.DOTALL) if m: found_type = m.group(1) found_dict = json.loads(m.group(2)) if found_type == 'FAILURE': try: sys.stderr.write(res.stdout) except Exception: pass job_results.AddFailure( failure.Failure( job, job.map_function_handle, None, 'Error', 'vinn runtime error while reducing results.', 'Unknown stack')) elif found_type == 'RESULTS': job_results.AddPair(key, found_dict[key]) else: if len(line) > 0: sys.stderr.write(line) sys.stderr.write('\n') if len(job_results.pairs) == 0 and len(job_results.failures) == 0: raise map_single_trace.InternalMapError( 'Internal error: No results were produced!')
def RunTests(): project = perf_insights_project.PerfInsightsProject() headless_test_module_filenames = [ '/' + _RelPathToUnixPath(x) for x in project.FindAllD8TestModuleRelPaths()] headless_test_module_filenames.sort() cmd = """ HTMLImportsLoader.loadHTML('/tracing/base/headless_tests.html'); tr.b.unittest.loadAndRunTests(sys.argv.slice(1)); """ res = vinn.RunJsString( cmd, source_paths=list(project.source_paths), js_args=headless_test_module_filenames, stdout=sys.stdout, stdin=sys.stdin) return res.returncode
def RunTests(): project = perf_insights_project.PerfInsightsProject() d8_test_module_filenames = [ '/' + _RelPathToUnixPath(x) for x in project.FindAllD8TestModuleRelPaths() ] d8_test_module_filenames.sort() cmd = """ loadHTML('/tracing/base/d8_tests.html'); """ res = vinn.RunJsString(cmd, source_paths=list(project.source_paths), js_args=d8_test_module_filenames, stdout=sys.stdout, stdin=sys.stdin) return res.returncode
def test_basic(self): # Note: We can't use "with" when working with tempfile.NamedTemporaryFile as # that does not work on Windows. We use the longer, more clunky version # instead. See https://bugs.python.org/issue14243 for detials. raw_tmpfile = tempfile.NamedTemporaryFile(mode='w', suffix='.html', delete=False) raw_tmpfile.close() try: project = perf_insights_project.PerfInsightsProject() with codecs.open(raw_tmpfile.name, 'w', encoding='utf-8') as tmpfile: res = pi_report_to_html.PiReportToHTML( tmpfile, project.perf_insights_test_data_path, project.GetAbsPathFromHRef( '/perf_insights/ui/reports/weather_report.html'), corpus_query.CorpusQuery.FromString('MAX_TRACE_HANDLES=2'), quiet=True) self.assertEquals(res, 0) finally: os.remove(raw_tmpfile.name)
def __init__(self): self.project = perf_insights_project.PerfInsightsProject()