移动性能测试 脚本获取 app 的 cpu、memory 信息,使用 pychartdir 生成图表

xuxu · 2015年01月29日 · 最后由 securitytest 回复于 2017年09月26日 · 2529 次阅读

又发帖了。。。

一直想弄个性能的脚本,今天实践了下,很简单,就是用 top 命令获取当前应用的 cpu、mem 信息,最后将结果生成图标,这里生成图标的话,使用了 pychartdir 模块,该模块使用起来还是比较方便的,尤其是帮助文档很全,包含各种 demo,就是使用的是未注册版本,底部有广告。

pychartdir 模块的安装不同于一般模块的安装,稍微有点麻烦,可参考:
http://blog.csdn.net/gb112211/article/details/43272049

#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 的信息时,处理空格好麻烦,求指点更好的解决办法!(已解决。哈哈)
我使用的是先分割,然后遍历列表,元素不为 “” 的话就将元素 append 到另一个列表里面,如此就可以获取固定位置的 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

下面的是绘制线性图表了,这里就不贴代码,代码放后面的链接中,先贴个生成的图表(android 的相机应用):

最后贴个脚本链接,有兴趣的可以尝试使用下:
https://github.com/gb112211/AndroidTestScripts/blob/master/python/get_cpu_mem_info.py

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 29 条回复 时间 点赞

#26 楼 @xuxu 如果横坐标换成 当前时间的话 有思路吗

xuxu #26 · 2016年07月19日 Author

@y693055797 你是需要下载什么?

#23 楼 @xuxu 这个要在那下载呢 度娘木有

图表是出来了,可是怎么知道 mem 或者 cpu 出现异常时的时间点和进行的哪部操作呢

xuxu #23 · 2015年08月18日 Author

@jiguanghover 你把整个 AndroidTestScripts 项目 Down 下来

问一下,scriptUtils 模块怎么无法导入呢,能否给个下载此模块的地址?谢谢

xuxu #21 · 2015年05月07日 Author

@xuxiujin 不是在 shell 里面运行。在里面的运行的话需要将 findstr 换成 grep

shell@hwH30:/ $ dumpsys window w|findstr\/|findstr name=
dumpsys window w|findstr\/|findstr name=
/system/bin/sh: findstr: not found
/system/bin/sh: findstr/: not found
127|shell@hwH30:/ $
果然是找不到的

xuxu #19 · 2015年05月06日 Author

@xuxiujin 手机打开你要获取 Activity 的应用,在 cmd 里面运行

adb shell dumpsys window w | findstr \/ | findstr name=

看看是否会有 Activity 输出,可能会有使用这个方法获取不到的时候。

你好,我运行的时候提示 f.write("Activity: \n%s\n" %utils.get_focused_package_and_activity()) 这个 IndexError: list index out of range 错误,是什么原因呀?谢谢

#2 楼 @xuxu 请教一下,为什么引用 scriptUtils 的 shell 方法执行下面,但读出来的数据是空。

memToal = until.shell("cat /proc/meminfo | grep MemTotal").stdout.readline()
xuxu #16 · 2015年04月24日 Author

@xuxiujin 不是的,不用填参数。会获取设备当前界面的 activity

你好,问个问题 pkg_name = utils.get_current_package_name() 这里是填写包名吗?

xuxu #14 · 2015年02月02日 Author

@284772894 想写就去写,不要被别人左右,服务器是很多这样的软件,自己写,最起码对代码的学习、性能数据的处理会有帮助,而同样的思路可以放在其他方面。

楼主,我一直想写个这样的脚本,但是有朋友告诉我,这样写没有意思,服务器有很多这样的软件可以监控这些信息了,希望得到楼主的回复,谢谢

这个帖子收藏了...结合评论~赞啊~

感谢楼上诸位大侠分享的 CPU,内存基础知识和绘图方法!~

pychartdir 建议改为 XlsxWriter,画出的图不是一个不可编辑的图片,而且和手工在 excel 绘制的效果一样。

xuxu #9 · 2015年01月30日 Author

@monkey 感谢那么晚还回帖!!看来先 split(“ ”),再处理列表是最稳妥的了

类似于这种

def cpu_analysis():
    _data_list =[]
    _cpudata_list = []
    cpu_total = 0
    cpu_file = Read_file()
    cpu_list = cpu_file.read_log(cpu_path)
    try:
        for i in xrange(len(cpu_list)):
            _data_list.append(cpu_list[i].split(" "))
        for i in xrange(len(_data_list)):
            for j in xrange(len(_data_list[i])):
                _noblank_list = [elem for elem in _data_list[i] if elem != ""]
                for k in xrange(len(_noblank_list)):
                    if 'CPU%' in _noblank_list[k]:
                        cpu_index = _noblank_list.index('CPU%')
                    if package_name in _noblank_list[k]:
                        _cpudata_list.append(_noblank_list[cpu_index][:-1])
                        cpu_total = cpu_total+int(_noblank_list[cpu_index][:-1])
        avg_cpu = cpu_total/len(_cpudata_list)
    except IndexError:
        print "日志文件不完整"
    return _cpudata_list,avg_cpu

不过我要提醒一点哦。不同手机出来的 top 的空格是不同的,split 的话肯定会有问题。所以我建议你用就是列的方式。代码我给你看下

xuxu #6 · 2015年01月29日 Author

@kasi OK 了~

不加符号的过滤 例如 s.split()

xuxu #4 · 2015年01月29日 Author

@kasi
中间的 split 后很多 ‘’。。

其实你命令过滤的时候使用 split()这个方法一般自动过滤掉空格了

xuxu #2 · 2015年01月29日 Author

@doctorq 这个好像可以,3500 1 1% S 26 1009348K 55968K fg u0_a15 com.android.camera
匹配%、K 结尾的部分,那只用取匹配结果列表中的 index 为 0 跟-1 位置的元素就 OK 了~。不过感觉正则处理也有点麻烦

用正则表达式试试

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册