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