예제 #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 test_from_file(self):
     update_systrace_trace_viewer.update(force_update=True)
     self.assertTrue(
         os.path.exists(
             update_systrace_trace_viewer.SYSTRACE_TRACE_VIEWER_HTML_FILE))
     output_file_name = util.generate_random_filename_for_test()
     try:
         # use from-file to create a specific expected output
         run_systrace.main_impl([
             './run_systrace.py', '--from-file', COMPRESSED_ATRACE_DATA,
             '-o', output_file_name
         ])
         # and verify file contents
         with contextlib.nested(open(output_file_name, 'r'),
                                open(DECOMPRESSED_ATRACE_DATA,
                                     'r')) as (f1, f2):
             full_trace = f1.read()
             expected_contents = f2.read()
             self.assertTrue(expected_contents in full_trace)
     except:
         raise
     finally:
         os.remove(
             update_systrace_trace_viewer.SYSTRACE_TRACE_VIEWER_HTML_FILE)
         if os.path.exists(output_file_name):
             os.remove(output_file_name)
예제 #4
0
    def __init__(self, script_dir, options, categories):
        """Constructor.

    Args:
        script_dir: Directory containing the trace viewer script
                    (systrace_trace_viewer.html)
        options: List of command line options.
        categories: List of trace categories to capture.
    """
        # Parse command line arguments and create agents.
        self._script_dir = script_dir
        self._out_filename = options.output_file
        agents = CreateAgents(options)

        # Update trace viewer if necessary.
        try:
            from systrace import update_systrace_trace_viewer
        except ImportError:
            pass
        else:
            update_systrace_trace_viewer.update(self._script_dir)

        # Set up tracing controller.
        self._tracing_controller = tracing_controller.TracingController(
            options, categories, agents)
예제 #5
0
  def __init__(self, script_dir, options, categories):
    """Constructor.

    Args:
        script_dir: Directory containing the trace viewer script
                    (systrace_trace_viewer.html)
        options: List of command line options.
        categories: List of trace categories to capture.
    """
    # Parse command line arguments and create agents.
    self._script_dir = script_dir
    self._out_filename = options.output_file
    agents = CreateAgents(options)

    # Update trace viewer if necessary.
    try:
      from systrace import update_systrace_trace_viewer
    except ImportError:
      pass
    else:
      update_systrace_trace_viewer.update()

    # Set up tracing controller.
    self._tracing_controller = tracing_controller.TracingController(options,
                                                                    categories,
                                                                    agents)
  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)
예제 #7
0
def GenerateHTMLOutput(trace_results, output_file_name):
    """Write the results of systrace to an HTML file.

  Args:
      trace_results: A list of TraceResults.
      output_file_name: The name of the HTML file that the trace viewer
          results should be written to.
  """
    def _ReadAsset(src_dir, filename):
        return open(os.path.join(src_dir, filename)).read()

    systrace_dir = os.path.abspath(os.path.dirname(__file__))

    try:
        from systrace import update_systrace_trace_viewer
    except ImportError:
        pass
    else:
        update_systrace_trace_viewer.update()

    trace_viewer_html = _ReadAsset(systrace_dir, 'systrace_trace_viewer.html')

    # Open the file in binary mode to prevent python from changing the
    # line endings, then write the prefix.
    systrace_dir = os.path.abspath(os.path.dirname(__file__))
    html_prefix = _ReadAsset(systrace_dir, 'prefix.html')
    html_suffix = _ReadAsset(systrace_dir, 'suffix.html')
    trace_viewer_html = _ReadAsset(systrace_dir, 'systrace_trace_viewer.html')

    # Open the file in binary mode to prevent python from changing the
    # line endings, then write the prefix.
    html_file = open(output_file_name, 'wb')
    html_file.write(
        html_prefix.replace('{{SYSTRACE_TRACE_VIEWER_HTML}}',
                            trace_viewer_html))

    # Write the trace data itself. There is a separate section of the form
    # <script class="trace-data" type="application/text"> ... </script>
    # for each tracing agent (including the controller tracing agent).
    html_file.write('<!-- BEGIN TRACE -->\n')
    for result in trace_results:
        if (result.source_name == tracing_controller.TRACE_DATA_CONTROLLER_NAME
                and not OUTPUT_CONTROLLER_TRACE_):
            continue
        html_file.write(
            '  <script class="trace-data" type="application/text">\n')
        html_file.write(_ConvertToHtmlString(result.raw_data))
        html_file.write('  </script>\n')
    html_file.write('<!-- END TRACE -->\n')

    # Write the suffix and finish.
    html_file.write(html_suffix)
    html_file.close()

    final_path = os.path.abspath(output_file_name)
    return final_path
예제 #8
0
def GenerateHTMLOutput(trace_results, output_file_name):
  """Write the results of systrace to an HTML file.

  Args:
      trace_results: A list of TraceResults.
      output_file_name: The name of the HTML file that the trace viewer
          results should be written to.
  """
  def _ReadAsset(src_dir, filename):
    return open(os.path.join(src_dir, filename)).read()

  systrace_dir = os.path.abspath(os.path.dirname(__file__))

  try:
    from systrace import update_systrace_trace_viewer
  except ImportError:
    pass
  else:
    update_systrace_trace_viewer.update()

  trace_viewer_html = _ReadAsset(systrace_dir, 'systrace_trace_viewer.html')

  # Open the file in binary mode to prevent python from changing the
  # line endings, then write the prefix.
  systrace_dir = os.path.abspath(os.path.dirname(__file__))
  html_prefix = _ReadAsset(systrace_dir, 'prefix.html')
  html_suffix = _ReadAsset(systrace_dir, 'suffix.html')
  trace_viewer_html = _ReadAsset(systrace_dir,
                                  'systrace_trace_viewer.html')

  # Open the file in binary mode to prevent python from changing the
  # line endings, then write the prefix.
  html_file = open(output_file_name, 'wb')
  html_file.write(html_prefix.replace('{{SYSTRACE_TRACE_VIEWER_HTML}}',
                                      trace_viewer_html))

  # Write the trace data itself. There is a separate section of the form
  # <script class="trace-data" type="application/text"> ... </script>
  # for each tracing agent (including the controller tracing agent).
  html_file.write('<!-- BEGIN TRACE -->\n')
  for result in trace_results:
    if (result.source_name == tracing_controller.TRACE_DATA_CONTROLLER_NAME and
        not OUTPUT_CONTROLLER_TRACE_):
      continue
    html_file.write('  <script class="trace-data" type="application/text">\n')
    html_file.write(_ConvertToHtmlString(result.raw_data))
    html_file.write('  </script>\n')
  html_file.write('<!-- END TRACE -->\n')

  # Write the suffix and finish.
  html_file.write(html_suffix)
  html_file.close()

  final_path = os.path.abspath(output_file_name)
  return final_path
예제 #9
0
  def test_systrace_trace_viewer(self):
    self.assertEqual(STABLE_VIEWER_PATH,
      update_systrace_trace_viewer.SYSTRACE_TRACE_VIEWER_HTML_FILE)

    update_systrace_trace_viewer.update(force_update=True)

    with open(STABLE_VIEWER_PATH) as f:
      content = f.read().strip()

      # expect big html file
      self.assertGreater(5 * 1024 * 1024, len(content))
      self.assertEqual('<', content[0])
    os.remove(f.name)
예제 #10
0
  def test_systrace_trace_viewer(self):
    self.assertEqual(STABLE_VIEWER_PATH,
      update_systrace_trace_viewer.SYSTRACE_TRACE_VIEWER_HTML_FILE)

    update_systrace_trace_viewer.update(force_update=True)

    with open(STABLE_VIEWER_PATH) as f:
      content = f.read().strip()

      # expect big html file
      self.assertGreater(5 * 1024 * 1024, len(content))
      self.assertEqual('<', content[0])
    os.remove(f.name)
예제 #11
0
  def testJsonTraceMerging(self):
    update_systrace_trace_viewer.update(force_update=True)
    self.assertTrue(os.path.exists(
        update_systrace_trace_viewer.SYSTRACE_TRACE_VIEWER_HTML_FILE))
    t1 = "{'traceEvents': [{'ts': 123, 'ph': 'b'}]}"
    t2 = "{'traceEvents': [], 'stackFrames': ['blah']}"
    results = [trace_result.TraceResult('a', t1),
               trace_result.TraceResult('b', t2)]

    merged_results = output_generator.MergeTraceResultsIfNeeded(results)
    for r in merged_results:
      if r.source_name == 'a':
        self.assertEquals(r.raw_data, t1)
      elif r.source_name == 'b':
        self.assertEquals(r.raw_data, t2)
    self.assertEquals(len(merged_results), len(results))
    os.remove(update_systrace_trace_viewer.SYSTRACE_TRACE_VIEWER_HTML_FILE)
  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)]
    with tempfile_ext.TemporaryFileName() as output_file_name:
      output_generator.GenerateHTMLOutput(trace_results, output_file_name)
      with open(output_file_name, 'r') as f:
        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)
    os.remove(update_systrace_trace_viewer.SYSTRACE_TRACE_VIEWER_HTML_FILE)
예제 #13
0
 def test_from_file(self):
   update_systrace_trace_viewer.update(force_update=True)
   self.assertTrue(os.path.exists(
       update_systrace_trace_viewer.SYSTRACE_TRACE_VIEWER_HTML_FILE))
   try:
     with tempfile_ext.TemporaryFileName() as output_file_name:
       # use from-file to create a specific expected output
       run_systrace.main_impl(['./run_systrace.py',
                               '--from-file',
                               COMPRESSED_ATRACE_DATA,
                               '-o',
                               output_file_name])
       # and verify file contents
       with contextlib.nested(open(output_file_name, 'r'),
                              open(DECOMPRESSED_ATRACE_DATA, 'r')) as (f1, f2):
         full_trace = f1.read()
         expected_contents = f2.read()
         self.assertTrue(expected_contents in full_trace)
   finally:
     os.remove(update_systrace_trace_viewer.SYSTRACE_TRACE_VIEWER_HTML_FILE)
 def test_default_output_filename(self):
   update_systrace_trace_viewer.update(force_update=True)
   self.assertTrue(os.path.exists(
       update_systrace_trace_viewer.SYSTRACE_TRACE_VIEWER_HTML_FILE))
   output_file_name = os.path.join(TEST_DIR, 'compressed_atrace_data.html')
   try:
     # use from-file to create a specific expected output
     run_systrace.main_impl(['./run_systrace.py',
                             '--from-file',
                             COMPRESSED_ATRACE_DATA])
     # and verify file contents
     with contextlib.nested(open(output_file_name, 'r'),
                            open(DECOMPRESSED_ATRACE_DATA, 'r')) as (f1, f2):
       full_trace = f1.read()
       expected_contents = f2.read()
       self.assertTrue(expected_contents in full_trace)
   except:
     raise
   finally:
     os.remove(update_systrace_trace_viewer.SYSTRACE_TRACE_VIEWER_HTML_FILE)
     if os.path.exists(output_file_name):
       os.remove(output_file_name)
예제 #15
0
def GenerateHTMLOutput(trace_results, output_file_name):
  """Write the results of systrace to an HTML file.

  Args:
      trace_results: A list of TraceResults.
      output_file_name: The name of the HTML file that the trace viewer
          results should be written to.
  """
  def _ReadAsset(src_dir, filename):
    return open(os.path.join(src_dir, filename)).read()

  # TODO(rnephew): The tracing output formatter is able to handle a single
  # systrace trace just as well as it handles multiple traces. The obvious thing
  # to do here would be to use it all for all systrace output: however, we want
  # to continue using the legacy way of formatting systrace output when a single
  # systrace and the tracing controller trace are present in order to match the
  # Java verison of systrace. Java systrace is expected to be deleted at a later
  # date. We should consolidate this logic when that happens.

  if len(trace_results) > 3:
    NewGenerateHTMLOutput(trace_results, output_file_name)
    return os.path.abspath(output_file_name)

  systrace_dir = os.path.abspath(os.path.dirname(__file__))

  try:
    from systrace import update_systrace_trace_viewer
  except ImportError:
    pass
  else:
    update_systrace_trace_viewer.update()

  trace_viewer_html = _ReadAsset(systrace_dir, 'systrace_trace_viewer.html')

  # Open the file in binary mode to prevent python from changing the
  # line endings, then write the prefix.
  systrace_dir = os.path.abspath(os.path.dirname(__file__))
  html_prefix = _ReadAsset(systrace_dir, 'prefix.html')
  html_suffix = _ReadAsset(systrace_dir, 'suffix.html')
  trace_viewer_html = _ReadAsset(systrace_dir,
                                  'systrace_trace_viewer.html')

  # Open the file in binary mode to prevent python from changing the
  # line endings, then write the prefix.
  html_file = open(output_file_name, 'wb')
  html_file.write(html_prefix.replace('{{SYSTRACE_TRACE_VIEWER_HTML}}',
                                      trace_viewer_html))

  # Write the trace data itself. There is a separate section of the form
  # <script class="trace-data" type="application/text"> ... </script>
  # for each tracing agent (including the controller tracing agent).
  html_file.write('<!-- BEGIN TRACE -->\n')
  for result in trace_results:
    html_file.write('  <script class="trace-data" type="application/text">\n')
    html_file.write(_ConvertToHtmlString(result.raw_data))
    html_file.write('  </script>\n')
  html_file.write('<!-- END TRACE -->\n')

  # Write the suffix and finish.
  html_file.write(html_suffix)
  html_file.close()

  final_path = os.path.abspath(output_file_name)
  return final_path