Exemplo n.º 1
0
    def testGetDiagnosticClassForName(self):
        cls0 = all_diagnostics.GetDiagnosticClassForName('GenericSet')
        gs0 = cls0(['foo'])
        gs0_dict = gs0.AsDict()

        # Run twice to ensure that the memoization isn't broken.
        cls1 = all_diagnostics.GetDiagnosticClassForName('GenericSet')
        gs1 = cls1(['foo'])
        gs1_dict = gs1.AsDict()

        self.assertEqual(gs0_dict['type'], 'GenericSet')
        self.assertEqual(gs1_dict['type'], 'GenericSet')
Exemplo n.º 2
0
    def _UpdateDiagnostics(self):
        """ Benchmarks that add histograms but don't use
    timeline_base_measurement need to add shared diagnostics separately.
    Make them available on the telemetry info."""
        for name, value in self.AsDict().items():
            if name in self.diagnostics:
                # If it is of type name, description or start time don't create new
                if name in [
                        reserved_infos.BENCHMARKS.name,
                        reserved_infos.BENCHMARK_START.name,
                        reserved_infos.BENCHMARK_DESCRIPTIONS.name
                ]:
                    continue
                else:
                    # this a stale value from the last run, remove it.
                    del self.diagnostics[name]

            if isinstance(value, list):
                keep = False
                for val in value:
                    if val:
                        keep = True
                if not keep:
                    continue
            else:
                if value is None:
                    continue

            name_type = reserved_infos.GetTypeForName(name)
            diag_class = all_diagnostics.GetDiagnosticClassForName(name_type)
            diag = diag_class(value)
            self.diagnostics[name] = diag
Exemplo n.º 3
0
    def _GetDiagnostics(self):
        """Get benchmark and current story details as histogram diagnostics."""
        diag_values = [
            (reserved_infos.BENCHMARKS, self.benchmark_name),
            (reserved_infos.BENCHMARK_START, self.benchmark_start_us),
            (reserved_infos.BENCHMARK_DESCRIPTIONS,
             self.benchmark_description),
            (reserved_infos.LABELS, self.label),
            (reserved_infos.HAD_FAILURES, self.current_story_run.failed),
            (reserved_infos.STORIES, self.current_story.name),
            (reserved_infos.STORY_TAGS, self.current_story.GetStoryTagsList()),
            (reserved_infos.STORYSET_REPEATS, self.current_story_run.index),
            (reserved_infos.TRACE_START, self.current_story_run.start_us),
        ]

        diags = {}
        for diag, value in diag_values:
            if value is None or value == []:
                continue
            if diag.type == 'GenericSet' and not isinstance(value, list):
                value = [value]
            elif diag.type == 'DateRange':
                # We store timestamps in microseconds, DateRange expects milliseconds.
                value = value / 1e3  # pylint: disable=redefined-variable-type
            diag_class = all_diagnostics.GetDiagnosticClassForName(diag.type)
            diags[diag.name] = diag_class(value)
        return diags
Exemplo n.º 4
0
    def _GetDiagnostics(self):
        """Get benchmark metadata as histogram diagnostics."""
        info = self._telemetry_info
        diag_values = [
            (reserved_infos.BENCHMARKS, info.benchmark_name),
            (reserved_infos.BENCHMARK_START, info.benchmark_start_us),
            (reserved_infos.BENCHMARK_DESCRIPTIONS,
             info.benchmark_descriptions), (reserved_infos.LABELS, info.label),
            (reserved_infos.HAD_FAILURES, info.had_failures),
            (reserved_infos.STORIES, info._story_name),
            (reserved_infos.STORY_TAGS, info.GetStoryTagsList()),
            (reserved_infos.STORYSET_REPEATS, info.storyset_repeat_counter),
            (reserved_infos.TRACE_START, info.trace_start_us),
            (reserved_infos.TRACE_URLS, info.trace_url)
        ]

        diags = {}
        for diag, value in diag_values:
            if value is None or value == []:
                continue
            if diag.type == 'GenericSet' and not isinstance(value, list):
                value = [value]
            elif diag.type == 'DateRange':
                # We store timestamps in microseconds, DateRange expects milliseconds.
                value = value / 1e3  # pylint: disable=redefined-variable-type
            diag_class = all_diagnostics.GetDiagnosticClassForName(diag.type)
            diags[diag.name] = diag_class(value)
        return diags
Exemplo n.º 5
0
 def FromDict(dct):
     cls = all_diagnostics.GetDiagnosticClassForName(dct['type'])
     if not cls:
         raise ValueError('Unrecognized diagnostic type: ' + dct['type'])
     diagnostic = cls.FromDict(dct)
     if 'guid' in dct:
         diagnostic.guid = dct['guid']
     return diagnostic
Exemplo n.º 6
0
 def assertHasDiagnostic(self, hist, diag_info, value=None):
   """Assert that a histogram is associated with the given diagnostic."""
   self.assertIn(diag_info.name, hist.diagnostics)
   diag = hist.diagnostics[diag_info.name]
   self.assertIsInstance(
       diag, all_diagnostics.GetDiagnosticClassForName(diag_info.type))
   if value is not None:
     # Assume we expect singleton GenericSet with the given value.
     self.assertEqual(len(diag), 1)
     self.assertEqual(next(iter(diag)), value)
Exemplo n.º 7
0
        def SetDiagnosticsValue(info, value):
            if value is None or value == []:
                return

            if info.type == 'GenericSet' and not isinstance(value, list):
                value = [value]
            elif info.type == 'DateRange':
                # We store timestamps in microseconds, DateRange expects milliseconds.
                value = value / 1e3
            diag_class = all_diagnostics.GetDiagnosticClassForName(info.type)
            self.diagnostics[info.name] = diag_class(value)
Exemplo n.º 8
0
    def FromProto(d):
        # Here we figure out which field is set and downcast to the right diagnostic
        # type. The diagnostic names in the proto must be the same as the class
        # names in the python code, for instance Breakdown.
        attr_name = d.WhichOneof('diagnostic_oneof')
        assert attr_name, 'The diagnostic oneof cannot be empty.'

        d = getattr(d, attr_name)
        assert type(d).__name__ in all_diagnostics.GetDiagnosticTypenames(), (
            'Unrecognized diagnostic type ' + type(d).__name__)

        diag_type = type(d).__name__
        cls = all_diagnostics.GetDiagnosticClassForName(diag_type)

        return cls.FromProto(d)
Exemplo n.º 9
0
def _WrapDiagnostics(info_value_pairs):
    """Wrap diagnostic values in corresponding Diagnostics classes.

  Args:
    info_value_pairs: any iterable of pairs (info, value), where info is one
        of reserved infos defined in tracing.value.diagnostics.reserved_infos,
        and value can be any json-serializable object.

  Returns:
    An iterator over pairs (diagnostic name, diagnostic value).
  """
    for info, value in info_value_pairs:
        if value is None or value == []:
            continue
        if info.type == 'GenericSet' and not isinstance(value, list):
            value = [value]
        diag_class = all_diagnostics.GetDiagnosticClassForName(info.type)
        yield info.name, diag_class(value)
Exemplo n.º 10
0
def Deserialize(type_name, data, deserializer):
    cls = all_diagnostics.GetDiagnosticClassForName(type_name)
    if not cls:
        raise ValueError('Unrecognized diagnostic type: ' + type_name)
    return cls.Deserialize(data, deserializer)
Exemplo n.º 11
0
 def testEqualityForSmoke(self):
     for name in all_diagnostics.GetDiagnosticTypenames():
         ctor = all_diagnostics.GetDiagnosticClassForName(name)
         self.assertTrue(hasattr(ctor, '__eq__'))