def testMapperDoesntAddValues(self): events = [{ 'pid': 1, 'tid': 2, 'ph': 'X', 'name': 'a', 'cat': 'c', 'ts': 0, 'dur': 10, 'args': {} }, { 'pid': 1, 'tid': 2, 'ph': 'X', 'name': 'b', 'cat': 'c', 'ts': 3, 'dur': 5, 'args': {} }] trace_handle = file_handle.InMemoryFileHandle('/a.json', json.dumps(events)) with map_single_trace.TemporaryMapScript(""" pi.FunctionRegistry.register( function MyMapFunction(results, model) { }); """) as map_script: result = map_single_trace.MapSingleTrace( trace_handle, _Handle(map_script.filename)) self.assertEquals(len(result.failures), 1) self.assertEquals(len(result.pairs), 0) f = result.failures[0] self.assertIsInstance(f, map_single_trace.NoResultsAddedFailure)
def testNoMapper(self): events = [{ 'pid': 1, 'tid': 2, 'ph': 'X', 'name': 'a', 'cat': 'c', 'ts': 0, 'dur': 10, 'args': {} }, { 'pid': 1, 'tid': 2, 'ph': 'X', 'name': 'b', 'cat': 'c', 'ts': 3, 'dur': 5, 'args': {} }] trace_handle = file_handle.InMemoryFileHandle('/a.json', json.dumps(events)) with map_single_trace.TemporaryMapScript(""" """) as map_script: result = map_single_trace.MapSingleTrace( trace_handle, _Handle(map_script.filename)) self.assertEquals(len(result.failures), 1) self.assertEquals(len(result.pairs), 0) f = result.failures[0] self.assertIsInstance(f, map_single_trace.FunctionNotDefinedFailure)
def testMapperWithUnexpectedError(self): events = [ { 'pid': 1, 'tid': 2, 'ph': 'X', 'name': 'a', 'cat': 'c', 'ts': 0, 'dur': 10, 'args': {} }, ] trace_handle = file_handle.InMemoryFileHandle('/a.json', json.dumps(events)) with map_single_trace.TemporaryMapScript(""" quit(100); """) as map_script: result = map_single_trace.MapSingleTrace( trace_handle, _Handle(map_script.filename)) self.assertEquals(len(result.failures), 1) self.assertEquals(len(result.pairs), 0) f = result.failures[0] self.assertIsInstance(f, failure.Failure)
def _ProcessOneTrace(self, trace_handle): canonical_url = trace_handle.canonical_url run_reporter = self._progress_reporter.WillRun(canonical_url) result = map_single_trace.MapSingleTrace( trace_handle, self._job, extra_import_options=self._extra_import_options) had_failure = len(result.failures) > 0 for f in result.failures: run_reporter.DidAddFailure(f) run_reporter.DidRun(had_failure) self._wq.PostMainThreadTask( self._MergeResultIntoMaster, result, trace_handle)
def testTraceDidntImport(self): trace_string = 'This is intentionally not a trace-formatted string.' trace_handle = file_handle.InMemoryFileHandle('/a.json', trace_string) with map_single_trace.TemporaryMapScript(""" pi.FunctionRegistry.register( function MyMapFunction(results, model) { }); """) as map_script: result = map_single_trace.MapSingleTrace( trace_handle, _Handle(map_script.filename)) self.assertEquals(len(result.failures), 1) self.assertEquals(len(result.pairs), 0) f = result.failures[0] self.assertIsInstance(f, map_single_trace.TraceImportFailure)
def testPassingMapScript(self): events = [{ 'pid': 1, 'tid': 2, 'ph': 'X', 'name': 'a', 'cat': 'c', 'ts': 0, 'dur': 10, 'args': {} }, { 'pid': 1, 'tid': 2, 'ph': 'X', 'name': 'b', 'cat': 'c', 'ts': 3, 'dur': 5, 'args': {} }] trace_handle = file_handle.InMemoryFileHandle('/a.json', json.dumps(events)) with map_single_trace.TemporaryMapScript(""" pi.FunctionRegistry.register( function MyMapFunction(result, model) { var canonicalUrl = model.canonicalUrl; result.addPair('result', { numProcesses: model.getAllProcesses().length }); }); """) as map_script: result = map_single_trace.MapSingleTrace( trace_handle, _Handle(map_script.filename)) self.assertFalse(result.failures) r = result.pairs['result'] self.assertEquals(r['numProcesses'], 1)