Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(341)

Unified Diff: tools/telemetry/telemetry/value/__init__.py

Issue 417193005: Add Value FromDict to Telemetry (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address Nat's comments Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tools/telemetry/telemetry/value/failure.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/telemetry/telemetry/value/__init__.py
diff --git a/tools/telemetry/telemetry/value/__init__.py b/tools/telemetry/telemetry/value/__init__.py
index 816e7cfa051de18be91a4d7538b356fcbb1f1d71..bfebc53afa87abe8dc5f483a5b4d2cf93fc32f2d 100644
--- a/tools/telemetry/telemetry/value/__init__.py
+++ b/tools/telemetry/telemetry/value/__init__.py
@@ -1,4 +1,4 @@
-# Copyright 2013 The Chromium Authors. All rights reserved.
+# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""
@@ -20,6 +20,10 @@ Downstream consumers of test results typically want to group these runs
together, then compute summary statistics across runs. Value provides the
Merge* family of methods for this kind of aggregation.
"""
+import os
+
+from telemetry.core import discover
+from telemetry.core import util
# When combining a pair of Values togehter, it is sometimes ambiguous whether
# the values should be concatenated, or one should be picked as representative.
@@ -161,22 +165,20 @@ class Value(object):
"""
raise NotImplementedError()
- @classmethod
- def GetJSONTypeName(cls):
+ @staticmethod
+ def GetJSONTypeName():
"""Gets the typename for serialization to JSON using AsDict."""
raise NotImplementedError()
def AsDict(self):
- """Gets a representation of this value as a dict for eventual
- serialization to JSON.
- """
+ """Pre-serializes a value to a dict for output as JSON."""
return self._AsDictImpl()
def _AsDictImpl(self):
d = {
'name': self.name,
'type': self.GetJSONTypeName(),
- 'unit': self.units,
+ 'units': self.units,
}
if self.description:
@@ -195,6 +197,81 @@ class Value(object):
return dict([(k, v) for (k, v) in full_dict.iteritems()
if k not in base_dict_keys])
+ @staticmethod
+ def FromDict(value_dict, page_dict):
+ """Produces a value from a value dict and a page dict.
+
+ Value dicts are produced by serialization to JSON, and must be accompanied
+ by a dict mapping page IDs to pages, also produced by serialization, in
+ order to be completely deserialized. If deserializing multiple values, use
+ ListOfValuesFromListOfDicts instead.
+
+ value_dict: a dictionary produced by AsDict() on a value subclass.
+ page_dict: a dictionary mapping IDs to page objects.
+ """
+ return Value.ListOfValuesFromListOfDicts([value_dict], page_dict)[0]
+
+ @staticmethod
+ def ListOfValuesFromListOfDicts(value_dicts, page_dict):
+ """Takes a list of value dicts to values.
+
+ Given a list of value dicts produced by AsDict, this method
+ deserializes the dicts given a dict mapping page IDs to pages.
+ This method performs memoization for deserializing a list of values
+ efficiently, where FromDict is meant to handle one-offs.
+
+ values: a list of value dicts produced by AsDict() on a value subclass.
+ page_dict: a dictionary mapping IDs to page objects.
+ """
+ value_dir = os.path.dirname(__file__)
+ value_classes = discover.DiscoverClasses(
+ value_dir, util.GetTelemetryDir(),
+ Value, index_by_class_name=True)
+
+ value_json_types = dict((value_classes[x].GetJSONTypeName(), x) for x in
+ value_classes)
+
+ values = []
+ for value_dict in value_dicts:
+ value_class = value_classes[value_json_types[value_dict['type']]]
+ assert 'FromDict' in value_class.__dict__, \
+ 'Subclass doesn\'t override FromDict'
+ values.append(value_class.FromDict(value_dict, page_dict))
+
+ return values
+
+ @staticmethod
+ def GetConstructorKwArgs(value_dict, page_dict):
+ """Produces constructor arguments from a value dict and a page dict.
+
+ Takes a dict parsed from JSON and an index of pages and recovers the
+ keyword arguments to be passed to the constructor for deserializing the
+ dict.
+
+ value_dict: a dictionary produced by AsDict() on a value subclass.
+ page_dict: a dictionary mapping IDs to page objects.
+ """
+ d = {
+ 'name': value_dict['name'],
+ 'units': value_dict['units']
+ }
+
+ description = value_dict.get('description', None)
+ if description:
+ d['description'] = description
+ else:
+ d['description'] = None
+
+ page_id = value_dict.get('page_id', None)
+ if page_id:
+ d['page'] = page_dict[int(page_id)]
+ else:
+ d['page'] = None
+
+ d['important'] = False
+
+ return d
+
def ValueNameFromTraceAndChartName(trace_name, chart_name=None):
"""Mangles a trace name plus optional chart name into a standard string.
« no previous file with comments | « no previous file | tools/telemetry/telemetry/value/failure.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698