zoukankan      html  css  js  c++  java
  • 手机终端app内存CPU测试

    1、Android端内存测试,可以通过adb命令:

    adb shell dumpsys meminfo <package_name>

    Dalvik : dalvik使用的内存
    Native : native堆上的内存
    Other dev: 除了dalvik和native的内存
    Pss : 指将共享内存按比例分配到使用了共享内存的进程
    Alloc: 已使用的内存
    Free : 空闲的内存
    Private Dirty : 非共享,又不能被换页出去的内存
    Share Dirty : 共享,但有不能被换页出去的内存
    Total 的 PSS 信息,这个值就是应用正真占用的内存大小

    重点关注如下几个字段:
      1) Native/Dalvik 的 Heap 信息,具体在上面的第一行和第二行,如果发现这个值一直增长,则代表程序可能出现了内存泄漏。
      2) Total 的 PSS 信息,这个值就是你的应用真正占据的内存大小,通过这个信息,你可以轻松判别手机中哪些程序占内存比较大了。

    2、Android端CPU测试

    1)使用android提供的adb shell dumpsys cpuinfo |packagename来获取当前CPU使用率
    2)使用top命令 adb shell top |grep packagename 来获取实时CPU使用率

    top命令中每项参数含义:
    PID:应用程序在系统中的ID;
    CPU%:当前瞬时所以使用CPU占用率;
    S:进程的状态, 其中S表示休眠,R表示正在运行;
    Z:表示僵死状态,N表示该进程优先值是负数;
    #THR:程序当前所用的线程数;
    VSS : 虚拟耗用内存;
    RSS : 实际使用物理内存;
    PCY:前台(fg)和后台(bg)进程;
    UID:运行当前进程的用户ID;
    Name : 应用程序名称。

    可以通过Python2脚本输出内存和CPU情况:

    使用系统cmd命令,在一定时间内操作应用,实时监控内存CPU变化,最后计算出内存均值和峰值。

    # encoding:utf-8
    import os
    import tempfile
    import time
    import re
    
    def run_once_mem(cmd):
        #     out_file = 'mem_out.tmp.txt.%s' % time.time()
        #     err_file = 'mem_err.tmp.txt.%s' % time.time()
        #     cmd = '%(cmd)s 1>>%(out)s 2>>%(err)s' % {
        #             'cmd': cmd,
        #
        #             'out': out_file,
        #             'err': err_file
        #         }
        #     return_code = os.system(cmd)
        #
        #     f1 = open(out_file)
        #     stdout = f1.read()
        #     f2 = open(err_file)
        #     stderr = f2.read()
        #     f1.close()
        #     f2.close()
    
        result = os.popen(cmd)
        stdout = result.read()
        return stdout, 0, 0
    
    def run_once_cpu(cmd):
        #     out_file = 'cpu_out.tmp.txt.%s' % time.time()
        #     err_file = 'cpu_err.tmp.txt.%s' % time.time()
        #     cmd = '%(cmd)s 1>>%(out)s 2>>%(err)s' % {
        #             'cmd': cmd,
        #             'out': out_file,
        #             'err': err_file
        #         }
        #     return_code = os.system(cmd)
        #     f1 = open(out_file)
        #     stdout = f1.read()
        #     f2 = open(err_file)
        #     stderr = f2.read()
        #     f1.close()
        #     f2.close()
        result = os.popen(cmd)
        stdout = result.read()
        return stdout, 0, 0
    
    i = 0
    file_name = 'res2.txt'
    if os.path.exists(file_name):
        os.remove(file_name)
    # f = open(file_name, 'a+')
    summem = countmem = 0
    sumcpu = countcpu = 0
    time1 = time.time()
    tempcpu = 0
    tempmem = 0
    
    # import  os
    # result = os.popen("ipconfig")
    # print (result.read())
    while i < 250:#根据时间修改
        tempsum = 0
        i = i + 1
        #     time.sleep(1)
        cmdmem = 'adb shell dumpsys meminfo <package_name>'#修改自己的应用
        outmem, std_errmem, codemem = run_once_mem(cmdmem)
    
        cmdcpu = 'adb shell top  -n 1 -d 0.02 |findstr <package_name>'#修改自己的应用
        outcpu, std_err, codecpu = run_once_cpu(cmdcpu)
        #print(outcpu)
        print i
        # print out
        # print std_err
        # print code
    
        if codemem == 0:
            #         f.write(outmem)
            mem = re.compile('TOTAL[ ]+(d+)[ ]+.*')
            resmem = mem.findall(outmem)
            print (resmem)
            if len(resmem):
                summem += int(resmem[0])
                countmem += 1
                if (tempmem < int(resmem[0])):
                    tempmem = int(resmem[0])
    
        if codecpu == 0:
            cpu = re.compile('(d+)\%.*')
            rescpu = cpu.findall(outcpu)
            #print rescpu
            if len(rescpu):
                for iter in range(len(rescpu)):
                    tempsum = tempsum + float(rescpu[iter])
                print (tempsum)
                sumcpu += tempsum
                countcpu += 1
                if (tempcpu < tempsum):
                    tempcpu = tempsum
    time2 = time.time()
    time = time2 - time1
    
    print ('运行时间:' + str(time) + ' s')
    print ('内存均值:'+str(summem/countmem/1024.0)+' MB')
    print ('内存峰值:'+str(tempmem/1024.0)+' MB')
    print ('cpu均值:'+str(sumcpu/countcpu)+' %')
    print 'cpu峰值:'+str(tempcpu)+' %'

    3、iOS端内存CPU测试

    使用工具,手机连接电脑,使用xcode-open developer tool - instrument-activity monitor,选择对应的手机对应的应用。查看下方待测应用对应的 %CPU ,CPU time,Real Mem列。

    开始后,手机上按照测试用例操作待测应用;
    每操作一步,记录real Mem的值 即内存,查看%CPU的值,记录操作过程中的最大值,即峰值;
    操作一定时间后,如5分钟;
    CPU峰值即为操作过程中记录的最大值;
    CPU均值为CPU time/操作时间(S);
    内存峰值为操作过程中Real Mem记录的最大值;
    内存均值为操作过程中Ream Mem记录下所有值(比如N个值)的和/N。

    谢谢查看,持续修改完善!

    2019-10-05

  • 相关阅读:
    asp.net实现页面的一般处理程序(CGI)学习笔记
    .NET下的状态(State)模式 行为型模式
    (插件Plugin)AssemblyLoader解决方案(插件开发)
    SQL基础编写基本的SQL SELECT 语句
    在查询语句中使用NOLOCK和READPAST(ZT)
    C# 3.0语言增强学习笔记(一)
    ram,rom,flash
    自动激活你的ActiveX控件
    用C#编写ActiveX控件(二)
    用C#编写ActiveX控件(一)
  • 原文地址:https://www.cnblogs.com/yuntimer/p/11624245.html
Copyright © 2011-2022 走看看