tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

average.py (736B)


      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 """ A metric implementation to calculate the average of the inputs. """
      7 
      8 from measure import Measure
      9 from test_script_metrics_pb2 import TestScriptMetric
     10 
     11 
     12 class Average(Measure):
     13 
     14  def __init__(self, name: str) -> None:
     15    self._name = name
     16    self._value = 0
     17    self._count = 0
     18 
     19  def record(self, value: float) -> None:
     20    self._value = (self._value * self._count + value) / (self._count + 1)
     21    self._count += 1
     22 
     23  def dump(self) -> TestScriptMetric:
     24    result = TestScriptMetric()
     25    result.name = self._name
     26    result.value = self._value
     27    return result