Home | History | Annotate | Download | only in power
      1 # Copyright 2018 The Chromium OS Authors. All rights reserved.
      2 # Use of this source code is governed by a BSD-style license that can be
      3 # found in the LICENSE file.
      4 
      5 """Helper class for power autotests requiring telemetry devices."""
      6 
      7 import logging
      8 import time
      9 
     10 CUSTOM_START = 'PowerTelemetryLogger custom start.'
     11 CUSTOM_END = 'PowerTelemetryLogger custom end.'
     12 
     13 def log_event_ts(message=None, timestamp=None, offset=0):
     14     """Log the event and timestamp for parsing later.
     15 
     16     @param message: description of the event.
     17     @param timestamp: timestamp to for the event, if not provided, default to
     18            current time. Local seconds since epoch.
     19     @param offset: offset in seconds from the provided timestamp, or offset from
     20            current time if timestamp is not provided. Can be positive or
     21            negative.
     22     """
     23     if not message:
     24         return
     25     if timestamp:
     26         ts = timestamp + offset
     27     else:
     28         ts = time.time() + offset
     29     logging.debug("%s %s", message, ts)
     30 
     31 def start_measurement(timestamp=None, offset=0):
     32     """Mark the start of power telemetry measurement.
     33 
     34     Optional. Use only once in the client side test that is wrapped in
     35     power_MeasurementWrapper to help pinpoint exactly where power telemetry
     36     data should start. PowerTelemetryLogger will trim off excess data before
     37     this point. If not used, power telemetry data will start right before the
     38     client side test.
     39     @param timestamp: timestamp for the start of measurement, if not provided,
     40            default to current time. Local seconds since epoch.
     41     @param offset: offset in seconds from the provided timestamp, or offset from
     42            current time if timestamp is not provided. Can be positive or
     43            negative.
     44     """
     45     log_event_ts(CUSTOM_START, timestamp, offset)
     46 
     47 def end_measurement(timestamp=None, offset=0):
     48     """Mark the end of power telemetry measurement.
     49 
     50     Optional. Use only once in the client side test that is wrapped in
     51     power_MeasurementWrapper to help pinpoint exactly where power telemetry
     52     data should end. PowerTelemetryLogger will trim off excess data after
     53     this point. If not used, power telemetry data will end right after the
     54     client side test.
     55     @param timestamp: timestamp for the end of measurement, if not provided,
     56            default to current time. Local seconds since epoch.
     57     @param offset: offset in seconds from the provided timestamp, or offset from
     58            current time if timestamp is not provided. Can be positive or
     59            negative.
     60     """
     61     log_event_ts(CUSTOM_END, timestamp, offset)
     62