Last active
March 20, 2018 08:35
-
-
Save ndvo2710/990908d2204a1c048b17240836fb22ae to your computer and use it in GitHub Desktop.
Power Drain Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import time | |
import subprocess | |
import shlex | |
import re | |
def read_battery_level(device): | |
cmd = "adb -s " + device + " shell dumpsys battery| grep level" | |
result = subprocess.check_output(shlex.split(cmd), stderr=subprocess.STDOUT) | |
batterylevel = float(re.sub(' level: ','', result).splitlines()[0]) | |
return batterylevel | |
def turn_off_charging(device): | |
cmds = [' root', | |
' shell "echo 0 > /sys/class/power_supply/battery/charging_enabled"'] | |
for cmd in cmds: | |
cmd = 'adb -s ' + device + cmd | |
print cmd | |
subprocess.check_output(shlex.split(cmd), stderr=subprocess.STDOUT) | |
print "Device charging mode is disabled" | |
def turn_on_charging(device): | |
cmds = [' root', | |
' shell "echo 1 > /sys/class/power_supply/battery/charging_enabled"'] | |
for cmd in cmds: | |
cmd = 'adb -s ' + device + cmd | |
print cmd | |
subprocess.check_output(shlex.split(cmd), stderr=subprocess.STDOUT) | |
print "Device charging mode is enabled" | |
def printOutput(hours, device, first, last): | |
print "-------------------------------" | |
print 'Standby Test for {} hours:'.format(hours) | |
print 'Device: {}'.format(device) | |
print 'Starts with: {}%'.format(first) | |
print 'Ends with: {}%'.format(last) | |
print "-------------------------------" | |
def main(args): | |
#print args | |
device = args[1] | |
duration = float(args[2]) * 60 * 60 | |
do_stop = False | |
start_time = time.time() | |
print 'Starting Time is {}'.format(time.strftime("%I : %M %p",time.localtime(start_time))) | |
battery_start_level = read_battery_level(device) | |
turn_off_charging(device) | |
#print battery_start_level | |
while not do_stop: | |
#print time.time() - start_time, duration | |
if time.time() - start_time > duration: | |
battery_end_level = read_battery_level(device) | |
#print battery_end_level | |
turn_on_charging(device) | |
do_stop = True | |
printOutput(args[2], device, battery_start_level, battery_end_level) | |
end_time = time.time() | |
print 'Starting Time is {}'.format(time.strftime("%I : %M %p",time.localtime(end_time))) | |
if __name__ == "__main__": | |
main(sys.argv) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Quickly Drain Battery to certain level | |
import os, sys, subprocess, shlex | |
import argparse | |
import time | |
def read_battery(): | |
cmd = 'adb shell "cat /sys/class/power_supply/battery/capacity"' | |
result = subprocess.check_output(shlex.split(cmd), stderr=subprocess.STDOUT).split('\n')[0] | |
return int(result) | |
def disable_charging(): | |
cmds = ['adb shell "echo 0 > /sys/class/power_supply/battery/charging_enabled"', | |
'adb shell "cat /sys/class/power_supply/battery/charging_enabled"'] | |
for cmd in cmds: | |
print cmd | |
subprocess.check_output(shlex.split(cmd), stderr=subprocess.STDOUT) | |
print "Battery charging is disable" | |
def drain_battery(): | |
# run 4 tight loops to discharge the battery | |
cmd1 = 'adb shell' | |
cmd2 = 'while true; do :; done' | |
p = subprocess.Popen(shlex.split(cmd1), stdin=subprocess.PIPE) | |
#time.sleep(1) | |
p.stdin.write(cmd2) | |
p.stdin.write('\n') | |
p.stdin.flush() | |
#time.sleep(2) | |
#p.kill() | |
def cpu_info(): | |
cmd = 'adb shell "dumpsys cpuinfo | grep TOTAL"' | |
result = subprocess.check_output(shlex.split(cmd), stderr=subprocess.STDOUT).split('% TOTAL')[0] | |
return float(result) | |
def main(): | |
# Read the current battery capacity | |
current_battery_level = read_battery() | |
print current_battery_level | |
print 'Headset Battery level is {}'.format(current_battery_level) | |
# Set lower limit and upper limit from command line args | |
parser = argparse.ArgumentParser() | |
parser.add_argument('lower', | |
type=int, | |
help='Set Lower limit for battery') | |
parser.add_argument('upper', | |
type=int, | |
help='Set Upper limit for battery') | |
args = parser.parse_args() | |
# Validate Limits | |
if args.lower > args.upper: | |
print "Error: Set your limits again" | |
quit() | |
if args.lower > current_battery_level: | |
print "Please Unplug and charge your device" | |
quit() | |
if args.upper - args.lower > 3: | |
print "Error: Try to set the smaller range with gap as 2% or 3%" | |
quit() | |
# Discharge Battery | |
disable_charging() | |
print "Draining battery....." | |
# Check for cpu load | |
do_stop = False | |
while not do_stop: | |
cpu_load = cpu_info() | |
current_battery_level = read_battery() | |
if cpu_load < 90.0: | |
drain_battery() | |
time.sleep(5) | |
#else: | |
#print "CPU is loading {}% > 90%".format(cpu_load) | |
if args.lower < current_battery_level < args.upper: | |
cmd = 'adb reboot bootloader' | |
subprocess.check_output(shlex.split(cmd), stderr=subprocess.STDOUT) | |
print "\n\n\t\t===============================" | |
print "\t\tHeadset Battery level is {}%".format(current_battery_level) | |
print "\t\tDevice is in fastboot mode" | |
print "\t\t===============================" | |
do_stop = True | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment