forked from gb112211/AndroidTestScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_cpu_mem_info.py
More file actions
116 lines (84 loc) · 2.96 KB
/
get_cpu_mem_info.py
File metadata and controls
116 lines (84 loc) · 2.96 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2015年1月28日
@author: xuxu
'''
#需要安装pychartdir模块,http://blog.csdn.net/gb112211/article/details/43272049
import string
from scriptUtils import utils
from pychartdir import *
PATH = lambda p: os.path.abspath(p)
#打开待测应用,运行脚本,默认times为20次(可自己手动修改次数),获取该应用cpu、memory占用率的曲线图,图表保存至chart目录下
#top次数
times = 20
#设备当前运行应用的包名
pkg_name = utils.get_current_package_name()
#获取cpu、mem占用
def top():
cpu = []
mem = []
top_info = utils.shell("top -n %s | %s %s$" %(str(times), utils.find_util, pkg_name)).stdout.readlines()
for info in top_info:
#temp_list = del_space(info)
temp_list = info.split()
cpu.append(temp_list[2])
mem.append(temp_list[6])
return (cpu, mem)
#去除top信息中的空格,便于获取cpu、mem的值
#def del_space(str):
# temp_list1 = str.split(" ")
# temp_list2 = []
#
# for str in temp_list1:
# if str != "":
# temp_list2.append(str)
#
# return temp_list2
#绘制线性图表,具体接口的用法查看ChartDirecto的帮助文档
def line_chart():
data = top()
cpu_data = []
mem_data = []
#去掉cpu占用率中的百分号,并转换为int型
for cpu in data[0]:
cpu_data.append(string.atoi(cpu.split("%")[0]))
#去掉内存占用中的单位K,并转换为int型,以M为单位
for mem in data[1]:
mem_data.append(string.atof(mem.split("K")[0])/1024)
#横坐标
labels = []
for i in range(1, times + 1):
labels.append(str(i))
#自动设置图表区域宽度
if times <= 50:
xArea = times * 40
elif 50 < times <= 90:
xArea = times * 20
else:
xArea = 1800
c = XYChart(xArea, 800, 0xCCEEFF, 0x000000, 1)
c.setPlotArea(60, 100, xArea - 100, 650)
c.addLegend(50, 30, 0, "arialbd.ttf", 15).setBackground(Transparent)
c.addTitle("cpu and memery info(%s)" %pkg_name, "timesbi.ttf", 15).setBackground(0xCCEEFF, 0x000000, glassEffect())
c.yAxis().setTitle("The numerical", "arialbd.ttf", 12)
c.xAxis().setTitle("Times", "arialbd.ttf", 12)
c.xAxis().setLabels(labels)
#自动设置X轴步长
if times <= 50:
step = 1
else:
step = times / 50 + 1
c.xAxis().setLabelStep(step)
layer = c.addLineLayer()
layer.setLineWidth(2)
layer.addDataSet(cpu_data, 0xff0000, "cpu(%)")
layer.addDataSet(mem_data, 0x008800, "mem(M)")
path = PATH("%s/chart" %os.getcwd())
if not os.path.isdir(path):
os.makedirs(path)
#图片保存至脚本当前目录的chart目录下
c.makeChart(PATH("%s/%s.png" %(path, utils.timestamp())))
if __name__ == "__main__":
print "Starting get top information..."
line_chart()