def perform_operations(tag, uid_info, x, app_file): FunCom.p_open(Res.adb_dump_battery_status_reset(tag)) FunCom.serial_operation_by_control_file(tag) battery_info = access_to_electricity(tag, uid_info) FunCom.sleep(5) write_data_into_excel(battery_info, x, app_file)
def access_to_electricity(tag, uid_info): info = Modules.BatteryInfo() info.end_time.append(FunCom.current_time()) battery_info = FunCom.p_open(Res.adb_dump_battery(tag)).readlines() for ii in range(len(battery_info) - 1): if 'level:' in battery_info[ii]: battery_info_arr = battery_info[ii].split(' level: ') info.battery_percentage.append(battery_info_arr[1].strip()) battery_detailed_info = FunCom.p_open( Res.adb_dump_battery_status(tag)).readlines() for i in range(len(battery_detailed_info) - 1): if 'Capacity:' in battery_detailed_info[i]: bat_info = battery_detailed_info[i].split(',') battery_capacity = bat_info[0].split(' Capacity: ') battery_computed_drain = bat_info[1].split(' Computed drain: ') info.battery_actual_drain = bat_info[2].split(' actual drain: ') info.capacity.append(int(battery_capacity[1].strip())) info.computed.append(float(battery_computed_drain[1].strip())) if 'Uid ' + str(uid_info) in battery_detailed_info[i]: num = battery_detailed_info[i].split(':') num = num[1].split('(') info.uid_item_info.append(float(num[0].strip())) return info
def convert_and_store_in_directory(dest_path, target_name): target_name = target_name.replace(Res.hprof_suffix, '') dump_file_path = os.path.join(dest_path, target_name) dump_file = dump_file_path + Res.hprof_suffix if not dump_file_path.endswith( Res.hprof_suffix) else dump_file_path Fun.make_dir_if_not_exist(dump_file_path) convert_file = os.path.join(dump_file_path, target_name + Res.convert_suffix) Fun.p_open(Res.adb_convert_heap_profile(dump_file, convert_file))
def information_collection(apk_tag, dev, path): Fun.log('trigger gc') absolute_tmp_dump_path = os.path.realpath(path) Fun.p_open( Res.adb_grab_heap_dump_file_with_pkg(dev, absolute_tmp_dump_path, Res.pkg_name)) Fun.sleep(10) lines = Fun.p_open(Res.asb_shell_dump_mem_info(dev)).readlines() name = apk_tag + Res.dump + Fun.current_time() dump_mem_info_store_to_file(name, lines, path) Grab.grab_dump_and_convert(dev, name, absolute_tmp_dump_path, Res.pkg_name)
def exception_handle(device_id, logcat_to_file=False): # if logcat_to_file: FunCom.log_cat_to_file(device_id) device_info = FunCom.parse_device_info(device_id) dir_to_store_logfile = FunCom.get_abspath( FunCom.path_join( Res.log_path, device_info + Res.underline + Res.logcat_tag + FunCom.current_time())) FunCom.make_dir_if_not_exist(dir_to_store_logfile) FunCom.log('Exception handle id:' + device_id + " dir to store log::" + dir_to_store_logfile) FunCom.p_open(Res.asb_shell_pull_log_file(device_id, dir_to_store_logfile))
def install_app_start_activity(tag, pkg, act, app_file, replace_apk=False, delay=6): if replace_apk or not FunCom.tell_app_installed(tag, pkg): apk_path = '' for f in FunCom.list_dir(FunCom.Res.util_path): if app_file in f: apk_path = FunCom.get_abspath(FunCom.path_join(FunCom.Res.util_path, f)) if apk_path == '': FunCom.raise_error( app_file + ' Apk file not found under ./util directory') # windows do not support grep command, cannot tell existence install_r(tag, apk_path, False) FunCom.sleep(delay) FunCom.p_open(FunCom.Res.asb_shell_start_activity(tag, act))
def grab_dump_and_convert(dev, target_name, dest_path, pkg): Fun.p_open( Res.adb_grab_heap_dump_file_with_pkg(dev, absolute_tmp_dump_path, pkg)) Fun.sleep(4) dump_file_path = os.path.join(dest_path, target_name) dump_file = dump_file_path + Res.hprof_suffix if not dump_file_path.endswith( Res.hprof_suffix) else dump_file_path if os.path.exists(dump_file): dump_file = dump_file.replace(Res.hprof_suffix, Fun.current_time() + Res.hprof_suffix) Fun.p_open( Res.adb_pull_heap_file_to_dest(dev, absolute_tmp_dump_path, dump_file)) convert_file_into_directory(dump_file)
def install_r(device, apk_path, r): install_cmd = Res.install_r if r else Res.install try: command = Res.adb_s + device + install_cmd + '%s' % apk_path return FunCom.p_open(command) except Exception as e: FunCom.log(e)
def battery_instrument_main(): FunCom.log('launch battery instrument ') x = 0 while True: for app_file in FunPkg.get_apk_file_in_project(): try: tag = FunCom.devices_list_with_connection_check()[0] FunCom.p_open(Res.adb_dump_battery_unplug(tag)) FunCom.send_key_home(tag) FunPkg.uninstall_and_install(tag, app_file, Res.pkg_name) FunCom.serial_clicks_by_control_file(tag) FunCom.sleep(20) perform_operations(tag, FunCom.get_device_uid(tag), x, app_file) x += 1 except Exception as e: FunCom.log(e)
def start_launcher_omit_splash(d, omit_splash=True, back_press=True): if omit_splash: Fun.p_open(Res.asb_shell_start_activity(d)) Fun.sleep(Res.pkg_init_delay) Fun.p_open(Res.asb_shell_force_stop(d)) Fun.p_open(Res.asb_shell_start_activity(d)) if back_press: for i in range(5): Fun.sleep(3) Fun.p_open(Res.adb_key_event_back(d))
def count_info(tag, total_number_dalvik_size, dalvik_size, dalvik_alloc): FunCom.sleep(15) mem_info = FunCom.p_open( 'adb -s ' + tag + ' shell dumpsys mem_info com.kcg.com.fun').readlines() for info_item in mem_info: if 'Dalvik Heap' in info_item: info_item_info_arr = info_item.split(' ') if len(info_item_info_arr) > 4: total_number_dalvik_size.append(info_item_info_arr[1]) dalvik_size.append(info_item_info_arr[-3]) dalvik_alloc.append(info_item_info_arr[-2]) FunCom.log(str(info_item_info_arr[1]) + '===============')
def count_info_first(tag, total_dalvik_size1, dalvik_size1, dalvik_alloc1): for i in range(10): FunCom.p_open( 'adb -s ' + tag + ' shell am dumpheap com.kcg.com.fun /sdcard/click_test.hprof ') FunCom.sleep(2) FunCom.p_open('adb -s ' + tag + ' shell dumpsys mem_info com.kcg.com.fun') FunCom.sleep(2) FunCom.sleep(20) FunCom.p_open('adb -s ' + tag + ' shell dumpsys mem_info com.kcg.com.fun') mem_info = FunCom.p_open( 'adb -s ' + tag + ' shell dumpsys mem_info com.kcg.com.fun').readlines() for info_item in mem_info: if 'Dalvik Heap' in info_item: info_item_info_arr = info_item.split(' ') if len(info_item_info_arr) > 4: total_dalvik_size1.append(info_item_info_arr[1]) dalvik_size1.append(info_item_info_arr[-3]) dalvik_alloc1.append(info_item_info_arr[-2]) FunCom.log(str(info_item_info_arr[1]) + '-------------------')
def start(tag, pkg, events, throttle, logfile): FunCom.p_open( Res.asb_shell_start_monkey_with_s(tag) % (pkg, events, throttle, FunCom.get_abspath(logfile))).read()
def clear(devices_id): FunCom.p_open('adb -s ' + devices_id + Res.shell_clear)
import sys
from multiprocessing import freeze_support
def dump_mem_info_store_to_file(name, lines, path): file_path = os.path.join(path, name + Res.txt_suffix) if not os.path.exists(file_path): Fun.p_open('touch ' + file_path) Fun.write_lines_into_file(file_path, lines)
# coding=utf-8
def push_lib_to_phone(tag): lib_file = FunCom.get_abspath( FunCom.path_join(Res.lib_path, Res.monkey_lib_file_name)) FunCom.p_open(Res.asb_shell_push_file(tag, lib_file))
def uninstall_directly(): command = Res.adb + Res.uninstall + Res.pkg_name FunCom.p_open(command)
def uninstall(device_id, package_name): command = Res.adb_s + device_id + Res.uninstall + package_name FunCom.p_open(command)
def memory_instrument_main(): number = sys.argv[1] # number=10 x = 0 app_map = FunPkg.get_apk_file_in_project() while True: apk_path = os.getcwd() devices_info = FunCom.connected_devices_arr() devices_num = len(devices_info) if devices_num > 0: tag = devices_info[0] for app_map_item in range(len(app_map)): try: total_number_dalvik_size = [] dalvik_size = [] dalvik_alloc = [] first_time = [] end_time = [] levels = [] capacity = [] computed = [] uid_item_info = [] app_map_name = app_map[app_map_item] total_dalvik_size1 = [] dalvik_size1 = [] dalvik_alloc1 = [] app_map_name_path = apk_path + "/apk/" + app_map_name.strip() FunCom.log(app_map_name_path) FunPkg.uninstall(tag, Res.pkg_name) FunPkg.install(tag, app_map_name_path) FunCom.sleep(10) for i in range(500): FunCom.serial_clicks_by_control_file(tag) FunCom.p_open('adb -s ' + tag + ' shell dumpsys battery unplug') count_info_first(tag, total_dalvik_size1, dalvik_size1, dalvik_alloc1) datetime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) first_time.append(datetime) uid_info = FunCom.get_device_uid(tag) FunCom.p_open('adb -s ' + tag + ' shell ' + 'dumpsys batterystats --reset') FunCom.serial_operation_by_control_file(tag) FunCom.log('------') access_to_electricity(tag, end_time, levels, capacity, computed, uid_item_info, uid_info) count_info(tag, total_number_dalvik_size, dalvik_size, dalvik_alloc) if total_number_dalvik_size[i] < total_dalvik_size1[i]: total_dalvik_size1.pop(i) total_number_dalvik_size.pop(i) dalvik_size.pop(i) dalvik_size1.pop(i) dalvik_alloc1.pop(i) dalvik_alloc.pop(i) first_time.pop(i) end_time.pop(i) levels.pop(i) capacity.pop(i) computed.pop(i) uid_item_info.pop(i) write_data_into_excel( x, total_number_dalvik_size, dalvik_size, dalvik_alloc, app_map_name, tag, total_dalvik_size1, dalvik_size1, dalvik_alloc1, first_time, end_time, levels, capacity, computed, uid_item_info) FunPkg.clear(tag) if len(total_number_dalvik_size) == int(number): break x += 1 except Exception as e: FunCom.log(e) if app_map_item == len(app_map) - 1: app_map_item == 0
import FunctionCommon as Fun
def uninstall_launcher(device_id): command = Res.adb_s + device_id + Res.uninstall + Res.pkg_name FunCom.p_open(command)