def testAsDict(self): result = mre_result.MreResult() with map_single_trace.TemporaryMapScript(""" tr.mre.FunctionRegistry.register( function MyMapFunction(result, model) { var canonicalUrl = model.canonicalUrl; result.addPair('result', { numProcesses: model.getAllProcesses().length }); }); """) as map_script: module = function_handle.ModuleToLoad(filename=map_script.filename) map_handle = function_handle.FunctionHandle( modules_to_load=[module], function_name='MyMapFunction') job = job_module.Job(map_handle, None) failure = failure_module.Failure(job, '2', '3', 'err', 'desc', 'stack') result.AddFailure(failure) result.AddPair('foo', 'bar') result_dict = result.AsDict() self.assertEquals(result_dict['failures'], [failure.AsDict()]) self.assertEquals(result_dict['pairs'], {'foo': 'bar'})
def Main(argv): parser = argparse.ArgumentParser(description='Bulk trace processing') parser.add_argument('--map_function_handle') parser.add_argument('-j', '--jobs', type=int, default=map_runner.AUTO_JOB_COUNT) parser.add_argument('-o', '--output-file') parser.add_argument('-s', '--stop-on-error', action='store_true') if len(sys.argv) == 1: parser.print_help() sys.exit(0) args = parser.parse_args(argv[1:]) corpus_driver = corpus_driver_cmdline.GetCorpusDriver(parser, args) if args.output_file: ofile = open(args.output_file, 'w') else: ofile = sys.stdout output_formatter = json_output_formatter.JSONOutputFormatter(ofile) try: map_handle = None if args.map_function_handle: map_handle = function_handle.FunctionHandle.FromUserFriendlyString( args.map_function_handle) job = job_module.Job(map_handle) except function_handle.UserFriendlyStringInvalidError: error_lines = [ 'The map_traces command-line API has changed! You must now specify the', 'filenames to load and the map function name, separated by :. For ' 'example, a mapper in', 'foo.html called Foo would be written as foo.html:Foo .' ] parser.error('\n'.join(error_lines)) try: trace_handles = corpus_driver.GetTraceHandles() runner = map_runner.MapRunner(trace_handles, job, stop_on_error=args.stop_on_error, jobs=args.jobs, output_formatters=[output_formatter]) results = runner.Run() if not any(result.failures for result in results): return 0 else: return 255 finally: if ofile != sys.stdout: ofile.close()
def testTranslateMreFailure(self): map_function_handle = _SingleFileFunctionHandle('foo.html', 'Foo', '2') job = job_module.Job(map_function_handle, '1') story_set = story.StorySet(base_dir=os.path.dirname(__file__)) p = page.Page('http://www.foo.com/', story_set, story_set.base_dir) f = failure.Failure(job, 'foo', '/a.json', 'MyFailure', 'failure', 'stack') fv = common_value_helpers.TranslateMreFailure(f, p) self.assertIn('stack', str(fv))
def _GetMetricRunnerHandle(metrics): assert isinstance(metrics, list) for metric in metrics: assert isinstance(metric, StringTypes) metrics_dir = _GetMetricsDir() metric_mapper_path = os.path.join(metrics_dir, _METRIC_MAP_FUNCTION_FILENAME) modules_to_load = [function_handle.ModuleToLoad(filename=metric_mapper_path)] options = {'metrics': metrics} map_function_handle = function_handle.FunctionHandle( modules_to_load, _METRIC_MAP_FUNCTION_NAME, options) return job_module.Job(map_function_handle, None)
def testAsDict(self): map_function_handle = _SingleFileFunctionHandle('foo.html', 'Foo', '2') job = job_module.Job(map_function_handle, '1') failure = failure_module.Failure(job, 'foo.html:Foo', 'file://foo.html', 'err', 'desc', 'stack') self.assertEquals( failure.AsDict(), { 'job_guid': '1', 'function_handle_string': 'foo.html:Foo', 'trace_canonical_url': 'file://foo.html', 'type': 'err', 'description': 'desc', 'stack': 'stack' })
def ExecuteTraceMappingCode(trace_file_path, process_trace_func_code, extra_import_options=None, trace_canonical_url=None): """Execute |process_trace_func_code| on the input |trace_file_path|. process_trace_func_code must contain a function named 'process_trace' with signature as follows: function processTrace(results, model) { // call results.addPair(<key>, <value>) to add data to results object. } Whereas results is an instance of tr.mre.MreResult, and model is an instance of tr.model.Model which was resulted from parsing the input trace. Returns: This function returns the dictionay that represents data collected in |results|. Raises: RuntimeError if there is any error with execute trace mapping code. """ with TemporaryMapScript(""" //# sourceURL=processTrace %s; tr.mre.FunctionRegistry.register(processTrace); """ % process_trace_func_code) as map_script: handle = function_handle.FunctionHandle( [function_handle.ModuleToLoad(filename=map_script.filename)], function_name='processTrace') mapping_job = job_module.Job(handle) trace_file_path = os.path.abspath(trace_file_path) if not trace_canonical_url: trace_canonical_url = 'file://%s' % trace_file_path trace_handle = file_handle.URLFileHandle(trace_file_path, trace_canonical_url) results = MapSingleTrace(trace_handle, mapping_job, extra_import_options) if results.failures: raise RuntimeError('Failures mapping trace:\n%s' % ('\n'.join(str(f) for f in results.failures))) return results.pairs
def _ProcessTracesWithMapper(mapper_handle_str, traces, output_file): map_handle = function_handle.FunctionHandle.FromUserFriendlyString( mapper_handle_str) job = job_module.Job(map_handle) trace_handles = [ file_handle.URLFileHandle(f, 'file://%s' % f) for f in traces ] runner = map_runner.MapRunner( trace_handles, job, progress_reporter=progress_reporter.ProgressReporter()) results = runner.RunMapper() results_dict = {k: v.AsDict() for k, v in results.iteritems()} _DumpToOutputJson(results_dict, output_file) return _GetExitCodeForResults(results)
def testFromDict(self): map_function_handle = _SingleFileFunctionHandle('foo.html', 'Foo', '2') job = job_module.Job(map_function_handle, '1') failure_dict = { 'job_guid': '1', 'function_handle_string': 'foo.html:Foo', 'trace_canonical_url': 'file://foo.html', 'type': 'err', 'description': 'desc', 'stack': 'stack' } failure = failure_module.Failure.FromDict(failure_dict, job) self.assertEquals(failure.job.guid, '1') self.assertEquals(failure.function_handle_string, 'foo.html:Foo') self.assertEquals(failure.trace_canonical_url, 'file://foo.html') self.assertEquals(failure.failure_type_name, 'err') self.assertEquals(failure.description, 'desc') self.assertEquals(failure.stack, 'stack')
def testComputeTBMv2MetricsFailure(self): test_result = testing.TestResult( 'benchmark/story1', output_artifacts={ compute_metrics.HTML_TRACE_NAME: testing.Artifact('/trace1.html', 'gs://trace1.html') }, tags=['tbmv2:metric1'], ) test_result['_histograms'] = histogram_set.HistogramSet() metrics_result = mre_result.MreResult() metrics_result.AddFailure(failure.Failure(job.Job(0), 0, 0, 0, 0, 0)) with mock.patch(GETSIZE_METHOD) as getsize_mock: with mock.patch(RUN_METRICS_METHOD) as run_metrics_mock: getsize_mock.return_value = 100 run_metrics_mock.return_value = metrics_result compute_metrics.ComputeTBMv2Metrics(test_result) histogram_dicts = test_result['_histograms'].AsDicts() self.assertEqual(histogram_dicts, []) self.assertEqual(test_result['status'], 'FAIL') self.assertFalse(test_result['expected'])
def testComputeTBMv2MetricsFailure(self): in_results = testing.IntermediateResults([ testing.TestResult( 'benchmark/story1', output_artifacts={ compute_metrics.HTML_TRACE_NAME: testing.Artifact('/trace1.html', 'gs://trace1.html') }, tags=['tbmv2:metric1'], ), ]) metrics_result = mre_result.MreResult() metrics_result.AddFailure(failure.Failure(job.Job(0), 0, 0, 0, 0, 0)) with mock.patch(GETSIZE_METHOD) as getsize_mock: with mock.patch(RUN_METRICS_METHOD) as run_metrics_mock: getsize_mock.return_value = 100 run_metrics_mock.return_value = metrics_result histogram_dicts = compute_metrics.ComputeTBMv2Metrics( in_results) self.assertEqual(histogram_dicts, []) self.assertEqual(in_results['testResults'][0]['status'], 'FAIL')
def _Handle(filename): module = function_handle.ModuleToLoad(filename=filename) map_handle = function_handle.FunctionHandle(modules_to_load=[module], function_name='MyMapFunction') return job_module.Job(map_handle, None)
def testNoDuplicateUUID(self): job0 = job.Job('not none') job1 = job.Job('not none') self.assertNotEqual(job0.guid, job1.guid)