Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / results / chart_json_output_formatter.py
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
5 import collections
6 import itertools
7 import json
8
9 from telemetry.results import output_formatter
10 from telemetry.value import summary as summary_module
11
12 def _ResultsAsChartDict(benchmark_metadata, page_specific_values,
13                        summary_values):
14   """Produces a dict for serialization to Chart JSON format from raw values.
15
16   Chart JSON is a transformation of the basic Telemetry JSON format that
17   removes the page map, summarizes the raw values, and organizes the results
18   by chart and trace name. This function takes the key pieces of data needed to
19   perform this transformation (namely, lists of values and a benchmark metadata
20   object) and processes them into a dict which can be serialized using the json
21   module.
22
23   Design doc for schema: http://goo.gl/kOtf1Y
24
25   Args:
26     page_specific_values: list of page-specific values
27     summary_values: list of summary values
28     benchmark_metadata: a benchmark.BenchmarkMetadata object
29
30   Returns:
31     A Chart JSON dict corresponding to the given data.
32   """
33   summary = summary_module.Summary(page_specific_values)
34   values = itertools.chain(
35       summary.interleaved_computed_per_page_values_and_summaries,
36       summary_values)
37   charts = collections.defaultdict(dict)
38
39   for value in values:
40     if value.page:
41       chart_name, trace_name = (value.GetChartAndTraceNameForPerPageResult())
42     else:
43       chart_name, trace_name = (
44           value.GetChartAndTraceNameForComputedSummaryResult(None))
45       if chart_name == trace_name:
46         trace_name = 'summary'
47
48     # This intentionally overwrites the trace if it already exists because this
49     # is expected of output from the buildbots currently.
50     # See: crbug.com/413393
51     charts[chart_name][trace_name] = value.AsDict()
52
53   result_dict = {
54     'format_version': '0.1',
55     'benchmark_name': benchmark_metadata.name,
56     'charts': charts
57   }
58
59   return result_dict
60
61 # TODO(eakuefner): Transition this to translate Telemetry JSON.
62 class ChartJsonOutputFormatter(output_formatter.OutputFormatter):
63   def __init__(self, output_stream, benchmark_metadata):
64     super(ChartJsonOutputFormatter, self).__init__(output_stream)
65     self._benchmark_metadata = benchmark_metadata
66
67   def Format(self, page_test_results):
68     json.dump(_ResultsAsChartDict(
69         self._benchmark_metadata,
70         page_test_results.all_page_specific_values,
71         page_test_results.all_summary_values),
72               self.output_stream)
73     self.output_stream.write('\n')