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