Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / value / string.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 from telemetry import value as value_module
6 from telemetry.value import list_of_string_values
7
8
9 class StringValue(value_module.Value):
10   def __init__(self, page, name, units, value, important=True,
11                description=None):
12     """A single value (float, integer or string) result from a test.
13
14     A test that output a hash of the content in a page might produce a
15     string value:
16        StringValue(page, 'page_hash', 'hash', '74E377FF')
17     """
18     super(StringValue, self).__init__(page, name, units, important, description)
19     assert isinstance(value, basestring)
20     self.value = value
21
22   def __repr__(self):
23     if self.page:
24       page_name = self.page.url
25     else:
26       page_name = None
27     return 'StringValue(%s, %s, %s, %s, important=%s, description=%s)' % (
28       page_name,
29       self.name,
30       self.units,
31       self.value,
32       self.important,
33       self.description)
34
35   def GetBuildbotDataType(self, output_context):
36     if self._IsImportantGivenOutputIntent(output_context):
37       return 'default'
38     return 'unimportant'
39
40   def GetBuildbotValue(self):
41     # Buildbot's print_perf_results method likes to get lists for all values,
42     # even when they are scalar, so list-ize the return value.
43     return [self.value]
44
45   def GetRepresentativeNumber(self):
46     return self.value
47
48   def GetRepresentativeString(self):
49     return str(self.value)
50
51   @staticmethod
52   def GetJSONTypeName():
53     return 'string'
54
55   def AsDict(self):
56     d = super(StringValue, self).AsDict()
57     d['value'] = self.value
58     return d
59
60   @staticmethod
61   def FromDict(value_dict, page_dict):
62     kwargs = value_module.Value.GetConstructorKwArgs(value_dict, page_dict)
63     kwargs['value'] = value_dict['value']
64
65     return StringValue(**kwargs)
66
67   @classmethod
68   def MergeLikeValuesFromSamePage(cls, values):
69     assert len(values) > 0
70     v0 = values[0]
71     return list_of_string_values.ListOfStringValues(
72         v0.page, v0.name, v0.units,
73         [v.value for v in values],
74         important=v0.important)
75
76   @classmethod
77   def MergeLikeValuesFromDifferentPages(cls, values,
78                                         group_by_name_suffix=False):
79     assert len(values) > 0
80     v0 = values[0]
81     if not group_by_name_suffix:
82       name = v0.name
83     else:
84       name = v0.name_suffix
85     return list_of_string_values.ListOfStringValues(
86         None, name, v0.units,
87         [v.value for v in values],
88         important=v0.important)