133259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck# Copyright 2014 The Chromium Authors. All rights reserved.
233259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck# Use of this source code is governed by a BSD-style license that can be
333259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck# found in the LICENSE file.
433259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck
533259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reckimport os
633259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck
733259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reckfrom telemetry.value import summary as summary_module
833259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck
933259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reckclass OutputFormatter(object):
1033259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck  """A formatter for PageTestResults.
1133259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck
1233259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck  An OutputFormatter takes PageTestResults, formats the results
1333259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck  (telemetry.value.Value instances), and output the formatted results
1433259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck  in the given output stream.
1533259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck
1633259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck  Examples of output formatter: CsvOutputFormatter produces results in
1733259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck  CSV format."""
1833259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck
1933259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck  def __init__(self, output_stream):
2033259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck    """Constructs a new formatter that writes to the output_stream.
2133259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck
2233259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck    Args:
2333259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck      output_stream: The stream to write the formatted output to.
2433259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck    """
2533259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck    self._output_stream = output_stream
2633259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck
2733259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck  def Format(self, page_test_results):
2833259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck    """Formats the given PageTestResults into the output stream.
2933259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck
3033259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck    This will be called once at the end of a benchmark.
3133259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck
3233259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck    Args:
3333259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck      page_test_results: A PageTestResults object containing all results
3433259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck         from the current benchmark run.
3533259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck    """
3633259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck    raise NotImplementedError()
3733259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck
3833259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck  def PrintViewResults(self):
3933259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck    print 'View result at file://' + os.path.abspath(self.output_stream.name)
4033259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck
4133259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck  def FormatDisabled(self):
4233259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck    """Formats disabled results into the output stream.
4333259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck
4433259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck    This will be called once when a benchmark is run but disabled.
4533259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck    """
4633259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck    pass
4733259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck
4833259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck  @property
4933259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck  def output_stream(self):
5033259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck    return self._output_stream
5133259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck
5233259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck
5333259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reckdef SummarizePageSpecificValues(page_specific_values):
5433259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck  """Summarize results appropriately for TBM and legacy benchmarks.
5533259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck
5633259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck  For benchmarks that are timeline-based, we need to summarize not once, but
5733259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck  twice, once by name and tir_label (default) and again by name only. But for
5833259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck  benchmarks that are not timeline-based, since no tir_labels are set, we will
5933259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck  end up duplicating values.
6033259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck
6133259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck  Thus, we only want to summarize once if the benchmark is not timeline-based,
6233259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck  but twice, using the two different key functions, otherwise.
6333259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck  """
6433259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck  # Default summary uses merge_values.DefaultKeyFunc to summarize both by name
6533259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck  # and tir_label.
6633259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck  summary = summary_module.Summary(page_specific_values)
6733259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck  values = summary.interleaved_computed_per_page_values_and_summaries
6833259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck
6933259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck  if any(v.tir_label for v in page_specific_values):
7033259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck    summary_by_name_only = summary_module.Summary(
7133259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck        page_specific_values,
7233259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck        key_func=lambda v: v.name)
7333259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck    values.extend(
7433259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck        summary_by_name_only.interleaved_computed_per_page_values_and_summaries
7533259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck    )
7633259e44c8229f70ffe0cf3bb5ca9375c4feb2f9John Reck  return values
77