def testTraceDidntImport(self):
        trace_string = b'This is intentionally not a trace-formatted string.'
        trace_handle = file_handle.InMemoryFileHandle('/a.json', trace_string)

        with map_single_trace.TemporaryMapScript("""
      tr.mre.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 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 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)
Exemple #4
0
def RunMetricOnSingleTrace(filename,
                           metrics,
                           extra_import_options=None,
                           canonical_url=None,
                           timeout=None):
    """A simplified RunMetric() that skips using MapRunner.

  This avoids the complexity of multithreading and progress
  reporting.
  """
    filename_url = 'file://' + filename
    if canonical_url is None:
        canonical_url = filename_url
    trace_handle = file_handle.URLFileHandle(canonical_url, filename_url)
    job = _GetMetricRunnerHandle(metrics)
    return map_single_trace.MapSingleTrace(
        trace_handle,
        job,
        extra_import_options=extra_import_options,
        timeout=timeout)
    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).encode('utf-8'))

        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:
            result = map_single_trace.MapSingleTrace(
                trace_handle, _Handle(map_script.filename))

        self.assertFalse(result.failures)
        r = result.pairs['result']
        self.assertEquals(r['numProcesses'], 1)
  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("""
      tr.mre.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 testMapFunctionThatThrows(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).encode('utf-8'))

        with map_single_trace.TemporaryMapScript("""
      tr.mre.FunctionRegistry.register(
          function MyMapFunction(results, model) {
            throw new Error('Expected error');
          });
    """) 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.MapFunctionFailure)