Android CPU Memory 资源测试和分析 (使用工具 0xbench + python + vmstat)

最近综合使用工具对 Galaxy S2 运行各种资源的情况进行了分析,得到一些有趣的结论

工具:

0xbench: 开源的 bench mark 测试工具 ( 提供 2D, 3D 等test case)

vmstat; Android 自带统计 cpu, mem 占用的工具

python: 调用 adb 对数据进行收集, 并画出直观图表


度量指标的选取:

memory:  选择 vmstat 的  anon   ( 匿名内存, 就是 heap和 stack 的内存占用量)

cpu: 选择 100 - id ( vmstat 中 id 是 idle 在总cpu时间的百分数,  100 - id 就是大致是  cpu 运行的时间)


实验结果如下

一般程序启动

使用 intent: org.zeroxlab.zeroxbenchmark/.ActivitySettings

( 本图最后的斜坡, 可能是 Android进行资源回收,不用管它, 就看前5秒的)


占用不到 6% 符合预期



内存增加1000kB

刷屏 2D

使用 intent: org.zeroxlab.zeroxbenchmark/org.zeroxlab.zeroxbenchmark.TesterCanvas




CPU 在程序运行时,负荷重, 内存增加3M

魔方 3D

使用org.zeroxlab.zeroxbenchmark/org.zeroxlab.kubench.Kubench, 程序演示一个魔方转动







程序初始化的时候, CPU, 内存消耗很高, 而后减少了5M, CPU在3D绘图中消耗不多


视频播放

0xBench没有相关 case, 直接编写一个基于videoview 简单的播放程序

Sample视频 480*320 视频avc编码, 音频aac 编码







CPU 的消耗在40%, 内存增量不到1M


结论:

1) 因为有了 GPU 加速,  3D对CPU 消耗不大

2) 视频播放对 CPU 消耗大


下面的代码供参考, 可以改进进行更细的分析

#!/usr/bin/python

import os, sys, time, re, threading, subprocess
import numpy as np
import matplotlib.pyplot as plt

ACTS = {
'setting':'org.zeroxlab.zeroxbenchmark/.ActivitySettings', 
'2D':'org.zeroxlab.zeroxbenchmark/org.zeroxlab.zeroxbenchmark.TesterCanvas -e ROUND 1 -e INDEX 0 -e SOURCE  Canvas',
'3D':'org.zeroxlab.zeroxbenchmark/org.zeroxlab.kubench.Kubench -e ROUND 1 -e INDEX 0 -e SOURCE  Kubench',
'video':'com.my.perftest/.VideoActivity'
}


ACT_RUNTIME_DEFAUT=10
ACT_INTERVAL=2

ACTS_TIME = {
'video':30,
'2D':10,
'3D':30, 
}

ACTS_TIME = {
'video':30,
'2D':10,
'3D':30, 
}

PLOT_MAP = {
'anon':'mem',
}
PLOT_UNIT_MAP = {
'anon':'kB',
'cpu':'%',
}


def runVMSTAT(test):
	vmstat_time = ACT_RUNTIME_DEFAUT;
	if ACTS_TIME.has_key(test):
		vmstat_time = ACTS_TIME[test]
	
	cmd = 'adb shell "vmstat & VST=$!; sleep ' + str(vmstat_time) +  ' ;kill -9 $VST"'
	logfile = open("vmstat_raw.txt", 'w')
	p = subprocess.Popen( cmd, shell=True, universal_newlines=True, stdout=logfile )
	ret_code = p.wait()
	logfile.flush()
	logfile.close()

def plotPng(file_name):
	f = open(file_name)
	labels = [item for item in f.readline().strip().split() if item]
	feed_data = {}
	for line in f:
		line = line.strip()
		if line:
			lineList = [item for item in line.split() if item]
			for i, item in enumerate(lineList):
				feed_data.setdefault(labels[i], []).append( int(item))
	 
	f.close()
	feed_data["cpu"] = map(lambda x:100-x, feed_data["id"])

	draw_data = ["cpu","anon"]
	for key in draw_data:
			label = key
			if PLOT_MAP.has_key(key):
				label = PLOT_MAP[key]
			plt.figure(label)
			plt.clf()
			plt.plot(feed_data[key], 'go-')
			plt.title(label)
			plt.xlabel("seconds")
			if PLOT_UNIT_MAP.has_key(key):
				plt.ylabel(PLOT_UNIT_MAP[key])
			plt.savefig(label+".png")
	
def runTest(test):
	target_dir = time.strftime('%Y%m%d%H%M')
	os.mkdir(test)
	os.chdir(test)
	print "Collecting vmstat data for " + test + ", Please waiting ...\n"

	vmstat_thr = threading.Thread(target = runVMSTAT,args=(test,)) 
	vmstat_thr.start()

	time.sleep(3)
	os.system('adb shell am start -n ' + ACTS[test] )
	vmstat_thr.join()
	
	rawf = open('vmstat_raw.txt', 'r')
	vmstat_raw_lines = rawf.readlines()
	
	of = open('vmstat_feed.txt', 'w')
	
	is_header_read = 0;
	for line in vmstat_raw_lines:
			if (is_header_read == 0):
					if re.search(r'^\s*r\s*b', line) != None:
							of.write(line)
							is_header_read =1;
			else:
					if re.search(r'^\s[0-9]+\s\s[0-9]{1,}', line) != None:
							of.write(line)
	rawf.close()
	of.close()
	plotPng('vmstat_feed.txt')
	os.chdir("../")


target_dir = time.strftime('%Y%m%d%H%M%S')
os.mkdir(target_dir)
os.chdir(target_dir)
if (os.system('adb shell cat /proc/cpuinfo > cpu_feed.txt') != 0):
	print "Please connect adb first!"
	exit(1)
os.system('adb shell cat /proc/meminfo > mem_feed.txt')

tests = sys.argv[1:]
print "you can run test in "+str(ACTS.keys())

if len(tests) == 0:
	print "Not sepecify test, Run default test 'setting'"
	runTest("setting")
	exit(0);   

for test in tests:
	if ACTS.has_key(test):
		runTest(test)
		time.sleep(ACT_INTERVAL)
	else:
		print "There is no test with the name of" + test
	
	



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值