- Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathTest_File.py
More file actions
Latest commit
164 lines (140 loc) · 5.06 KB
/
Copy pathTest_File.py
File metadata and controls
164 lines (140 loc) · 5.06 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# -*- coding: utf-8 -*-
importos, re,sys
importsubprocess
importtime, os, sched,shlex,threading
fromreportlab.graphics.shapesimport*
fromreportlab.graphics.charts.lineplotsimportLinePlot
fromreportlab.graphics.charts.textlabelsimportLabel
fromreportlab.graphicsimportrenderPDF
# execute command, and return the output
defexecCmd(cmd):
r=os.popen(cmd)
text=r.read()
r.close()
returntext
defreadShellBack():
popen=subprocess.Popen(['adb', 'logcat'], stdout=subprocess.PIPE)
whileTrue:
printpopen.stdout.readline()
defcatchException():
try:
readShellBack()
exceptKeyboardInterrupt:
printexecCmd('adb devices')
defgetAppPID(packagename):
appPID_list= []
args=shlex.split('adb shell top')
popen=subprocess.Popen(args,stdout=subprocess.PIPE)
Status=True
try:
whileStatus:
logcat=popen.stdout.readline()
ifpackagenameinlogcat:
Status=False
forpidinlogcat.split(' '):
ifpid.isalnum():
printpid
returnpid
exceptKeyboardInterrupt:
returnpid
defgetGCfile():
os.system("adb kill-server")
os.system("adb devices")
print'日志正在记录中'
try:
os.system("adb logcat -v time -v threadtime *:D | grep GC>GCFile.txt")
exceptKeyboardInterrupt:
print'GC logcat is over'
defMakePDF(times,list,reportname,pdfname):
drawing=Drawing(500,300)
lp=LinePlot()
lp.x=50
lp.y=50
lp.height=125
lp.width=300
lp.data= [zip(times, list)]
lp.lines[0].strokeColor=colors.blue
lp.lines[1].strokeColor=colors.red
lp.lines[2].strokeColor=colors.green
drawing.add(lp)
drawing.add(String(350,150, reportname,fontSize=14,fillColor=colors.red))
renderPDF.drawToFile(drawing,pdfname,reportname)
defanalysisGCFile(AppPID):
GC_Freed=0
GC_Freed_All=0
GC_per=0
GC_per_all=0
GC_time=0
GC_time_all=0
GC_Freed_list= []
GC_per_list= []
GC_time_list= []
GC_count_list= []
GC_count=0
fordatainopen('GCFile.txt'):
foriindata.split(' '):
ifAppPID==iand'freed'indata:
GC_Freed=int(filter(str.isdigit,data.split(' ')[(data.split(' ').index('freed'))+1]))
GC_Freed_list.append(GC_Freed)
GC_Freed_All=GC_Freed_All+GC_Freed
GC_per=int(filter(str.isdigit,data.split(' ')[(data.split(' ').index('freed'))+2]))
GC_per_list.append(GC_per)
GC_per_all=GC_per_all+GC_per
number1=data.split(' ')[(data.split(' ').index('freed'))+8]
if'+'innumber1:
GC_time=int(filter(str.isdigit,number1.split('+')[0]))+int(filter(str.isdigit,number1.split('+')[1]))
GC_time_all=GC_time_all+GC_time
GC_time_list.append(GC_time)
else:
GC_time=int(number1[:-4])
GC_time_all=GC_time_all+GC_time
GC_time_list.append(GC_time)
printGC_time
GC_count=GC_count+1
GC_count_list.append(GC_count)
average_GC_Freed='%.2f'%float(GC_Freed_All/GC_count)
average_GC_per='%.2f'%float(GC_per_all/GC_count)
average_GC_time='%.2f'%float(GC_time_all/GC_count)
printGC_Freed_list,average_GC_Freed,GC_count_list,GC_per_list,average_GC_per,GC_time_list,average_GC_time
returnGC_Freed_list,average_GC_Freed,GC_count_list,GC_per_list,average_GC_per,GC_time_list,average_GC_time
defanalysisTrafficFile(packagename):
TrafficDataList=[]
TrafficDataList2=[]
TrafficCount=0
TrafficCountList=[]
fordatainopen('NetWorkFile.txt'):
ifpackagenameindata:
TrafficDataList.append(int(data.split(' ')[len(data.split(' '))-1]))
TrafficCount=TrafficCount+1
TrafficCountList.append(TrafficCount)
foriinTrafficDataList:
TrafficDataList2.append(float(i-TrafficDataList[0])/1024)
returnTrafficCountList,TrafficDataList2
defWriteLog(waittime):
try:
subprocess.Popen("adb logcat -v time -v threadtime *:D | grep GC>GCFile.txt",shell=True)
subprocess.Popen("adb logcat -v time *:E | grep Python2Android>NetWorkFile.txt",shell=True)
print'日志正在记录中。。。请稍后'
time.sleep(float(waittime)*3600)
exceptKeyboardInterrupt:
print'报告正在生成'
os.system('adb kill-server')
schedule=sched.scheduler(time.time, time.sleep)
defperform_command(cmd, inc):
os.system(cmd)
deftimming_exe(cmd, inc=60):
# enter用来安排某事件的发生时间,从现在起第n秒开始启动
schedule.enter(inc, 0, perform_command, (cmd, inc))
# 持续运行,直到计划时间队列变成空为止
schedule.run()
#timming_exe("echo %time%", 10)
if__name__=="__main__":
app_pid=getAppPID(sys.argv[1])
WriteLog(sys.argv[2])
TrafficCountList,TrafficDataList=analysisTrafficFile(sys.argv[1])
printTrafficCountList,TrafficDataList,'xxxx'
MakePDF(TrafficCountList,TrafficDataList,'average Traffic:'+'kb',"流量消耗.pdf")
GC_Freed_list,average_GC_Freed,GC_count_list,GC_per_list,average_GC_per,GC_time_list,average_GC_time=analysisGCFile(app_pid)
MakePDF(GC_count_list,GC_Freed_list,'average GC Freed:'+str(average_GC_Freed)+'kb',"GC_Freed报告.pdf")
MakePDF(GC_count_list,GC_per_list,'average GC per:'+str(average_GC_per)+'%',"GC_百分比报告.pdf")
MakePDF(GC_count_list,GC_per_list,'average GC time:'+str(average_GC_time)+'ms',"GC_时间消耗.pdf")