def LoadTraces(chrome_trace_filename): """Load traces from a file and return them as a python list. There are several tools in tracing/bin that deal with reading traces from a file. None of them does exactly what we need here. In particular, merge_traces.LoadTrace will discard all traces but one if an HTML file has several traces. TODO(chiniforooshan): create a module for reading/writing different trace file formats and make every other tool use that module. """ traces = [] if chrome_trace_filename.endswith(GZIP_FILENAME_SUFFIX): with gzip.open(chrome_trace_filename, 'rb') as f: traces.append(json.load(f)) elif chrome_trace_filename.endswith(HTML_FILENAME_SUFFIX): with codecs.open(chrome_trace_filename, mode='r', encoding='utf-8') as f: traces = html2trace.ReadTracesFromHTMLFile(f) elif chrome_trace_filename.endswith(JSON_FILENAME_SUFFIX): with open(chrome_trace_filename, 'r') as f: traces.append(json.load(f)) else: raise Exception('Unknown trace file suffix: %s', chrome_trace_filename) return map(_ConvertToDictIfNecessary, traces)
def testSerialize(self): test_dir = tempfile.mkdtemp() trace_path = os.path.join(test_dir, 'test_trace.json') try: ri = trace_data.CreateTraceDataFromRawData({'traceEvents': [1, 2, 3]}) ri.Serialize(trace_path) with open(trace_path) as f: json_traces = html2trace.ReadTracesFromHTMLFile(f) self.assertEqual(json_traces, [{'traceEvents': [1, 2, 3]}]) finally: shutil.rmtree(test_dir)
def LoadHTMLTrace(filename): """Load a trace from a vulcanized HTML trace file.""" trace_components = collections.defaultdict(list) with open(filename) as file_handle: for sub_trace in html2trace.ReadTracesFromHTMLFile(file_handle): for name, component in TraceAsDict(sub_trace).items(): trace_components[name].append(component) trace = {} for name, components in trace_components.items(): if len(components) == 1: trace[name] = components[0] elif all(isinstance(component, list) for component in components): trace[name] = [e for component in components for e in component] else: trace[name] = components[0] logging.warning( 'Values of repeated trace component %r in HTML trace %r are not ' 'lists. The first defined value of the component will be used.', filename, name) return trace
def ExtractTracesFromFile(self, trace_file_handle): return html2trace.ReadTracesFromHTMLFile(trace_file_handle)