0%

cpu

这是一个用python写的获取单个手机游戏cpu利用率和内存占用的情况,最后用matplotlib绘图工具画曲线图,这里主要记录一下在python中调用.sh文件的一种思路,把.sh文件push到手机(模拟器)中,然后调用adb命令执行.sh文件,具体看push_shell()函数和execute_shell()函数

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
# -*-coding:utf-8-*-
import os
import re
import sys
import subprocess
import time
import matplotlib as mpl
import matplotlib.pyplot as plt

# sys.path.append(os.path.join(os.path.dirname(__file__), 'util'))
# from util import execute_cmd, time_couter
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


# 把shell文件push到手机
def push_shell():
# main文件在C:/Users/Administrator/PycharmProjects/pythonProject123/pythonProject/main.py中

# os.path.dirname,去掉文件名,返回目录;返回的是C:\Users\Administrator\PycharmProjects\pythonProject123\pythonProject

cur_dir = os.path.dirname(__file__)

# os.path.join,追加路径;C:\Users\Administrator\PycharmProjects\pythonProject123\pythonProject\huo_qu.sh

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")

# execute_cmd("adb push " + shell + " /sdcard")


# 执行shell文件
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()
# res_fin = int(res_list[1])
# print(res_list)
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画布对象
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()

.sh文件如下:

1
2
3
4
5
pid=$1
cat /proc/stat | grep cpu[^0-9]
cat /proc/${pid}/stat
dumpsys meminfo ${pid} | grep TOTAL:

点这里请我吃个小蛋糕吧~~

Welcome to my other publishing channels