1#!/usr/bin/env vpython3 2 3# Copyright 2024 The Chromium Authors 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6""" The entry of the metric system.""" 7 8from typing import List, Set 9 10from measure import Measure 11from test_script_metrics_pb2 import TestScriptMetrics 12 13 14class Metric: 15 16 def __init__(self) -> None: 17 # A list of Measure to dump. 18 self._metrics: List[Measure] = [] 19 # A list of tags to suffix the dumped results; see tag and dump function. 20 self._tags: Set[str] = set() 21 22 def register(self, metric: Measure) -> None: 23 self._metrics.append(metric) 24 25 def size(self) -> int: 26 return len(self._metrics) 27 28 def clear(self) -> None: 29 self._metrics.clear() 30 self._tags.clear() 31 32 def tag(self, *args: str) -> None: 33 # Tags the metrics, the tags will be suffixed to the name of each metric for 34 # easy selection. 35 # This is an easy and hacky solution before adding output properties from 36 # test script becomes possible. Currently adding output properties is 37 # limited to the scope of the recipe, so any runtime tags are pretty much 38 # impossible. 39 self._tags.update(list(args)) 40 41 def dump(self) -> TestScriptMetrics: 42 result = TestScriptMetrics() 43 result.metrics.extend([m.dump() for m in self._metrics]) 44 for tag in sorted(self._tags): 45 for metric in self._metrics: 46 m = metric.dump() 47 m.name = m.name + '@' + tag 48 result.metrics.append(m) 49 return result 50