-
Notifications
You must be signed in to change notification settings - Fork 6
/
getAndroidCpu.py
executable file
·125 lines (103 loc) · 4.26 KB
/
getAndroidCpu.py
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
# -*- coding: utf-8 -*-
import os
import subprocess
import sys
import time
defulat_adb_tool_path = "D:\\developSdk\\Android\\sdk\\platform-tools"
def collect_msg(arg_time, arg_log_path, arg_app_name):
# 根据应用的包名称 获取CPU以及内存占用
global param_device_id
app_id_str = ""
cpu_str = ""
while 1:
now_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
cpu_memery_cmd = [os.path.join(defulat_adb_tool_path, "adb"), "-s", param_device_id, "shell", "top", "-m", "10", "-n", "1"]
child = subprocess.Popen(cpu_memery_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
cwd=defulat_adb_tool_path)
child.wait()
child_out = child.stdout.readlines()
child.stdout.close()
for item in child_out:
if item.find(arg_app_name) > 0:
if item.find('%') > 0: # 获取CPU占用百分比
cpu_str = str(int(item[item.find('%') - 3:item.find('%')])) # cpu占用数值
break
# 将数据写入文件
with open(os.path.join(arg_log_path, param_device_id + "_" + arg_app_name + "_cpu.log"), "a") as f:
write_str = '[' + str(
now_time) + ']|{"cpu":"' + cpu_str + '%"} \n'
f.write(write_str)
time.sleep(float(arg_time))
def getparam():
global param_collect_time
global param_log_path
global param_package_name
global param_device_id
count = 1
while count < len(sys.argv):
if sys.argv[count] == "-h" or sys.argv[count] == "--help":
print_help()
sys.exit(0)
if sys.argv[count] == "--collect-time":
count += 1
if count < len(sys.argv):
param_collect_time = sys.argv[count]
if sys.argv[count] == "--log-path":
count += 1
if count < len(sys.argv):
param_log_path = sys.argv[count]
if sys.argv[count] == "--package-name":
count += 1
if count < len(sys.argv):
param_package_name = sys.argv[count]
if sys.argv[count] == "--device-id":
count += 1
if count < len(sys.argv):
param_device_id = sys.argv[count]
count +=1
def check_device():
global param_device_id;
cmd = [os.path.join(defulat_adb_tool_path, "adb"),"devices"]
child = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
cwd=defulat_adb_tool_path)
child.wait()
child_out = child.stdout.readlines()
child.stdout.close()
flag = False;
for item in child_out:
if item.find(param_device_id) >= 0 and item.find("offline") < 0:
flag = True
break
return flag;
def print_help():
print u"用法 python getAndroidCpu.py --collect-time ... --log-path ... --package-name ... --device-id ..."
print u"选项:"
print u"\t -h/--help 帮助"
print u"\t --collect-time 信息采集间隔,以秒为单位"
print u"\t --log-path 日志文件的放置位置"
print u"\t --package-name 应用的包名"
print u"\t --device-id 设备序列号"
if __name__ == "__main__":
adb_path = os.environ.get('ANDROID_HOME')
if os.path.isdir(os.path.join(adb_path, "platform-tools")):
defulat_adb_tool_path = os.path.join(adb_path, "platform-tools")
else:
print u"请设置‘ANDROID_HOME’环境变量"
sys.exit(0)
param_collect_time = "" # 采集信息时间间隔
param_log_path = "" # 日志的放置位置 d:\mi.log
param_package_name = "" # 应用的appname
param_device_id = ""
if len(sys.argv) <= 3:
print_help()
sys.exit(0)
getparam()
if param_collect_time is "" or param_log_path is "" or param_package_name is "" or param_device_id is "":
print u"参数不足"
print_help();
sys.exit(0)
if check_device() is False:
print U"设备序列号不存在或离线"
print_help();
sys.exit(0)
collect_msg(param_collect_time, param_log_path, param_package_name)