Article Directory
1 Introduction
In the previous content, I have roughly introduced how python can realize the automatic operation of android devices.
Read the content of the file subdivided here and draw the corresponding graph according to the relevant data.
First of all, what I did in the last system was to automatically test the android device camera with python and record the contents of the PSS part of its memory.
We need to use related commands to save the PSS dump corresponding to the camera into a file.
The corresponding adb command is as follows, just embed this command into the python script.
adb shell dumpsys meminfo camerahalserver |grep -iE "TOTAL PSS" >> hal_PSS.txt
Then we write a function to read the content of this file, analyze the data and draw a picture.
Is not it simple.
2 read file
filename = "hal_PSS.txt" #文件名字,我这边是做成一个函数,所以我并不在乎实际的文件名。反正都可以随便拼接
Read the contents of the file line by line and operate in this loop:
for line in fileinput.input(filename):
Then we analyze the contents of the hal_PSS.txt file, the relevant contents are as follows:
TOTAL PSS: 79355 TOTAL RSS: 104520 TOTAL SWAP (KB): 0
TOTAL PSS: 79372 TOTAL RSS: 104520 TOTAL SWAP (KB): 0
TOTAL PSS: 86361 TOTAL RSS: 111872 TOTAL SWAP (KB): 0
TOTAL PSS: 91069 TOTAL RSS: 116888 TOTAL SWAP (KB): 0
TOTAL PSS: 360285 TOTAL RSS: 386116 TOTAL SWAP (KB): 0
TOTAL PSS: 351685 TOTAL RSS: 377516 TOTAL SWAP (KB): 0
TOTAL PSS: 348977 TOTAL RSS: 374808 TOTAL SWAP (KB): 0
TOTAL PSS: 359813 TOTAL RSS: 385644 TOTAL SWAP (KB): 0
TOTAL PSS: 89932 TOTAL RSS: 115696 TOTAL SWAP (KB): 0
TOTAL PSS: 92000 TOTAL RSS: 117764 TOTAL SWAP (KB): 0
TOTAL PSS: 91940 TOTAL RSS: 117704 TOTAL SWAP (KB): 0
TOTAL PSS: 92296 TOTAL RSS: 118060 TOTAL SWAP (KB): 0
TOTAL PSS: 92273 TOTAL RSS: 118080 TOTAL SWAP (KB): 0
TOTAL PSS: 92310 TOTAL RSS: 118124 TOTAL SWAP (KB): 0
TOTAL PSS: 92326 TOTAL RSS: 118140 TOTAL SWAP (KB): 0
TOTAL PSS: 92250 TOTAL RSS: 118064 TOTAL SWAP (KB): 0
TOTAL PSS: 92310 TOTAL RSS: 118124 TOTAL SWAP (KB): 0
TOTAL PSS: 92322 TOTAL RSS: 118136 TOTAL SWAP (KB): 0
What we need is the content of the first field after PSS .
First filter out the content after PSS and save it in textPSS.
textPSS = line.split('TOTAL PSS:')[1]
Ok, at this point, print out textPSS to see
79355 TOTAL RSS: 104520 TOTAL SWAP (KB): 0
79372 TOTAL RSS: 104520 TOTAL SWAP (KB): 0
86361 TOTAL RSS: 111872 TOTAL SWAP (KB): 0
91069 TOTAL RSS: 116888 TOTAL SWAP (KB): 0
360285 TOTAL RSS: 386116 TOTAL SWAP (KB): 0
351685 TOTAL RSS: 377516 TOTAL SWAP (KB): 0
348977 TOTAL RSS: 374808 TOTAL SWAP (KB): 0
359813 TOTAL RSS: 385644 TOTAL SWAP (KB): 0
89932 TOTAL RSS: 115696 TOTAL SWAP (KB): 0
92000 TOTAL RSS: 117764 TOTAL SWAP (KB): 0
91940 TOTAL RSS: 117704 TOTAL SWAP (KB): 0
92296 TOTAL RSS: 118060 TOTAL SWAP (KB): 0
92273 TOTAL RSS: 118080 TOTAL SWAP (KB): 0
92310 TOTAL RSS: 118124 TOTAL SWAP (KB): 0
92326 TOTAL RSS: 118140 TOTAL SWAP (KB): 0
92250 TOTAL RSS: 118064 TOTAL SWAP (KB): 0
92310 TOTAL RSS: 118124 TOTAL SWAP (KB): 0
92322 TOTAL RSS: 118136 TOTAL SWAP (KB): 0
Then remove the spaces to ensure that the numbers are stored in the first field of the array first
textPSS = textPSS.strip() #去掉空格
The printed display content is as follows:
79355 TOTAL RSS: 104520 TOTAL SWAP (KB): 0
79372 TOTAL RSS: 104520 TOTAL SWAP (KB): 0
86361 TOTAL RSS: 111872 TOTAL SWAP (KB): 0
91069 TOTAL RSS: 116888 TOTAL SWAP (KB): 0
360285 TOTAL RSS: 386116 TOTAL SWAP (KB): 0
351685 TOTAL RSS: 377516 TOTAL SWAP (KB): 0
348977 TOTAL RSS: 374808 TOTAL SWAP (KB): 0
359813 TOTAL RSS: 385644 TOTAL SWAP (KB): 0
89932 TOTAL RSS: 115696 TOTAL SWAP (KB): 0
92000 TOTAL RSS: 117764 TOTAL SWAP (KB): 0
91940 TOTAL RSS: 117704 TOTAL SWAP (KB): 0
92296 TOTAL RSS: 118060 TOTAL SWAP (KB): 0
92273 TOTAL RSS: 118080 TOTAL SWAP (KB): 0
92310 TOTAL RSS: 118124 TOTAL SWAP (KB): 0
92326 TOTAL RSS: 118140 TOTAL SWAP (KB): 0
92250 TOTAL RSS: 118064 TOTAL SWAP (KB): 0
92310 TOTAL RSS: 118124 TOTAL SWAP (KB): 0
92322 TOTAL RSS: 118136 TOTAL SWAP (KB): 0
The rest is to filter out the numbers
textPSS = re.findall("\d+\.?\d*",textPSS) #过滤出对应的数字
The printed content is shown as follows
['79355', '104520', '0']
['79372', '104520', '0']
['86361', '111872', '0']
['91069', '116888', '0']
['360285', '386116', '0']
['351685', '377516', '0']
['348977', '374808', '0']
['359813', '385644', '0']
['89932', '115696', '0']
['92000', '117764', '0']
['91940', '117704', '0']
['92296', '118060', '0']
['92273', '118080', '0']
['92310', '118124', '0']
['92326', '118140', '0']
['92250', '118064', '0']
['92310', '118124', '0']
['92322', '118136', '0']
We only need the content of the first paragraph of PSS. The following RSS and SWAP content are not required.
Since the unit of the dump is kb, it needs to be converted into MB by /1024, and loaded into y_list as the ordinate.
y = round(int(textPSS[0])/1024, 2)
y_list.append(y)
The abscissa is based on the number of lines. cnt is assigned a value of 0 before the loop starts, and the number of lines read increases by 1 and then given to x_list.
x = cnt + 1
x_list.append(x)
cnt = cnt+1
3 drawing
There are abscissa and ordinate. Then you can draw normally.
The entire drawing demo code is as follows.
#画图函数
def plot(filename):
global a
cnt = 0
y_list = list()
x_list = list()
for line in fileinput.input(filename):
#print(cnt+1,line)
#print("测试次数",cnt)
textPSS = line.split('TOTAL PSS:')[1]
textPSS = textPSS.strip() #去掉空格
textPSS = re.findall("\d+\.?\d*",textPSS) #过滤出对应的数字
y = round(int(textPSS[0])/1024, 2)
y_list.append(y)
x = cnt + 1
x_list.append(x)
cnt = cnt+1
print("总共测试",cnt,"次")
y_max = y_list.index(max(y_list))
#a.testCase为对应的测试模块/ open/photo/switch
print(a.testCase,"最大PSS测试是第",y_max+1,"次,测试PSS为",max(y_list),"MB")
#画图
plt.figure()
plt.scatter(x_list, y_list, c='red', s=10, label='mb')
plt.xticks(range(x_list[0], x_list[cnt-1], int(cnt/4)))
plt.yticks(range(0, int(max(y_list)+100), int((max(y_list)+100)/5)))
peaks, _ = find_peaks(y_list)
plt.xlabel("time", fontdict={
'size': 16})
plt.ylabel("PSS", fontdict={
'size': 16})
plt.title(a.testCase, fontdict={
'size': 20})
plt.legend(loc='best')
plt.savefig(outJPG)
The overall rendering effect is as follows: