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 testOpenTraceHandleFor(self): original_data = {'msg': 'The answer is 42'} with trace_data.TraceDataBuilder() as builder: with builder.OpenTraceHandleFor(trace_data.CHROME_TRACE_PART) as handle: handle.write(json.dumps(original_data)) out_data = builder.AsData().GetTraceFor(trace_data.CHROME_TRACE_PART) # Trace handle should be cleaned up. self.assertFalse(os.path.exists(handle.name)) self.assertEqual(original_data, out_data)
def testAddTraceFileFor(self): original_data = {'msg': 'The answer is 42'} with tempfile.NamedTemporaryFile(delete=False) as source: json.dump(original_data, source) with trace_data.TraceDataBuilder() as builder: builder.AddTraceFileFor(trace_data.CHROME_TRACE_PART, source.name) self.assertFalse(os.path.exists(source.name)) out_data = builder.AsData().GetTraceFor(trace_data.CHROME_TRACE_PART) self.assertEqual(original_data, out_data)
def testBrowserProcess(self): builder = trace_data.TraceDataBuilder() builder.AddTraceFor(trace_data.CHROME_TRACE_PART, { "traceEvents": [ {"name": "process_name", "args": {"name": "Browser"}, "pid": 5, "ph": "M"}, {"name": "thread_name", "args": {"name": "CrBrowserMain"}, "pid": 5, "tid": 32578, "ph": "M"}]}) domain.model = model_module.TimelineModel(builder.AsData()) self.assertEquals(5, domain.model.browser_process.pid)
def testHtmlOutputGenerationFormatsMultipleTraces(self): trace_results = [] with trace_data_module.TraceDataBuilder() as trace_data_builder: with open(ATRACE_DATA) as fp: atrace_data = fp.read() trace_results.append( trace_result.TraceResult('systemTraceEvents', atrace_data)) trace_data_builder.AddTraceFor(trace_data_module.ATRACE_PART, atrace_data, allow_unstructured=True) with open(ATRACE_PROCESS_DUMP_DATA) as fp: atrace_process_dump_data = fp.read() trace_results.append( trace_result.TraceResult('atraceProcessDump', atrace_process_dump_data)) trace_data_builder.AddTraceFor( trace_data_module.ATRACE_PROCESS_DUMP_PART, atrace_process_dump_data, allow_unstructured=True) with open(CGROUP_DUMP_DATA) as fp: cgroup_dump_data = fp.read() trace_results.append( trace_result.TraceResult('cgroupDump', cgroup_dump_data)) trace_data_builder.AddTraceFor(trace_data_module.CGROUP_TRACE_PART, cgroup_dump_data, allow_unstructured=True) with open(COMBINED_PROFILE_CHROME_DATA) as fp: chrome_data = json.load(fp) trace_results.append( trace_result.TraceResult('traceEvents', chrome_data)) trace_data_builder.AddTraceFor(trace_data_module.CHROME_TRACE_PART, chrome_data) trace_results.append( trace_result.TraceResult('systraceController', str({}))) trace_data_builder.AddTraceFor(trace_data_module.TELEMETRY_PART, {}) with tempfile_ext.NamedTemporaryDirectory() as temp_dir: data_builder_out = os.path.join(temp_dir, 'data_builder.html') output_generator_out = os.path.join(temp_dir, 'output_generator.html') output_generator.GenerateHTMLOutput(trace_results, output_generator_out) trace_data_builder.Serialize(data_builder_out, 'Systrace') output_generator_md5sum = hashlib.md5( open(output_generator_out, 'rb').read()).hexdigest() data_builder_md5sum = hashlib.md5( open(data_builder_out, 'rb').read()).hexdigest() self.assertEqual(output_generator_md5sum, data_builder_md5sum)
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.AddTraceFor(trace_data.INSPECTOR_TRACE_PART, events) return model.TimelineModel(builder.AsData(), shift_world_to_zero=False)
def testHtmlOutputGenerationFormatsMultipleTraces(self): trace_results = [] trace_data_builder = trace_data_module.TraceDataBuilder() with open(BATTOR_DATA) as fp: battor_data = fp.read() trace_results.append( trace_result.TraceResult('powerTraceAsString', battor_data)) trace_data_builder.AddTraceFor(trace_data_module.BATTOR_TRACE_PART, battor_data) with open(ATRACE_DATA) as fp: atrace_data = fp.read() trace_results.append( trace_result.TraceResult('systemTraceEvents', atrace_data)) trace_data_builder.AddTraceFor(trace_data_module.ATRACE_PART, atrace_data) with open(COMBINED_PROFILE_CHROME_DATA) as fp: chrome_data = fp.read() trace_results.append( trace_result.TraceResult('traceEvents', json.loads(chrome_data))) trace_data_builder.AddTraceFor(trace_data_module.CHROME_TRACE_PART, json.loads(chrome_data)) trace_results.append( trace_result.TraceResult('systraceController', str({}))) trace_data_builder.AddTraceFor(trace_data_module.TELEMETRY_PART, {}) try: data_builder_out = util.generate_random_filename_for_test() output_generator_out = util.generate_random_filename_for_test() output_generator.GenerateHTMLOutput(trace_results, output_generator_out) trace_data_builder.AsData().Serialize(data_builder_out, 'Systrace') output_generator_md5sum = hashlib.md5( open(output_generator_out, 'rb').read()).hexdigest() data_builder_md5sum = hashlib.md5( open(data_builder_out, 'rb').read()).hexdigest() self.assertEqual(output_generator_md5sum, data_builder_md5sum) finally: def del_if_exist(path): try: os.remove(path) except IOError: pass del_if_exist(output_generator_out) del_if_exist(data_builder_out)
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 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 = 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.assertTrue(backend._has_received_all_tracing_data)
def testOpenTraceHandleForCompressedData(self): original_data = {'msg': 'The answer is 42'} # gzip.compress() does not work in python 2, so hardcode the encoded data. compressed_data = base64.b64decode( 'H4sIAIDMblwAA6tWyi1OV7JSUArJSFVIzCsuTy1SyCxWMDFSquUCAA4QMtscAAAA') with trace_data.TraceDataBuilder() as builder: with builder.OpenTraceHandleFor( trace_data.CHROME_TRACE_PART, compressed=True) as handle: handle.write(compressed_data) out_data = builder.AsData().GetTraceFor(trace_data.CHROME_TRACE_PART) # Trace handle should be cleaned up. self.assertFalse(os.path.exists(handle.name)) self.assertEqual(original_data, out_data)
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 = self._GetRawChromeTracesFor(trace_data_builder)[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 testWriteTelemetryInfo(self): info = page_test_results.TelemetryInfo() info.benchmark_name = 'example' info.benchmark_start_epoch = 0 self.agent.StartAgentTracing(self.config, timeout=10) telemetry_tracing_agent.SetTelemetryInfo(info) self.agent.StopAgentTracing() with trace_data.TraceDataBuilder() as builder: self.agent.CollectAgentTraceData(builder) trace = builder.AsData().GetTraceFor(trace_data.TELEMETRY_PART) benchmarks = trace['metadata']['telemetry']['benchmarks'] self.assertEqual(len(benchmarks), 1) self.assertEqual(benchmarks[0], 'example')
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 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.GetTracesFor(trace_data.CPU_TRACE_DATA)[0]) self.assertTrue(data) self.assertEquals(set(data[0].keys()), set(TRACE_EVENT_KEYS)) self.assertEquals(set(data[0]['args']['snapshot'].keys()), set(['processes'])) self.assertTrue(data[0]['args']['snapshot']['processes']) self.assertEquals(set(data[0]['args']['snapshot']['processes'][0].keys()), set(SNAPSHOT_KEYS))
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 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) with trace_data.TraceDataBuilder() as builder: backend._CollectTracingData(builder, 10) trace = builder.AsData().GetTraceFor(trace_data.CHROME_TRACE_PART) self.assertEqual(5, len(trace['traceEvents'])) self.assertTrue(backend._has_received_all_tracing_data)
def testWriteBenchmarkMetadata(self): story = test_stories.DummyStory('story') with page_test_results.PageTestResults( benchmark_name='benchmark_name') as results: with results.CreateStoryRun(story): self.agent.StartAgentTracing(self.config, timeout=10) telemetry_tracing_agent.RecordBenchmarkMetadata(results) self.agent.StopAgentTracing() with trace_data.TraceDataBuilder() as builder: self.agent.CollectAgentTraceData(builder) trace = builder.AsData().GetTraceFor( trace_data.TELEMETRY_PART) benchmarks = trace['metadata']['telemetry']['benchmarks'] self.assertEqual(len(benchmarks), 1) self.assertEqual(benchmarks[0], 'benchmark_name')
def testCollectAgentTraceDataFormat(self): self._agent.StartAgentTracing(self._config, 0) time.sleep(2) self._agent.StopAgentTracing() with trace_data.TraceDataBuilder() as builder: self._agent.CollectAgentTraceData(builder) data = builder.AsData().GetTraceFor( trace_data.CPU_TRACE_DATA)['traceEvents'] self.assertEquals(set(data[0].keys()), set(TRACE_EVENT_KEYS)) self.assertEquals(set(data[0]['args']['snapshot'].keys()), set(['processes'])) self.assertTrue(data[0]['args']['snapshot']['processes']) self.assertEquals(set(data[0]['args']['snapshot']['processes'][0].keys()), set(SNAPSHOT_KEYS))
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) with trace_data.TraceDataBuilder() as builder: backend._CollectTracingData(builder, 10) traces = 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 = builder.GetTraceFor(trace_data.CPU_TRACE_DATA)['traceEvents'] 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 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) chrome_trace = self._GetRawChromeTracesFor(trace_data_builder)[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): 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 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 testWriteBenchmarkMetadata(self): results = page_test_results.PageTestResults( benchmark_name='benchmark', benchmark_description='desc') story = mock.Mock() story.name = 'story' story.GetStoryTagsList.return_value = ['tag1', 'tag2'] results.WillRunPage(story) self.agent.StartAgentTracing(self.config, timeout=10) telemetry_tracing_agent.RecordBenchmarkMetadata(results) self.agent.StopAgentTracing() results.DidRunPage(story) with trace_data.TraceDataBuilder() as builder: self.agent.CollectAgentTraceData(builder) trace = builder.AsData().GetTraceFor(trace_data.TELEMETRY_PART) benchmarks = trace['metadata']['telemetry']['benchmarks'] self.assertEqual(len(benchmarks), 1) self.assertEqual(benchmarks[0], 'benchmark')
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 = domain.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 testWriteTelemetryInfo(self): info = page_test_results.TelemetryInfo() info.benchmark_start_epoch = 1 info._trace_start_us = 2000 info.benchmark_name = 'benchmark' info.benchmark_descriptions = 'desc' info._story_name = 'story' info._story_tags = ['tag1', 'tag2'] info._storyset_repeat_counter = 0 self.agent.StartAgentTracing(self.config, timeout=10) telemetry_tracing_agent.SetTelemetryInfo(info) self.agent.StopAgentTracing() with trace_data.TraceDataBuilder() as builder: self.agent.CollectAgentTraceData(builder) trace = builder.AsData().GetTraceFor(trace_data.TELEMETRY_PART) benchmarks = trace['metadata']['telemetry']['benchmarks'] self.assertEqual(len(benchmarks), 1) self.assertEqual(benchmarks[0], 'benchmark')
def testSetTraceForRaisesWithInvalidTrace(self): builder = trace_data.TraceDataBuilder() self.assertRaises( exceptions.AssertionError, lambda: builder.AddTraceFor( trace_data.TELEMETRY_PART, datetime.time.min))