def testCanImportEmptyTimelineData(self):
     # We can import empty lists and empty string wrapped in subclasses of
     # TimelineData
     self.assertTrue(
         empty_timeline_data_importer.EmptyTimelineDataImporter.CanImport(
             tracing_timeline_data.TracingTimelineData([])))
     self.assertTrue(
         empty_timeline_data_importer.EmptyTimelineDataImporter.CanImport(
             tracing_timeline_data.TracingTimelineData('')))
 def testCanImportEmpty(self):
   # TraceEventTimelineImporter needs to return false for empty lists and
   # strings, because it assumes that they are >0 in len. But, TimelineMode can
   # still import empty lists and strings (wrapped in a TimelineData object)
   # via EmptyTimelineDataImporter.
   self.assertFalse(
       trace_event_importer.TraceEventTimelineImporter.CanImport(
           tracing_timeline_data.TracingTimelineData([])))
   self.assertFalse(
       trace_event_importer.TraceEventTimelineImporter.CanImport(
           tracing_timeline_data.TracingTimelineData('')))
  def testNestedParsing(self):
    events = [
      {'name': 'a', 'args': {}, 'pid': 1, 'ts': 1, 'tts': 2, 'cat': 'foo',
       'tid': 1, 'ph': 'B'},
      {'name': 'b', 'args': {}, 'pid': 1, 'ts': 3, 'tts': 3, 'cat': 'bar',
       'tid': 1, 'ph': 'B'},
      {'name': 'b', 'args': {}, 'pid': 1, 'ts': 5, 'tts': 4, 'cat': 'bar',
       'tid': 1, 'ph': 'E'},
      {'name': 'a', 'args': {}, 'pid': 1, 'ts': 7, 'tts': 5, 'cat': 'foo',
       'tid': 1, 'ph': 'E'}
    ]
    timeline_data = tracing_timeline_data.TracingTimelineData(events)
    m = timeline_model.TimelineModel(timeline_data=timeline_data,
                                     shift_world_to_zero=False)
    t = m.GetAllProcesses()[0].threads[1]

    slice_a = FindEventNamed(t.all_slices, 'a')
    slice_b = FindEventNamed(t.all_slices, 'b')

    self.assertEqual('a', slice_a.name)
    self.assertEqual('foo', slice_a.category)
    self.assertAlmostEqual(0.001, slice_a.start)
    self.assertAlmostEqual(0.006, slice_a.duration)
    self.assertAlmostEqual(0.002, slice_a.thread_start)
    self.assertAlmostEqual(0.003, slice_a.thread_duration)

    self.assertEqual('b', slice_b.name)
    self.assertEqual('bar', slice_b.category)
    self.assertAlmostEqual(0.003, slice_b.start)
    self.assertAlmostEqual(0.002, slice_b.duration)
    self.assertAlmostEqual(0.003, slice_b.thread_start)
    self.assertAlmostEqual(0.001, slice_b.thread_duration)
def Main(args):
    if len(args) is not 1:
        print 'Invalid arguments. Usage: measure_trace.py <trace file>'
        return 1
    with open(args[0]) as trace_file:
        trace_data = tracing_timeline_data.TracingTimelineData(
            json.load(trace_file))

    timeline_model = model.TimelineModel(trace_data)
    smoothness_metric = smoothness.SmoothnessMetric()
    formatters = [
        buildbot_output_formatter.BuildbotOutputFormatter(sys.stdout)
    ]
    results = page_test_results.PageTestResults(output_formatters=formatters)
    for thread in timeline_model.GetAllThreads():
        interaction_records = _ExtractInteractionsRecordFromThread(
            thread, timeline_model)
        if not any(interaction_records):
            continue
        records_label_to_records_map = collections.defaultdict(list)
        for r in interaction_records:
            records_label_to_records_map[r.label].append(r)
        for label, records in records_label_to_records_map.iteritems():
            if records[0].is_smooth:
                page = page_module.Page('interaction-record://%s' % label)
                results.WillRunPage(page)
                smoothness_metric.AddResults(timeline_model, thread, records,
                                             results)
                results.DidRunPage(page)
    results.PrintSummary()
    return 0
  def testNestedAutoclosing(self):
    events = [
      # Tasks that don't finish.
      {'name': 'a1', 'args': {}, 'pid': 1, 'ts': 1, 'cat': 'foo',
       'tid': 1, 'ph': 'B'},
      {'name': 'a2', 'args': {}, 'pid': 1, 'ts': 1.5, 'cat': 'foo',
       'tid': 1, 'ph': 'B'},

      # Slice that does finish to give an 'end time' to make autoclosing work.
      {'name': 'b', 'args': {}, 'pid': 1, 'ts': 1, 'cat': 'foo',
       'tid': 2, 'ph': 'B'},
      {'name': 'b', 'args': {}, 'pid': 1, 'ts': 2, 'cat': 'foo',
       'tid': 2, 'ph': 'E'}
    ]
    timeline_data = tracing_timeline_data.TracingTimelineData(events)
    m = timeline_model.TimelineModel(timeline_data=timeline_data,
                                     shift_world_to_zero=False)
    t1 = m.GetAllProcesses()[0].threads[1]
    t2 = m.GetAllProcesses()[0].threads[2]

    slice_a1 = FindEventNamed(t1.all_slices, 'a1')
    slice_a2 = FindEventNamed(t1.all_slices, 'a2')
    FindEventNamed(t2.all_slices, 'b')

    self.assertAlmostEqual(0.002, slice_a1.end)
    self.assertAlmostEqual(0.002, slice_a2.end)
  def testMultiCounter(self):
    events = [
      {'name': 'ctr', 'args': {'value1': 0, 'value2': 7}, 'pid': 1, 'ts': 0,
       'cat': 'foo', 'tid': 1, 'ph': 'C'},
      {'name': 'ctr', 'args': {'value1': 10, 'value2': 4}, 'pid': 1, 'ts': 10,
       'cat': 'foo', 'tid': 1, 'ph': 'C'},
      {'name': 'ctr', 'args': {'value1': 0, 'value2': 1}, 'pid': 1, 'ts': 20,
       'cat': 'foo', 'tid': 1, 'ph': 'C'}
    ]
    timeline_data = tracing_timeline_data.TracingTimelineData(events)
    m = timeline_model.TimelineModel(timeline_data=timeline_data)
    p = m.GetAllProcesses()[0]
    ctr = p.counters['foo.ctr']
    self.assertEqual('ctr', ctr.name)

    self.assertEqual('ctr', ctr.name)
    self.assertEqual('foo', ctr.category)
    self.assertEqual(3, ctr.num_samples)
    self.assertEqual(2, ctr.num_series)

    self.assertEqual(sorted(['value1', 'value2']), sorted(ctr.series_names))
    self.assertEqual(sorted([0, 0.01, 0.02]), sorted(ctr.timestamps))
    self.assertEqual(sorted([0, 7, 10, 4, 0, 1]), sorted(ctr.samples))
    # We can't check ctr.totals here because it can change depending on
    # the order in which the series names are added.
    self.assertEqual(14, ctr.max_total)
  def testStartFinishOneSliceOneThread(self):
    events = [
      # Time is intentionally out of order.
      {'name': 'a', 'args': {}, 'pid': 52, 'ts': 560, 'cat': 'cat',
       'tid': 53,
         'ph': 'F', 'id': 72},
      {'name': 'a', 'pid': 52, 'ts': 524, 'cat': 'cat',
       'tid': 53,
         'ph': 'S', 'id': 72, 'args': {'foo': 'bar'}}
    ]

    timeline_data = tracing_timeline_data.TracingTimelineData(events)
    m = timeline_model.TimelineModel(timeline_data=timeline_data)

    events = list(m.IterAllEvents())
    self.assertEqual(2, len(events))

    processes = m.GetAllProcesses()
    t = processes[0].threads[53]
    slices = t.async_slices
    self.assertEqual(1, len(slices))
    self.assertEqual('a', slices[0].name)
    self.assertEqual('cat', slices[0].category)
    self.assertEqual(72, slices[0].id)
    self.assertEqual('bar', slices[0].args['foo'])
    self.assertEqual(0, slices[0].start)
    self.assertAlmostEqual((60 - 24) / 1000.0, slices[0].duration)
    self.assertEqual(t, slices[0].start_thread)
    self.assertEqual(t, slices[0].end_thread)
  def testTraceEventsWithTabIdsMarkers(self):
    trace_events = [
      {'name': 'a', 'args': {}, 'pid': 1, 'ts': 20, 'tts': 10, 'cat': 'foo',
       'tid': 1, 'ph': 'B'},
      # tab-id-1
      {'name': 'tab-id-1', 'args': {}, 'pid': 1, 'ts': 25, 'cat': 'foo',
       'tid': 1,
         'ph': 'S', 'id': 72},
      {'name': 'a', 'args': {}, 'pid': 1, 'ts': 30, 'tts': 20, 'cat': 'foo',
       'tid': 1, 'ph': 'E'},
      {'name': 'tab-id-1', 'args': {}, 'pid': 1, 'ts': 35, 'cat': 'foo',
       'tid': 1,
         'ph': 'F', 'id': 72},
      # tab-id-2
      {'name': 'tab-id-2', 'args': {}, 'pid': 1, 'ts': 25, 'cat': 'foo',
       'tid': 2,
         'ph': 'S', 'id': 72},
      {'name': 'tab-id-2', 'args': {}, 'pid': 1, 'ts': 26, 'cat': 'foo',
       'tid': 2,
         'ph': 'F', 'id': 72},
     ]
    event_data = {'traceEvents': trace_events,
                  'tabIds': ['tab-id-1', 'tab-id-2']}
    timeline_data = tracing_timeline_data.TracingTimelineData(event_data)
    m = timeline_model.TimelineModel(timeline_data=timeline_data)
    processes = m.GetAllProcesses()
    self.assertEqual(1, len(processes))
    self.assertIs(processes[0], m.GetRendererProcessFromTabId('tab-id-1'))
    self.assertIs(processes[0], m.GetRendererProcessFromTabId('tab-id-2'))

    p = processes[0]
    self.assertEqual(2, len(p.threads))
    self.assertIs(p.threads[1], m.GetRendererThreadFromTabId('tab-id-1'))
    self.assertIs(p.threads[2], m.GetRendererThreadFromTabId('tab-id-2'))
Beispiel #9
0
 def testNoLeakedTempFileWhenUploadingTrace(self):
   v = trace.TraceValue(
       None, tracing_timeline_data.TracingTimelineData({'test': 1}))
   trace.cloud_storage.SetCalculatedHashesForTesting(
       TestDefaultDict(123))
   bucket = trace.cloud_storage.PUBLIC_BUCKET
   v.UploadToCloud(bucket)
   self.assertTrue(_IsEmptyDir(self.temp_test_dir))
  def testBasicSingleThreadNonnestedParsing(self):
    events = [
      {'name': 'a', 'args': {}, 'pid': 52, 'ts': 520, 'tts': 280, 'cat': 'foo',
       'tid': 53, 'ph': 'B'},
      {'name': 'a', 'args': {}, 'pid': 52, 'ts': 560, 'tts': 310, 'cat': 'foo',
       'tid': 53, 'ph': 'E'},
      {'name': 'b', 'args': {}, 'pid': 52, 'ts': 629, 'tts': 356, 'cat': 'bar',
       'tid': 53, 'ph': 'B'},
      {'name': 'b', 'args': {}, 'pid': 52, 'ts': 631, 'tts': 357, 'cat': 'bar',
       'tid': 53, 'ph': 'E'},
      {'name': 'c', 'args': {}, 'pid': 52, 'ts': 633, 'cat': 'baz',
       'tid': 53, 'ph': 'B'},
      {'name': 'c', 'args': {}, 'pid': 52, 'ts': 637, 'cat': 'baz',
       'tid': 53, 'ph': 'E'}
    ]

    timeline_data = tracing_timeline_data.TracingTimelineData(events)
    m = timeline_model.TimelineModel(timeline_data=timeline_data)
    processes = m.GetAllProcesses()
    self.assertEqual(1, len(processes))
    p = processes[0]
    self.assertEqual(52, p.pid)

    self.assertEqual(1, len(p.threads))
    t = p.threads[53]
    self.assertEqual(3, len(t.all_slices))
    self.assertEqual(53, t.tid)
    slice_event = t.all_slices[0]
    self.assertEqual('a', slice_event.name)
    self.assertEqual('foo', slice_event.category)
    self.assertAlmostEqual(0, slice_event.start)
    self.assertAlmostEqual((560 - 520) / 1000.0, slice_event.duration)
    self.assertAlmostEqual((560 - 520) / 1000.0, slice_event.end)
    self.assertAlmostEqual(280 / 1000.0, slice_event.thread_start)
    self.assertAlmostEqual((310 - 280) / 1000.0, slice_event.thread_duration)
    self.assertAlmostEqual(310 / 1000.0, slice_event.thread_end)
    self.assertEqual(0, len(slice_event.sub_slices))

    slice_event = t.all_slices[1]
    self.assertEqual('b', slice_event.name)
    self.assertEqual('bar', slice_event.category)
    self.assertAlmostEqual((629 - 520) / 1000.0, slice_event.start)
    self.assertAlmostEqual((631 - 629) / 1000.0, slice_event.duration)
    self.assertAlmostEqual((631 - 520) / 1000.0, slice_event.end)
    self.assertAlmostEqual(356 / 1000.0, slice_event.thread_start)
    self.assertAlmostEqual((357 - 356) / 1000.0, slice_event.thread_duration)
    self.assertAlmostEqual(357 / 1000.0, slice_event.thread_end)
    self.assertEqual(0, len(slice_event.sub_slices))

    slice_event = t.all_slices[2]
    self.assertEqual('c', slice_event.name)
    self.assertEqual('baz', slice_event.category)
    self.assertAlmostEqual((633 - 520) / 1000.0, slice_event.start)
    self.assertAlmostEqual((637 - 633) / 1000.0, slice_event.duration)
    self.assertEqual(None, slice_event.thread_start)
    self.assertEqual(None, slice_event.thread_duration)
    self.assertEqual(None, slice_event.thread_end)
    self.assertEqual(0, len(slice_event.sub_slices))
    def testWrite1(self):
        ri = tracing_timeline_data.TracingTimelineData(map(json.loads, []))
        f = cStringIO.StringIO()
        ri.Serialize(f)
        v = f.getvalue()

        j = json.loads(v)
        assert 'traceEvents' in j
        self.assertEquals(j['traceEvents'], [])
 def testBrowserProcess(self):
   ri = tracing_timeline_data.TracingTimelineData(map(json.loads, [
       '{"name": "process_name",'
       '"args": {"name": "Browser"},'
       '"pid": 5, "ph": "M"}',
       '{"name": "thread_name",'
       '"args": {"name": "CrBrowserMain"},'
       '"pid": 5, "tid": 32578, "ph": "M"}']))
   timeline_model = model.TimelineModel(ri)
   self.assertEquals(timeline_model.browser_process.pid, 5)
  def testMultiplePidParsing(self):
    events = [
      {'name': 'a', 'args': {}, 'pid': 1, 'ts': 2, 'tts': 1, 'cat': 'foo',
       'tid': 1, 'ph': 'B'},
      {'name': 'a', 'args': {}, 'pid': 1, 'ts': 4, 'tts': 2, 'cat': 'foo',
       'tid': 1, 'ph': 'E'},
      {'name': 'b', 'args': {}, 'pid': 2, 'ts': 6, 'tts': 3, 'cat': 'bar',
       'tid': 2, 'ph': 'B'},
      {'name': 'b', 'args': {}, 'pid': 2, 'ts': 8, 'tts': 4, 'cat': 'bar',
       'tid': 2, 'ph': 'E'}
    ]

    timeline_data = tracing_timeline_data.TracingTimelineData(events)
    m = timeline_model.TimelineModel(timeline_data=timeline_data)
    processes = m.GetAllProcesses()
    self.assertEqual(2, len(processes))

    p = processes[0]
    self.assertEqual(1, p.pid)
    self.assertEqual(1, len(p.threads))

    # Check process 1 thread 1.
    t = p.threads[1]
    self.assertEqual(1, len(t.all_slices))
    self.assertEqual(1, t.tid)

    slice_event = t.all_slices[0]
    self.assertEqual('a', slice_event.name)
    self.assertEqual('foo', slice_event.category)
    self.assertAlmostEqual(0, slice_event.start)
    self.assertAlmostEqual((4 - 2) / 1000.0, slice_event.duration)
    self.assertAlmostEqual(1 / 1000.0, slice_event.thread_start)
    self.assertAlmostEqual((2 - 1) / 1000.0, slice_event.thread_duration)

    # Check process 2 thread 2.
    # TODO: will this be in deterministic order?
    p = processes[1]
    self.assertEqual(2, p.pid)
    self.assertEqual(1, len(p.threads))
    t = p.threads[2]
    self.assertEqual(1, len(t.all_slices))
    self.assertEqual(2, t.tid)

    slice_event = t.all_slices[0]
    self.assertEqual('b', slice_event.name)
    self.assertEqual('bar', slice_event.category)
    self.assertAlmostEqual((6 - 2) / 1000.0, slice_event.start)
    self.assertAlmostEqual((8 - 6) / 1000.0, slice_event.duration)
    self.assertAlmostEqual(3 / 1000.0, slice_event.thread_start)
    self.assertAlmostEqual((4 - 3) / 1000.0, slice_event.thread_duration)

    # Check getAllThreads.
    self.assertEqual([processes[0].threads[1],
                      processes[1].threads[2]],
                      m.GetAllThreads())
Beispiel #14
0
 def testNoLeakedTempFileWhenTraceSerialize(self):
   tempdir = tempfile.mkdtemp()
   v = trace.TraceValue(
       None, tracing_timeline_data.TracingTimelineData({'test': 1}))
   fh = v.Serialize(tempdir)
   try:
     shutil.rmtree(fh.GetAbsPath(), ignore_errors=True)
     self.assertTrue(tempdir)
   finally:
     shutil.rmtree(tempdir)
     self.assertTrue(_IsEmptyDir(self.temp_test_dir))
  def testImportObjectInsteadOfArray(self):
    events = {'traceEvents': [
      {'name': 'a', 'args': {}, 'pid': 52, 'ts': 524, 'cat': 'foo',
       'tid': 53, 'ph': 'B'},
      {'name': 'a', 'args': {}, 'pid': 52, 'ts': 560, 'cat': 'foo',
       'tid': 53, 'ph': 'E'}
    ]}

    timeline_data = tracing_timeline_data.TracingTimelineData(events)
    m = timeline_model.TimelineModel(timeline_data=timeline_data)
    self.assertEqual(1, len(m.GetAllProcesses()))
  def testSliceHierarchy(self):
    ''' The slice hierarchy should look something like this:
           [            a            ]
              [      b      ]  [ d ]
              [ c ]     [ e ]
    '''
    events = [
      {'name': 'a', 'args': {}, 'pid': 52, 'ts': 100, 'cat': 'foo',
       'tid': 53, 'ph': 'B'},
      {'name': 'a', 'args': {}, 'pid': 52, 'ts': 200, 'cat': 'foo',
       'tid': 53, 'ph': 'E'},
      {'name': 'b', 'args': {}, 'pid': 52, 'ts': 125, 'cat': 'foo',
       'tid': 53, 'ph': 'B'},
      {'name': 'b', 'args': {}, 'pid': 52, 'ts': 165, 'cat': 'foo',
       'tid': 53, 'ph': 'E'},
      {'name': 'c', 'args': {}, 'pid': 52, 'ts': 125, 'cat': 'foo',
       'tid': 53, 'ph': 'B'},
      {'name': 'c', 'args': {}, 'pid': 52, 'ts': 135, 'cat': 'foo',
       'tid': 53, 'ph': 'E'},
      {'name': 'd', 'args': {}, 'pid': 52, 'ts': 175, 'cat': 'foo',
       'tid': 53, 'ph': 'B'},
      {'name': 'd', 'args': {}, 'pid': 52, 'ts': 190, 'cat': 'foo',
       'tid': 53, 'ph': 'E'},
      {'name': 'e', 'args': {}, 'pid': 52, 'ts': 155, 'cat': 'foo',
       'tid': 53, 'ph': 'B'},
      {'name': 'e', 'args': {}, 'pid': 52, 'ts': 165, 'cat': 'foo',
       'tid': 53, 'ph': 'E'}
    ]
    timeline_data = tracing_timeline_data.TracingTimelineData(events)
    m = timeline_model.TimelineModel(timeline_data=timeline_data,
                                     shift_world_to_zero=False)
    processes = m.GetAllProcesses()
    self.assertEqual(1, len(processes))
    p = processes[0]

    self.assertEqual(1, len(p.threads))
    t = p.threads[53]

    slice_a = t.all_slices[0]
    self.assertEqual(4, len(slice_a.GetAllSubSlices()))
    self.assertEqual('a', slice_a.name)
    self.assertEqual(100 / 1000.0, slice_a.start)
    self.assertEqual(200 / 1000.0, slice_a.end)
    self.assertEqual(2, len(slice_a.sub_slices))

    slice_b = slice_a.sub_slices[0]
    self.assertEqual('b', slice_b.name)
    self.assertEqual(2, len(slice_b.sub_slices))
    self.assertEqual('c', slice_b.sub_slices[0].name)
    self.assertEqual('e', slice_b.sub_slices[1].name)

    slice_d = slice_a.sub_slices[1]
    self.assertEqual('d', slice_d.name)
    self.assertEqual(0, len(slice_d.sub_slices))
  def testImportStringWithTrailingNewLine(self):
    events = [
      {'name': 'a', 'args': {}, 'pid': 52, 'ts': 524, 'cat': 'foo',
       'tid': 53, 'ph': 'B'},
      {'name': 'a', 'args': {}, 'pid': 52, 'ts': 560, 'cat': 'foo',
       'tid': 53, 'ph': 'E'}
    ]

    timeline_data = tracing_timeline_data.TracingTimelineData(
        json.dumps(events) + '\n')
    m = timeline_model.TimelineModel(timeline_data=timeline_data)
    self.assertEqual(1, len(m.GetAllProcesses()))
  def testAsyncStepsMissingFinish(self):
    events = [
      # Time is intentionally out of order.
      {'name': 'a', 'args': {'step': 's1', 'y': 2}, 'pid': 52, 'ts': 548,
       'cat': 'foo', 'tid': 53, 'ph': 'T', 'id': 72},
      {'name': 'a', 'args': {'z': 3}, 'pid': 52, 'ts': 560, 'cat': 'foo',
       'tid': 53, 'ph': 'S', 'id': 72}
    ]

    timeline_data = tracing_timeline_data.TracingTimelineData(events)
    m = timeline_model.TimelineModel(timeline_data=timeline_data)
    t = m.GetAllProcesses()[0].threads[53]
    self.assertTrue(t is not None)
 def testImportOldFormat(self):
   lines = [
     '[',
     '{"cat":"a","pid":9,"tid":8,"ts":194,"ph":"E","name":"I","args":{}},',
     '{"cat":"b","pid":9,"tid":8,"ts":194,"ph":"B","name":"I","args":{}}',
     ']'
     ]
   text = '\n'.join(lines)
   timeline_data = tracing_timeline_data.TracingTimelineData(text)
   m = timeline_model.TimelineModel(timeline_data=timeline_data)
   processes = m.GetAllProcesses()
   self.assertEqual(1, len(processes))
   self.assertEqual(1, len(processes[0].threads[8].all_slices))
  def testImportStringWithEndingCommaButMissingCloseSquareBracketCRLF(self):
    lines = [
      '[',
      '{"name": "a", "args": {}, "pid": 52, "ts": 524, "cat": "foo", '
        '"tid": 53, "ph": "B"},',
      '{"name": "a", "args": {}, "pid": 52, "ts": 560, "cat": "foo", '
        '"tid": 53, "ph": "E"},'
      ]
    text = '\r\n'.join(lines)

    timeline_data = tracing_timeline_data.TracingTimelineData(text)
    m = timeline_model.TimelineModel(timeline_data=timeline_data)
    processes = m.GetAllProcesses()
    self.assertEqual(1, len(processes))
    self.assertEqual(1, len(processes[0].threads[53].all_slices))
 def testImportSamplesMissingArgs(self):
   events = [
     {'name': 'a', 'pid': 52, 'ts': 548, 'cat': 'test',
      'tid': 53, 'ph': 'P'},
     {'name': 'b', 'pid': 52, 'ts': 548, 'cat': 'test',
      'tid': 53, 'ph': 'P'},
     {'name': 'c', 'pid': 52, 'ts': 549, 'cat': 'test',
      'tid': 53, 'ph': 'P'}
   ]
   timeline_data = tracing_timeline_data.TracingTimelineData(events)
   m = timeline_model.TimelineModel(timeline_data=timeline_data)
   p = m.GetAllProcesses()[0]
   t = p.threads[53]
   self.assertEqual(3, len(t.samples))
   self.assertEqual(0, len(m.import_errors))
Beispiel #22
0
 def testAsDictWhenTraceSerializedAndUploaded(self):
   tempdir = tempfile.mkdtemp()
   try:
     v = trace.TraceValue(
         None, tracing_timeline_data.TracingTimelineData({'test': 1}))
     fh = v.Serialize(tempdir)
     trace.cloud_storage.SetCalculatedHashesForTesting(
         {fh.GetAbsPath(): 123})
     bucket = trace.cloud_storage.PUBLIC_BUCKET
     cloud_url = v.UploadToCloud(bucket)
     d = v.AsDict()
     self.assertEqual(d['file_id'], fh.id)
     self.assertEqual(d['cloud_url'], cloud_url)
   finally:
     shutil.rmtree(tempdir)
Beispiel #23
0
 def testAsDictWhenTraceIsNotSerializedAndUploaded(self):
   test_temp_file = tempfile.NamedTemporaryFile(delete=False)
   try:
     v = trace.TraceValue(
         None, tracing_timeline_data.TracingTimelineData({'test': 1}))
     trace.cloud_storage.SetCalculatedHashesForTesting(
         TestDefaultDict(123))
     bucket = trace.cloud_storage.PUBLIC_BUCKET
     cloud_url = v.UploadToCloud(bucket)
     d = v.AsDict()
     self.assertEqual(d['cloud_url'], cloud_url)
   finally:
     if os.path.exists(test_temp_file.name):
       test_temp_file.close()
       os.remove(test_temp_file.name)
  def testImportErrornousFlowEvent(self):
    events = [
      {'name': 'a', 'cat': 'foo', 'id': 70, 'pid': 52, 'tid': 53, 'ts': 548,
       'ph': 's', 'args': {}},
      {'name': 'a2', 'cat': 'foo', 'id': 70, 'pid': 52, 'tid': 53, 'ts': 550,
       'ph': 's', 'args': {}},
      {'name': 'b', 'cat': 'foo', 'id': 73, 'pid': 52, 'tid': 53, 'ts': 570,
       'ph': 'f', 'args': {}},
      {'name': 'a', 'cat': 'foo', 'id': 72, 'pid': 52, 'tid': 53, 'ts': 560,
       'ph': 't', 'args': {}},
    ]

    timeline_data = tracing_timeline_data.TracingTimelineData(events)
    m = timeline_model.TimelineModel(timeline_data=timeline_data)
    self.assertEqual(0, len(m.flow_events))
  def testArgumentDupeCreatesNonFailingImportError(self):
    events = [
      {'name': 'a', 'args': {'x': 1}, 'pid': 1, 'ts': 520, 'cat': 'foo',
       'tid': 1, 'ph': 'B'},
      {'name': 'a', 'args': {'x': 2}, 'pid': 1, 'ts': 560, 'cat': 'foo',
       'tid': 1, 'ph': 'E'}
    ]

    timeline_data = tracing_timeline_data.TracingTimelineData(events)
    m = timeline_model.TimelineModel(timeline_data=timeline_data)
    processes = m.GetAllProcesses()
    t = processes[0].threads[1]
    slice_a = FindEventNamed(t.all_slices, 'a')

    self.assertEqual(2, slice_a.args['x'])
    self.assertEqual(1, len(m.import_errors))
  def testImportStringWithMissingCloseSquareBracketAndNewline(self):
    events = [
      {'name': 'a', 'args': {}, 'pid': 52, 'ts': 524, 'cat': 'foo',
       'tid': 53, 'ph': 'B'},
      {'name': 'a', 'args': {}, 'pid': 52, 'ts': 560, 'cat': 'foo',
       'tid': 53, 'ph': 'E'}
    ]

    tmp = json.dumps(events)
    self.assertEqual(']', tmp[-1])

    # Drop off the trailing ] and add a newline
    dropped = tmp[:-1]
    timeline_data = tracing_timeline_data.TracingTimelineData(dropped + '\n')
    m = timeline_model.TimelineModel(timeline_data=timeline_data)
    self.assertEqual(1, len(m.GetAllProcesses()))
  def testInstanceCounter(self):
    events = [
      {'name': 'ctr', 'args': {'value': 0}, 'pid': 1, 'ts': 0, 'cat': 'foo',
       'tid': 1,
       'ph': 'C', 'id': 0},
      {'name': 'ctr', 'args': {'value': 10}, 'pid': 1, 'ts': 10, 'cat': 'foo',
       'tid': 1,
       'ph': 'C', 'id': 0},
      {'name': 'ctr', 'args': {'value': 10}, 'pid': 1, 'ts': 10, 'cat': 'foo',
       'tid': 1,
       'ph': 'C', 'id': 1},
      {'name': 'ctr', 'args': {'value': 20}, 'pid': 1, 'ts': 15, 'cat': 'foo',
       'tid': 1,
       'ph': 'C', 'id': 1},
      {'name': 'ctr', 'args': {'value': 30}, 'pid': 1, 'ts': 18, 'cat': 'foo',
       'tid': 1,
       'ph': 'C', 'id': 1},
      {'name': 'ctr', 'args': {'value': 40}, 'pid': 1, 'ts': 20, 'cat': 'bar',
       'tid': 1,
       'ph': 'C', 'id': 2}
    ]
    timeline_data = tracing_timeline_data.TracingTimelineData(events)
    m = timeline_model.TimelineModel(timeline_data=timeline_data)
    p = m.GetAllProcesses()[0]
    ctr = p.counters['foo.ctr[0]']
    self.assertEqual('ctr[0]', ctr.name)
    self.assertEqual('foo', ctr.category)
    self.assertEqual(2, ctr.num_samples)
    self.assertEqual(1, ctr.num_series)
    self.assertEqual([0, 0.01], ctr.timestamps)
    self.assertEqual([0, 10], ctr.samples)

    ctr = m.GetAllProcesses()[0].counters['foo.ctr[1]']
    self.assertEqual('ctr[1]', ctr.name)
    self.assertEqual('foo', ctr.category)
    self.assertEqual(3, ctr.num_samples)
    self.assertEqual(1, ctr.num_series)
    self.assertEqual([0.01, 0.015, 0.018], ctr.timestamps)
    self.assertEqual([10, 20, 30], ctr.samples)

    ctr = m.GetAllProcesses()[0].counters['bar.ctr[2]']
    self.assertEqual('ctr[2]', ctr.name)
    self.assertEqual('bar', ctr.category)
    self.assertEqual(1, ctr.num_samples)
    self.assertEqual(1, ctr.num_series)
    self.assertEqual([0.02], ctr.timestamps)
    self.assertEqual([40], ctr.samples)
 def StopTracing(self):
   """ Stops tracing and returns the result as TimelineData object. """
   tab_ids_list = []
   for (i, _) in enumerate(self._browser.tabs):
     tab = self.tab_list_backend.Get(i, None)
     if tab:
       success = tab.EvaluateJavaScript(
           "console.time('" + tab.id + "');" +
           "console.timeEnd('" + tab.id + "');" +
           "console.time.toString().indexOf('[native code]') != -1;")
       if not success:
         raise Exception('Page stomped on console.time')
       tab_ids_list.append(tab.id)
   trace_events = self._tracing_backend.StopTracing()
   # Augment tab_ids data to trace events.
   event_data = {'traceEvents' : trace_events, 'tabIds': tab_ids_list}
   return tracing_timeline_data.TracingTimelineData(event_data)
  def testAutoclosingWithEventsOutsideBounds(self):
    events = [
      # Slice that begins before min and ends after max of the other threads.
      {'name': 'a', 'args': {}, 'pid': 1, 'ts': 0, 'tts': 0, 'cat': 'foo',
       'tid': 1, 'ph': 'B'},
      {'name': 'b', 'args': {}, 'pid': 1, 'ts': 6, 'tts': 3, 'cat': 'foo',
       'tid': 1, 'ph': 'B'},

      # Slice that does finish to give an 'end time' to establish a basis
      {'name': 'c', 'args': {}, 'pid': 1, 'ts': 2, 'tts': 1, 'cat': 'bar',
       'tid': 2, 'ph': 'B'},
      {'name': 'c', 'args': {}, 'pid': 1, 'ts': 4, 'tts': 2, 'cat': 'bar',
       'tid': 2, 'ph': 'E'}
    ]
    timeline_data = tracing_timeline_data.TracingTimelineData(events)
    m = timeline_model.TimelineModel(timeline_data=timeline_data,
                                     shift_world_to_zero=False)
    p = m.GetAllProcesses()[0]
    t1 = p.threads[1]
    t1_thread_time_bounds = m._thread_time_bounds[t1] # pylint: disable=W0212
    self.assertAlmostEqual(0.000, t1_thread_time_bounds.min)
    self.assertAlmostEqual(0.003, t1_thread_time_bounds.max)
    self.assertEqual(2, len(t1.all_slices))

    slice_event = FindEventNamed(t1.all_slices, 'a')
    self.assertEqual('a', slice_event.name)
    self.assertEqual('foo', slice_event.category)
    self.assertAlmostEqual(0, slice_event.start)
    self.assertAlmostEqual(0.006, slice_event.duration)
    self.assertAlmostEqual(0, slice_event.thread_start)
    self.assertAlmostEqual(0.003, slice_event.thread_duration)

    t2 = p.threads[2]
    t2_thread_time_bounds = m._thread_time_bounds[t2] # pylint: disable=W0212
    self.assertAlmostEqual(0.001, t2_thread_time_bounds.min)
    self.assertAlmostEqual(0.002, t2_thread_time_bounds.max)
    slice2 = FindEventNamed(t2.all_slices, 'c')
    self.assertEqual('c', slice2.name)
    self.assertEqual('bar', slice2.category)
    self.assertAlmostEqual(0.002, slice2.start)
    self.assertAlmostEqual(0.002, slice2.duration)
    self.assertAlmostEqual(0.001, slice2.thread_start)
    self.assertAlmostEqual(0.001, slice2.thread_duration)

    self.assertAlmostEqual(0.000, m.bounds.min)
    self.assertAlmostEqual(0.006, m.bounds.max)
 def testAutoclosingLoneBegin(self):
   events = [
     # Slice that doesn't finish.
     {'name': 'a', 'args': {}, 'pid': 1, 'ts': 1, 'tts': 1, 'cat': 'foo',
      'tid': 1, 'ph': 'B'}
   ]
   timeline_data = tracing_timeline_data.TracingTimelineData(events)
   m = timeline_model.TimelineModel(timeline_data=timeline_data)
   p = m.GetAllProcesses()[0]
   t = p.threads[1]
   slice_event = t.all_slices[0]
   self.assertEqual('a', slice_event.name)
   self.assertEqual('foo', slice_event.category)
   self.assertTrue(slice_event.did_not_finish)
   self.assertAlmostEqual(0, slice_event.start)
   self.assertAlmostEqual(0, slice_event.duration)
   self.assertAlmostEqual(1 / 1000.0, slice_event.thread_start)
   self.assertAlmostEqual(0, slice_event.thread_duration)