1# Lint as: python2, python3 2# Copyright 2019 The Chromium OS Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6"""Stress test Servo's charging functionalities. """ 7 8from __future__ import print_function 9 10import collections 11import logging 12import time 13 14from autotest_lib.client.common_lib import error 15from autotest_lib.server import test 16from autotest_lib.server.cros.power import servo_charger 17 18 19TOTAL_LOOPS = 100 20SLEEP = 5 21COMMAND_FAIL = -1 22 23class power_ServoChargeStress(test.test): 24 """Stress test Servo's charging functionalities. 25 26 This test runs loops to change Servo's PD role. 27 """ 28 version = 1 29 30 def run_once(self, host, total_loops=TOTAL_LOOPS, sleep=SLEEP): 31 """Run loops to change Servo's PD role. 32 33 @param host: CrosHost object representing the DUT. 34 @param total_loops: total loops of Servo role change. 35 @param sleep: seconds to sleep between Servo role change command. 36 """ 37 self._charge_manager = servo_charger.ServoV4ChargeManager(host, 38 host.servo) 39 pd_roles = ['snk', 'src'] 40 total_fail = collections.defaultdict(int) 41 total_success_with_recovery = collections.defaultdict(int) 42 keyval = dict() 43 44 for loop in range(total_loops): 45 logging.info('') # Add newline to make logs easier to read. 46 logging.info('Starting loop %d...', loop) 47 48 for role in pd_roles: 49 time.sleep(sleep) 50 try: 51 # result = -1 failed; 52 # result = 0 successful; 53 # result > 0 successful with 'result' # of recoveries. 54 if role == 'snk': 55 result = self._charge_manager.stop_charging() 56 elif role == 'src': 57 result = self._charge_manager.start_charging() 58 if result > 0: # Recoveries triggered. 59 total_success_with_recovery[role] += 1 60 except error.TestBaseException as e: 61 logging.error('Loop %d: %s', loop, str(e)) 62 result = COMMAND_FAIL 63 total_fail[role] += 1 64 keyval['loop%04d_%s' % (loop, role)] = result 65 66 logging.info('End of loop %d.', loop) 67 68 logging.info('Restore original Servo PD setting...') 69 self._charge_manager.restore_original_setting() 70 logging.info('End of test.') 71 72 for role in pd_roles: 73 keyval['Total_%s_fail' % role] = total_fail[role] 74 keyval['Total_%s_success_with_recovery' % role] = \ 75 total_success_with_recovery[role] 76 keyval['Total_loops'] = total_loops 77 self.write_perf_keyval(keyval) 78