1# Copyright 2014 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import os
6
7from telemetry.value import summary as summary_module
8
9class OutputFormatter(object):
10  """A formatter for PageTestResults.
11
12  An OutputFormatter takes PageTestResults, formats the results
13  (telemetry.value.Value instances), and output the formatted results
14  in the given output stream.
15
16  Examples of output formatter: CsvOutputFormatter produces results in
17  CSV format."""
18
19  def __init__(self, output_stream):
20    """Constructs a new formatter that writes to the output_stream.
21
22    Args:
23      output_stream: The stream to write the formatted output to.
24    """
25    self._output_stream = output_stream
26
27  def Format(self, page_test_results):
28    """Formats the given PageTestResults into the output stream.
29
30    This will be called once at the end of a benchmark.
31
32    Args:
33      page_test_results: A PageTestResults object containing all results
34         from the current benchmark run.
35    """
36    raise NotImplementedError()
37
38  def PrintViewResults(self):
39    print 'View result at file://' + os.path.abspath(self.output_stream.name)
40
41  def FormatDisabled(self):
42    """Formats disabled results into the output stream.
43
44    This will be called once when a benchmark is run but disabled.
45    """
46    pass
47
48  @property
49  def output_stream(self):
50    return self._output_stream
51
52
53def SummarizePageSpecificValues(page_specific_values):
54  """Summarize results appropriately for TBM and legacy benchmarks.
55
56  For benchmarks that are timeline-based, we need to summarize not once, but
57  twice, once by name and tir_label (default) and again by name only. But for
58  benchmarks that are not timeline-based, since no tir_labels are set, we will
59  end up duplicating values.
60
61  Thus, we only want to summarize once if the benchmark is not timeline-based,
62  but twice, using the two different key functions, otherwise.
63  """
64  # Default summary uses merge_values.DefaultKeyFunc to summarize both by name
65  # and tir_label.
66  summary = summary_module.Summary(page_specific_values)
67  values = summary.interleaved_computed_per_page_values_and_summaries
68
69  if any(v.tir_label for v in page_specific_values):
70    summary_by_name_only = summary_module.Summary(
71        page_specific_values,
72        key_func=lambda v: v.name)
73    values.extend(
74        summary_by_name_only.interleaved_computed_per_page_values_and_summaries
75    )
76  return values
77