예제 #1
0
    def testHtmlOutputGenerationFormatsSingleTrace(self):
        update_systrace_trace_viewer.update(force_update=True)
        self.assertTrue(
            os.path.exists(
                update_systrace_trace_viewer.SYSTRACE_TRACE_VIEWER_HTML_FILE))
        with open(ATRACE_DATA) as f:
            atrace_data = f.read().replace(" ", "").strip()
            trace_results = [
                trace_result.TraceResult('systemTraceEvents', atrace_data)
            ]
            output_file_name = util.generate_random_filename_for_test()
            final_path = output_generator.GenerateHTMLOutput(
                trace_results, output_file_name)
            with open(output_file_name, 'r') as f:
                output_generator.GenerateHTMLOutput(trace_results, f.name)
                html_output = f.read()
                trace_data = (html_output.split(
                    '<script class="trace-data" type="application/text">')
                              [1].split('</script>'))[0].replace(" ",
                                                                 "").strip()
            os.remove(final_path)

        # Ensure the trace data written in HTML is located within the
        # correct place in the HTML document and that the data is not
        # malformed.
        self.assertEquals(trace_data, atrace_data)
        os.remove(update_systrace_trace_viewer.SYSTRACE_TRACE_VIEWER_HTML_FILE)
예제 #2
0
    def testHtmlOutputGenerationFormatsMultipleTraces(self):
        update_systrace_trace_viewer.update(force_update=True)
        self.assertTrue(
            os.path.exists(
                update_systrace_trace_viewer.SYSTRACE_TRACE_VIEWER_HTML_FILE))
        json_data = open(COMBINED_PROFILE_CHROME_DATA).read()
        combined_data = json.loads(json_data)
        trace_results = []
        trace_results_expected = []
        for (trace_name, data) in combined_data.iteritems():
            trace_results.append(
                trace_result.TraceResult(str(trace_name), str(data)))
            trace_results_expected.append(str(data).replace(" ", "").strip())
        output_file_name = util.generate_random_filename_for_test()
        final_path = output_generator.GenerateHTMLOutput(
            trace_results, output_file_name)
        with open(output_file_name, 'r') as f:
            html_output = f.read()
            for i in range(1, len(trace_results)):
                trace_data = (html_output.split(
                    '<script class="trace-data" type="application/text">')
                              [i].split('</script>'))[0].replace(" ",
                                                                 "").strip()

                # Ensure the trace data written in HTML is located within the
                # correct place in the HTML document and that the data is not
                # malformed.
                self.assertTrue(trace_data in trace_results_expected)
        os.remove(final_path)
        os.remove(update_systrace_trace_viewer.SYSTRACE_TRACE_VIEWER_HTML_FILE)
    def testHtmlOutputGenerationFormatsMultipleTraces(self):
        json_data = open(COMBINED_PROFILE_CHROME_DATA).read()
        combined_data = json.loads(json_data)
        trace_results = []
        trace_results_expected = []
        for (trace_name, data) in combined_data.iteritems():
            trace_results.append(
                trace_result.TraceResult(str(trace_name), str(data)))
            trace_results_expected.append(str(data).replace(" ", "").strip())
        output_file_name = ''.join(
            random.choice(string.ascii_uppercase + string.digits)
            for _ in range(10))
        output_generator.GenerateHTMLOutput(trace_results, output_file_name)
        with open(output_file_name, 'r') as f:
            html_output = f.read()
            for i in range(1, len(trace_results)):
                trace_data = (html_output.split(
                    '<script class="trace-data" type="application/text">')
                              [i].split('</script>'))[0].replace(" ",
                                                                 "").strip()

                # Ensure the trace data written in HTML is located within the
                # correct place in the HTML document and that the data is not
                # malformed.
                self.assertTrue(trace_data in trace_results_expected)
예제 #4
0
def _PullTraces(controllers, output, compress, write_json):
    ui.PrintMessage('Downloading...', eol='')
    trace_files = [controller.PullTrace() for controller in controllers]
    trace_files = [trace for trace in trace_files if trace]
    if not trace_files:
        ui.PrintMessage('No results')
        return ''

    trace_files = output_generator.MergeTraceFilesIfNeeded(trace_files)
    if not write_json:
        print 'Writing trace HTML'
        html_file = os.path.splitext(trace_files[0])[0] + '.html'
        trace_results = _PrepareTracesForOutput(trace_files)
        result = output_generator.GenerateHTMLOutput(trace_results, html_file)
        print '\nWrote file://%s\n' % result
        trace_files = [html_file]
    if compress and len(trace_files) == 1:
        result = output or trace_files[0] + '.gz'
        util.CompressFile(trace_files[0], result)
    elif len(trace_files) > 1:
        result = (output
                  or 'chrome-combined-trace-%s.zip' % util.GetTraceTimestamp())
        util.ArchiveFiles(trace_files, result)
    elif output:
        result = output
        shutil.move(trace_files[0], result)
    else:
        result = trace_files[0]

    return result
예제 #5
0
    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)
예제 #6
0
    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 testHtmlOutputGenerationFormatsSingleTrace(self):
        with open(ATRACE_DATA) as f:
            atrace_data = f.read().replace(" ", "").strip()
            trace_results = [
                trace_result.TraceResult('systemTraceEvents', atrace_data)
            ]
            output_file_name = ''.join(
                random.choice(string.ascii_uppercase + string.digits)
                for _ in range(10))
            output_generator.GenerateHTMLOutput(trace_results,
                                                output_file_name)
            with open(output_file_name, 'r') as f:
                output_generator.GenerateHTMLOutput(trace_results, f.name)
                html_output = f.read()
                trace_data = (html_output.split(
                    '<script class="trace-data" type="application/text">')
                              [1].split('</script>'))[0].replace(" ",
                                                                 "").strip()

        # Ensure the trace data written in HTML is located within the
        # correct place in the HTML document and that the data is not
        # malformed.
        self.assertEquals(trace_data, atrace_data)
예제 #8
0
def _GetResults(trace_results, controller, output, compress, write_json,
                interval):
    ui.PrintMessage('Downloading...')

    # Wait for the trace file to get written.
    time.sleep(1)

    for agent in controller.get_child_agents:
        if isinstance(agent, chrome_tracing_agent.ChromeTracingAgent):
            time.sleep(interval / 4)

    # Ignore the systraceController because it will not contain any results,
    # instead being in charge of collecting results.
    trace_results = [
        x for x in controller.all_results
        if not (x.source_name == 'systraceController')
    ]

    if not trace_results:
        ui.PrintMessage('No results')
        return ''

    result = None
    trace_results = output_generator.MergeTraceResultsIfNeeded(trace_results)
    if not write_json:
        ui.PrintMessage('Writing trace HTML...')
        html_file = output or trace_results[0].source_name + '.html'
        result = output_generator.GenerateHTMLOutput(trace_results, html_file)
        ui.PrintMessage('\nWrote file://%s' % result)
    elif compress and len(trace_results) == 1:
        result = output or trace_results[0].source_name + '.gz'
        util.WriteDataToCompressedFile(trace_results[0].raw_data, result)
    elif len(trace_results) > 1:
        result = (output
                  or 'chrome-combined-trace-%s.zip' % util.GetTraceTimestamp())
        util.ArchiveData(trace_results, result)
    elif output:
        result = output
        with open(result, 'wb') as f:
            f.write(trace_results[0].raw_data)
    else:
        result = trace_results[0].source_name
        with open(result, 'wb') as f:
            f.write(trace_results[0].raw_data)

    return result
예제 #9
0
    def OutputSystraceResults(self, write_json=False):
        """Output the results of systrace to a file.

    If output is necessary, then write the results of systrace to either (a)
    a standalone HTML file, or (b) a json file which can be read by the
    trace viewer.

    Args:
       write_json: Whether to output to a json file (if false, use HTML file)
    """
        print 'Tracing complete, writing results'
        if write_json:
            result = output_generator.GenerateJSONOutput(
                self._tracing_controller.all_results, self._out_filename)
        else:
            result = output_generator.GenerateHTMLOutput(
                self._tracing_controller.all_results, self._out_filename)
        print '\nWrote trace %s file: file://%s\n' % (
            ('JSON' if write_json else 'HTML'), result)
예제 #10
0
파일: profiler.py 프로젝트: fanarm/catapult
def _GetResults(agents, output, compress, write_json, interval):
    ui.PrintMessage('Downloading...', eol='')

    # Wait for the trace file to get written.
    time.sleep(1)

    trace_results = []
    for agent in agents:
        if isinstance(agent, chrome_tracing_agent.ChromeTracingAgent):
            time.sleep(interval / 4)
        trace_results.append(agent.GetResults())

    if not trace_results:
        ui.PrintMessage('No results')
        return ''

    result = None
    trace_results = output_generator.MergeTraceResultsIfNeeded(trace_results)
    if not write_json:
        print 'Writing trace HTML'
        html_file = trace_results[0].source_name + '.html'
        result = output_generator.GenerateHTMLOutput(trace_results, html_file)
        print '\nWrote file://%s\n' % result
    elif compress and len(trace_results) == 1:
        result = output or trace_results[0].source_name + '.gz'
        util.WriteDataToCompressedFile(trace_results[0].raw_data, result)
    elif len(trace_results) > 1:
        result = (output
                  or 'chrome-combined-trace-%s.zip' % util.GetTraceTimestamp())
        util.ArchiveData(trace_results, result)
    elif output:
        result = output
        with open(result, 'wb') as f:
            f.write(trace_results[0].raw_data)
    else:
        result = trace_results[0].source_name
        with open(result, 'wb') as f:
            f.write(trace_results[0].raw_data)

    return result