def testCollectAgentTraceData(self): builder = trace_data.TraceDataBuilder() self._agent.StartAgentTracing(self._config, 0) self._agent.CollectAgentTraceData(builder) self.assertFalse(self._agent._snapshot_ongoing) builder = builder.AsData() self.assertTrue(builder.HasTraceFor(trace_data.CPU_TRACE_DATA))
def testTraceEventsWithTabIdsMarkers(self): builder = trace_data_module.TraceDataBuilder() builder.AddEventsTo(trace_data_module.CHROME_TRACE_PART, [ {'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}, ]) builder.AddEventsTo( trace_data_module.TAB_ID_PART, ['tab-id-1', 'tab-id-2']) m = timeline_model.TimelineModel(builder.AsData()) 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'))
def testSetTraceForRaisesAfterAsData(self): builder = trace_data.TraceDataBuilder() builder.AsData() self.assertRaises( exceptions.Exception, lambda: builder.AddTraceFor(trace_data.TELEMETRY_PART, {}))
def testSetTraceForRaisesWithAlreadySetPart(self): builder = trace_data.TraceDataBuilder() builder.SetTraceFor(trace_data.TELEMETRY_PART, {}) self.assertRaises( exceptions.Exception, lambda: builder.SetTraceFor(trace_data.TELEMETRY_PART, {}))
def testCollectTracingDataTimeout(self): self._inspector_socket.AddEvent( 'Tracing.dataCollected', {'value': { 'traceEvents': [{ 'ph': 'B' }] }}, 9) self._inspector_socket.AddEvent( 'Tracing.dataCollected', {'value': { 'traceEvents': [{ 'ph': 'E' }] }}, 19) self._inspector_socket.AddEvent('Tracing.tracingComplete', {}, 35) backend = tracing_backend.TracingBackend(self._inspector_socket) trace_data_builder = trace_data.TraceDataBuilder() # The third response is 16 seconds after the second response, so we expect # a TracingTimeoutException. with self.assertRaises(tracing_backend.TracingTimeoutException): backend._CollectTracingData(trace_data_builder, 10) traces = self._GetRawChromeTracesFor(trace_data_builder) self.assertEqual(2, len(traces)) self.assertEqual(1, len(traces[0].get('traceEvents', []))) self.assertEqual(1, len(traces[1].get('traceEvents', []))) self.assertFalse(backend._has_received_all_tracing_data)
def testCollectAgentTraceData(self): builder = trace_data.TraceDataBuilder() self.android_agent.CollectAgentTraceData(builder) self.assertTrue(self.android_agent._battor._collect_trace_data_called) builder = builder.AsData() self.assertTrue(builder.HasTracesFor(trace_data.BATTOR_TRACE_PART)) data_from_builder = builder.GetTracesFor(trace_data.BATTOR_TRACE_PART) self.assertEqual([_BATTOR_RETURN], data_from_builder)
def StopTimelineRecording(self): builder = trace_data_module.TraceDataBuilder() data = self._network.timeline_recorder.Stop() if data: builder.AddEventsTo(trace_data_module.INSPECTOR_TRACE_PART, data) self._timeline_model = timeline_model_module.TimelineModel( builder.AsData(), shift_world_to_zero=False)
def Stop(self): assert self.is_tracing_running, 'Can only stop tracing when tracing.' trace_data_builder = trace_data_module.TraceDataBuilder() for agent in self._active_agents_instances: agent.Stop(trace_data_builder) self._active_agents_instances = [] self._current_trace_options = None self._current_category_filter = None return trace_data_builder.AsData()
def testImportOverflowedTrace(self): builder = trace_data_module.TraceDataBuilder() builder.AddTraceFor( trace_data_module.CHROME_TRACE_PART, { 'traceEvents': [{ 'name': 'a', 'args': {}, 'pid': 1, 'ts': 7, 'cat': 'foo', 'tid': 1, 'ph': 'B' }, { 'name': 'a', 'args': {}, 'pid': 1, 'ts': 8, 'cat': 'foo', 'tid': 1, 'ph': 'E' }, { 'name': 'b', 'args': {}, 'pid': 2, 'ts': 9, 'cat': 'foo', 'tid': 2, 'ph': 'B' }, { 'name': 'b', 'args': {}, 'pid': 2, 'ts': 10, 'cat': 'foo', 'tid': 2, 'ph': 'E' }, { 'name': 'trace_buffer_overflowed', 'args': { 'overflowed_at_ts': 12 }, 'pid': 2, 'ts': 0, 'tid': 2, 'ph': 'M' }] }) builder.AddTraceFor(trace_data_module.TAB_ID_PART, ['tab-id-1', 'tab-id-2']) with self.assertRaises(tab_id_importer.TraceBufferOverflowException) \ as context: timeline_model.TimelineModel(builder.AsData()) self.assertTrue( 'Trace buffer of process with pid=2 overflowed at timestamp 12' in context.exception.message)
def testBasicChrome(self): builder = trace_data.TraceDataBuilder() builder.AddEventsTo(trace_data.CHROME_TRACE_PART, [1, 2, 3]) builder.AddEventsTo(trace_data.TAB_ID_PART, ['tab-7']) d = builder.AsData() self.assertTrue(d.HasEventsFor(trace_data.CHROME_TRACE_PART)) self.assertTrue(d.HasEventsFor(trace_data.TAB_ID_PART)) self.assertRaises(Exception, builder.AsData)
def testMinimumCpuThreshold(self): builder = trace_data.TraceDataBuilder() self._agent.StartAgentTracing(self._config, 0) time.sleep(2) self._agent.StopAgentTracing() self._agent.CollectAgentTraceData(builder) builder = builder.AsData() data = json.loads(builder.GetTraceFor(trace_data.CPU_TRACE_DATA)) self.assertTrue(data) for snapshot in data: for process in snapshot['args']['snapshot']['processes']: self.assertTrue(process['pCpu'] > 0 or process['pMem'] > 0)
def testMinimumCpuThreshold(self): builder = trace_data.TraceDataBuilder() self._agent.StartAgentTracing(self._config, 0) time.sleep(2) self._agent.StopAgentTracing() self._agent.CollectAgentTraceData(builder) builder = builder.AsData() data = json.loads(builder.GetTraceFor(trace_data.CPU_TRACE_DATA)) self.assertTrue(data) for snapshot in data: # We know that at least Python is running in every snapshot. self.assertTrue(len(snapshot['args']['snapshot']['processes']) > 0)
def Stop(self): if not self._is_recording: return None responses = self._inspector_network.GetResponseData() events = [r.AsTimelineEvent() for r in list(responses)] self._inspector_network.StopMonitoringNetwork() self._is_recording = False if len(events) == 0: return None builder = trace_data.TraceDataBuilder() builder.AddEventsTo(trace_data.INSPECTOR_TRACE_PART, events) return model.TimelineModel(builder.AsData(), shift_world_to_zero=False)
def testBasicChrome(self): builder = trace_data.TraceDataBuilder() builder.AddTraceFor(trace_data.CHROME_TRACE_PART, {'traceEvents': [1, 2, 3]}) builder.AddTraceFor(trace_data.TAB_ID_PART, ['tab-7']) builder.AddTraceFor(trace_data.BATTOR_TRACE_PART, 'battor data here') d = builder.AsData() self.assertTrue(d.HasTracesFor(trace_data.CHROME_TRACE_PART)) self.assertTrue(d.HasTracesFor(trace_data.TAB_ID_PART)) self.assertTrue(d.HasTracesFor(trace_data.BATTOR_TRACE_PART)) self.assertRaises(Exception, builder.AsData)
def testCollectAgentTraceDataFormat(self): builder = trace_data.TraceDataBuilder() self._agent.StartAgentTracing(self._config, 0) time.sleep(2) self._agent.StopAgentTracing() self._agent.CollectAgentTraceData(builder) builder = builder.AsData() data = json.loads(builder.GetTraceFor(trace_data.CPU_TRACE_DATA)) self.assertTrue(data) self.assertEquals(set(data[0].keys()), set(TRACE_EVENT_KEYS)) self.assertEquals(set(data[0]['args'].keys()), set(['processes'])) self.assertTrue(data[0]['args']['processes']) self.assertEquals(set(data[0]['args']['processes'][0].keys()), set(SNAPSHOT_KEYS))
def testTracing(self): devtools_client = self._devtools_client if not devtools_client.IsChromeTracingSupported(): self.skipTest('Browser does not support tracing, skipping test.') # Start Chrome tracing. options = tracing_options.TracingOptions() options.enable_chrome_trace = True devtools_client.StartChromeTracing(options) # Stop Chrome tracing and check that the resulting data is valid. builder = trace_data.TraceDataBuilder() devtools_client.StopChromeTracing(builder) model.TimelineModel(builder.AsData())
def testCollectTracingDataFromStreamNoContainer(self): self._inspector_socket.AddEvent( 'Tracing.tracingComplete', {'stream': '42'}, 1) self._inspector_socket.AddAsyncResponse( 'IO.read', {'data': '{"traceEvents": [{},{},{'}, 2) self._inspector_socket.AddAsyncResponse( 'IO.read', {'data': '},{},{}]}', 'eof': True}, 3) backend = tracing_backend.TracingBackend(self._inspector_socket) trace_data_builder = trace_data.TraceDataBuilder() backend._CollectTracingData(trace_data_builder, 10) trace_events = trace_data_builder.AsData().GetTracesFor( trace_data.CHROME_TRACE_PART)[0].get('traceEvents', []) self.assertEqual(5, len(trace_events)) self.assertTrue(backend._has_received_all_tracing_data)
def testSetTraceFor(self): telemetry_trace = { 'traceEvents': [1, 2, 3], 'metadata': { 'field1': 'value1' } } builder = trace_data.TraceDataBuilder() builder.AddTraceFor(trace_data.TELEMETRY_PART, telemetry_trace) d = builder.AsData() self.assertEqual(d.GetTracesFor(trace_data.TELEMETRY_PART), [telemetry_trace])
def testTracing(self): devtools_client = self._devtools_client if not devtools_client.IsChromeTracingSupported(): self.skipTest('Browser does not support tracing, skipping test.') # Start Chrome tracing. config = tracing_config.TracingConfig() config.enable_chrome_trace = True devtools_client.StartChromeTracing(config) # Stop Chrome tracing and check that the resulting data is valid. builder = trace_data.TraceDataBuilder() devtools_client.StopChromeTracing() devtools_client.CollectChromeTracingData(builder) model.TimelineModel(builder.AsData())
def testImport(self): builder = trace_data.TraceDataBuilder() builder.AddTraceFor(trace_data.INSPECTOR_TRACE_PART, [_BACKGROUND_MESSAGE, _SAMPLE_MESSAGE]) m = model.TimelineModel(builder.AsData(), shift_world_to_zero=False) self.assertEquals(1, len(m.processes)) process = m.processes.values()[0] threads = process.threads self.assertEquals(2, len(threads)) renderer_thread = threads[0] self.assertEquals(1, len(renderer_thread.toplevel_slices)) self.assertEquals('Program', renderer_thread.toplevel_slices[0].name) second_thread = threads['2'] self.assertEquals(1, len(second_thread.toplevel_slices)) self.assertEquals('BeginFrame', second_thread.toplevel_slices[0].name)
def testCollectTracingDataNoTimeout(self): self._inspector_socket.AddEvent( 'Tracing.dataCollected', {'value': {'traceEvents': [{'ph': 'B'}]}}, 9) self._inspector_socket.AddEvent( 'Tracing.dataCollected', {'value': {'traceEvents': [{'ph': 'E'}]}}, 14) self._inspector_socket.AddEvent('Tracing.tracingComplete', {}, 19) backend = tracing_backend.TracingBackend(self._inspector_socket) trace_data_builder = trace_data.TraceDataBuilder() backend._CollectTracingData(trace_data_builder, 10) traces = trace_data_builder.AsData().GetTracesFor( trace_data.CHROME_TRACE_PART) self.assertEqual(2, len(traces)) self.assertEqual(1, len(traces[0].get('traceEvents', []))) self.assertEqual(1, len(traces[1].get('traceEvents', []))) self.assertTrue(backend._has_received_all_tracing_data)
def testOutOfOrderData(self): builder = trace_data.TraceDataBuilder() builder.AddTraceFor(trace_data.INSPECTOR_TRACE_PART, [{ 'startTime': 5295.004, 'endTime': 5305.004, 'data': {}, 'type': 'Program', 'children': [{ 'startTime': 5295.004, 'data': { 'id': 0 }, 'type': 'BeginFrame', }, { 'startTime': 4492.973, 'endTime': 4493.086, 'data': { 'rootNode': -3 }, 'type': 'PaintSetup' }, { 'startTime': 5298.004, 'endTime': 5301.004, 'type': 'Paint', 'frameId': '53228.1', 'data': { 'rootNode': -3, 'clip': [0, 0, 1018, 0, 1018, 764, 0, 764], 'layerId': 10 }, 'children': [] }, { 'startTime': 5301.004, 'endTime': 5305.004, 'data': {}, 'type': 'CompositeLayers', 'children': [] }, { 'startTime': 5305.004, 'data': {}, 'type': 'MarkFirstPaint' }] }]) model.TimelineModel(builder.AsData(), shift_world_to_zero=False)
def testContainsRealProcesses(self): builder = trace_data.TraceDataBuilder() self._agent.StartAgentTracing(self._config, 0) time.sleep(2) self._agent.StopAgentTracing() self._agent.CollectAgentTraceData(builder) builder = builder.AsData() data = json.loads(builder.GetTraceFor(trace_data.CPU_TRACE_DATA)) self.assertTrue(data) for snapshot in data: found_unittest_process = False processes = snapshot['args']['snapshot']['processes'] for process in processes: if 'run_tests' in process['name']: found_unittest_process = True self.assertTrue(found_unittest_process)
def CollectProfile(self): print 'Processing trace...' trace_result_builder = trace_data_module.TraceDataBuilder() self._browser_backend.StopTracing() self._browser_backend.CollectTracingData(trace_result_builder) trace_result = trace_result_builder.AsData() try: trace_file = '%s.html' % self._output_path title = os.path.basename(self._output_path) trace_result.Serialize(trace_file, trace_title=title) finally: trace_result.CleanUpAllTraces() print 'Trace saved as file:///%s' % os.path.abspath(trace_file) return [trace_file]
def CollectProfile(self): self._profiler.communicate(input='\n') trace_result_builder = trace_data_module.TraceDataBuilder() self._browser_backend.StopTracing(trace_result_builder) trace_result = trace_result_builder.AsData() trace_file = StringIO.StringIO() trace_result.Serialize(trace_file) # Merge the chrome and systraces into a zip file. with zipfile.ZipFile(self._output_path, 'w', zipfile.ZIP_DEFLATED) as z: z.writestr('trace.json', trace_file.getvalue()) z.write(self._systrace_output_path, 'systrace') os.unlink(self._systrace_output_path) print 'Systrace saved as %s' % self._output_path print 'To view, open in chrome://tracing' return [self._output_path]
def testCollectTracingDataFromStreamJSONContainer(self): self._inspector_socket.AddEvent( 'Tracing.tracingComplete', {'stream': '42'}, 1) self._inspector_socket.AddAsyncResponse( 'IO.read', {'data': '{"traceEvents": [{},{},{}],'}, 2) self._inspector_socket.AddAsyncResponse( 'IO.read', {'data': '"metadata": {"a": "b"}'}, 3) self._inspector_socket.AddAsyncResponse( 'IO.read', {'data': '}', 'eof': True}, 4) backend = tracing_backend.TracingBackend(self._inspector_socket) trace_data_builder = trace_data.TraceDataBuilder() backend._CollectTracingData(trace_data_builder, 10) data = trace_data_builder.AsData() chrome_trace = data.GetTracesFor(trace_data.CHROME_TRACE_PART)[0] self.assertEqual(3, len(chrome_trace.get('traceEvents', []))) self.assertEqual(dict, type(chrome_trace.get('metadata'))) self.assertTrue(backend._has_received_all_tracing_data)
def CollectProfile(self): print 'Processing trace...' out_json = [] for single_process in self._process_profilers: out_json.extend(single_process.CollectProfile()) trace_data_builder = trace_data_module.TraceDataBuilder() self._browser_backend.browser.StopTracing(trace_data_builder) timeline_model = model.TimelineModel(trace_data_builder.AsData()) out_json.extend(_GenerateTraceMetadata(timeline_model)) with open(self._output_file, 'w') as f: f.write(json.dumps(out_json, separators=(',', ':'))) print 'Trace saved as %s' % self._output_file print 'To view, open in chrome://tracing' return [self._output_file]
def CollectProfile(self): print 'Processing trace...' trace_result_builder = trace_data_module.TraceDataBuilder() self._browser_backend.StopTracing(trace_result_builder) trace_result = trace_result_builder.AsData() trace_file = '%s.zip' % self._output_path with zipfile.ZipFile(trace_file, 'w', zipfile.ZIP_DEFLATED) as z: trace_data = StringIO.StringIO() trace_result.Serialize(trace_data) trace_name = '%s.json' % os.path.basename(self._output_path) z.writestr(trace_name, trace_data.getvalue()) print 'Trace saved as %s' % trace_file print 'To view, open in chrome://tracing' return [trace_file]
def testBrowserProcess(self): builder = trace_data.TraceDataBuilder() builder.AddEventsTo(trace_data.CHROME_TRACE_PART, [{ "name": "process_name", "args": { "name": "Browser" }, "pid": 5, "ph": "M" }, { "name": "thread_name", "args": { "name": "CrBrowserMain" }, "pid": 5, "tid": 32578, "ph": "M" }]) model = model_module.TimelineModel(builder.AsData()) self.assertEquals(5, model.browser_process.pid)
def StopTracing(self): assert self.is_tracing_running, 'Can only stop tracing when tracing is on.' trace_data_builder = trace_data_module.TraceDataBuilder() raised_execption_messages = [] for agent in self._active_agents_instances: try: agent.StopAgentTracing(trace_data_builder) except Exception: raised_execption_messages.append(''.join( traceback.format_exception(*sys.exc_info()))) self._active_agents_instances = [] self._current_config = None if raised_execption_messages: raise TracingControllerStoppedError( 'Exceptions raised when trying to stop tracing:\n' + '\n'.join(raised_execption_messages)) return trace_data_builder.AsData()