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