1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
| import os import re import sys import subprocess import time import matplotlib as mpl import matplotlib.pyplot as plt
shell_file = ""
def package_name(): hq_list = [] cmd = 'adb shell dumpsys window | findstr mCurrentFocus' huo_qu = os.popen(cmd).read() hq_list = re.split(r'[, /]', huo_qu) pack_name = hq_list[4] return pack_name
def app_pid(): cmd = 'adb shell ps | findstr ' + package_name() + '$' huo_qu = os.popen(cmd).read() hq_list = huo_qu.split() ap_pid = hq_list[1] print("进程号:" + ap_pid) return ap_pid
def push_shell():
cur_dir = os.path.dirname(__file__)
shell = os.path.join(cur_dir, "huo_qu.sh") cmd = "adb push " + shell + " /sdcard" subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf-8")
def execute_shell(ap_pid): push_shell() res_list = [] cmd = "adb shell sh /sdcard/huo_qu.sh " + ap_pid res_str = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf-8") result = res_str.stdout.read() res_list = result.split() return res_list
def cpu_list(res): cpu_add = 0 singer_add = 0 cpu_all = res[1:11] cpu_singer = res[24:28] for i in range(10): cpu_add = int(cpu_all[i]) + cpu_add for p in range(4): singer_add = int(cpu_singer[p]) + singer_add return cpu_add, singer_add
def app_pss(res): pss_all = int(res[-5]) return pss_all
def dot(): cpu_all = [] cpu_singer = [] cpu_all_add_cha = [] cpu_singer_add_cha = [] pss_add_list = [] cpu = [] i_list = [] cpu_all_fin = 0 cpu_singer_fin = 0 cpu_chu = 0 pid = app_pid()
figure = plt.figure()
for i in range(300): res = execute_shell(pid) pss = app_pss(res) cpu_all.append(cpu_list(res)[0]) cpu_singer.append(cpu_list(res)[1]) pss_add_list.append(pss) time.sleep(0.1) if i == 0: cpu_all_fin = 0 cpu_singer_fin = 0 cpu_chu = 0 cpu_all_add_cha.append(cpu_all_fin) cpu_singer_add_cha.append(cpu_singer_fin) cpu.append(cpu_chu) elif i >= 1: cpu_all_fin = cpu_all[i] - cpu_all[i - 1] cpu_singer_fin = cpu_singer[i] - cpu_singer[i - 1] cpu_all_add_cha.append(cpu_all_fin) cpu_singer_add_cha.append(cpu_singer_fin) cpu_chu = cpu_singer_add_cha[i] / cpu_all_add_cha[i] cpu.append(cpu_chu) i_list.append(i) print(pss_add_list) print(cpu) print(pss_add_list)
x = i_list y = cpu y1 = pss_add_list
ax1 = figure.add_subplot(221) ax2 = figure.add_subplot(223)
ax1.plot(x, y) ax2.plot(x, y1)
plt.show()
if __name__ == '__main__': dot()
|