手機終端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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM