<h1>Android自動化性能收集</h1>html
<p>Android 功能測試自動化框架較多,UIAutomator,Robotium,Appium等。Case執行過程當中,可能但願收集手機的性能指標,包括內存、cpu、流量等。使用java+shell+bat簡單實現了android手機性能收集。</p>java
<h2>簡述</h2>linux
<p>過程很簡單:</p>android
<ul> <li><p>在啓動自動化case前,執行收集信息的命令。<br/> 其實就是一些adb shell命令,以下:</p>git
<pre><code> adb shell top -n 1| awk '{print $3" "$10}' >> cpu.dat adb shell ps | awk '{print $5" "$9}' >> mem.dat //android sdk level 大於16 adb shell cat /proc/uid_stat/$uid/tcp_rcv >> $uid"_recv.dat" adb shell cat /proc/uid_stat/$uid/tcp_snd >> $uid"_snd.dat" //android sdk level 小於16 adb shell cat /proc/$pid/net/dev | grep wlan | awk '{print $2" "$10}' >> $pid"_net.dat" </code></pre></li>github
<li><p>執行自動化case,可能須要好久。這個過程當中,上面的命令在不斷地執行</p></li> <li>case執行結束,kill掉第一步中的命令</li> <li>分析收集到的dat文件,使用jscharts繪出走勢圖</li> </ul>shell
<h2>腳本</h2>windows
<h3>cpu</h3>api
<p>使用top命令不斷查看各進程的cpu佔用</p>app
<h4>linux</h4>
<p>get-android-cpu.sh</p>
<pre><code> #!/bin/sh #path--target/android-info/mem/dat cd ../../../ mkdir -p target/android-info/cpu/dat mkdir -p target/android-info/cpu/html cp src/main/resources/jscharts.js target/android-info/cpu/html cd target/android-info/cpu/dat while true do adb shell top -n 1| awk '{print $3" "$10}' >> cpu.dat sleep 15 done </code></pre>
<h4>windows</h4>
<p>get-android-cpu.bat</p>
<pre><code> cd ..\..\jenkins\workspace\android-info-end3 mkdir target\android-info\cpu\dat mkdir target\android-info\cpu\html copy src\main\resources\jscharts.js target\android-info\cpu\html cd target\android-info\cpu\dat :run adb shell top -n 1 | awk "{print $3\" \"$10}" >> cpu.dat ping 127.0.0.1 -n 15 > null goto run </code></pre>
<h3>memory</h3>
<p>使用ps命令不斷查看各進程的內存佔用</p>
<h4>linux</h4>
<p>get-android-mem.sh</p>
<pre><code> #!/bin/sh #path--target/android-info/mem/dat cd ../../../ mkdir -p target/android-info/mem/dat mkdir -p target/android-info/mem/html cp src/main/resources/jscharts.js target/android-info/mem/html cd target/android-info/mem/dat while true do adb shell ps | awk '{print $5" "$9}' >> mem.dat sleep 15 done </code></pre>
<h4>windows</h4>
<pre><code> cd ..\..\jenkins\workspace\android-info-end3 mkdir target\android-info\mem\dat mkdir target\android-info\mem\html copy src\main\resources\jscharts.js target\android-info\mem\html cd target\android-info\mem\dat :run adb shell ps | awk "{print $5\" \"$9}" >> mem.dat ping 127.0.0.1 -n 15 > null goto run </code></pre>
<h3>流量</h3>
<p>android 4.0以上版本能夠用<code>/proc/uid_stat/$uid/tcp_rcv</code>和<code>/proc/uid_stat/$uid/tcp_snd</code>來獲取某個程序的上下行流量;而4.0如下版本要用<code>cat /proc/$pid/net/dev</code>來查看上下行流量。uid和pid的關係,能夠從<code>/data/system/packages.list</code>這個文件中獲取。</p>
<h4>linux</h4>
<p>判斷android api level:</p>
<pre><code> #!/bin/sh #android 4.0以上和4.0如下方法不一樣 #get android sdk level apileveltemp=`adb shell getprop | grep ro.build.version.sdk` apilevel=${apileveltemp:25:2} chmod +x *.sh echo "android api level:"$apilevel if [ $apilevel -gt 14 ] then ./get-android-net-gt-4.0.sh elif [ $apilevel -lt 14 ] then ./get-android-net-lt-4.0.sh fi </code></pre>
<p>get-android-net-gt-4.0.sh</p>
<pre><code> #!/bin/sh #android api level great than 14(android 4.0) #path--target/android-info/net/dat cd ../../../ mkdir -p target/android-info/net/dat mkdir -p target/android-info/net/html cp src/main/resources/jscharts.js target/android-info/net/html cd target/android-info/net/dat cd .. echo "adb pull/data/system/packages.list--start" adb pull /data/system/packages.list cd dat while true do echo "get net info from /proc/uid-stat/$uid" for i in `adb shell ls /proc/uid_stat` do #delete the Enter character uid=`echo $i | tr -d ["\r\n"]` adb shell cat /proc/uid_stat/$uid/tcp_rcv >> $uid"_recv.dat" adb shell cat /proc/uid_stat/$uid/tcp_snd >> $uid"_snd.dat" done sleep 15 done </code></pre>
<p>get-android-net-lt-4.0.sh</p>
<pre><code> #!/bin/sh #path--target/android-info/net/dat cd ../../../ mkdir -p target/android-info/net/dat mkdir -p target/android-info/net/html cp src/main/resources/jscharts.js target/android-info/net/html cd target/android-info/net/dat #get /proc/$pid/net/dev while true do #get pid for i in `adb shell ps | awk '{print $2}'` do pid=`echo $i | tr -d ["\r\n"]` echo $pid adb shell cat /proc/$pid/net/dev | grep wlan | awk '{print $2" "$10}' >> $pid"_net.dat" done sleep 15 done </code></pre>
<h4>windows</h4>
<p>bat命令不熟,不知道如何在windows下實現linux下地反轉義,這裏用java代碼實現。</p>
<pre><code> import java.io.*; /** * Created by Xuemeng Wang on 14-9-15. * api Level > 16 */ public class GetNetInfo { public static void main(String[] args) { String uidString = execCmd("adb shell ls /proc/uid_stat"); String[] uidArray = uidString.split("\n"); int length = uidArray.length-1; for(int i=0;i<=length-1;i++) { String contentRcv = execCmd("adb shell cat /proc/uid_stat/"+uidArray[i]+"/tcp_rcv"); String contentSnd = execCmd("adb shell cat /proc/uid_stat/"+uidArray[i]+"/tcp_snd"); System.out.println(System.getProperty("user.dir")); try { File file = new File(uidArray[i]+"_recv.dat"); if(!file.exists()) file.createNewFile(); File file2 = new File(uidArray[i]+"_snd.dat"); if(!file2.exists()) file2.createNewFile(); FileWriter fileWriter1 = new FileWriter(file, true); FileWriter fileWriter2 = new FileWriter(file2, true); fileWriter1.write(contentRcv); fileWriter2.write(contentSnd); fileWriter1.close(); fileWriter2.close(); } catch (IOException e) { e.printStackTrace(); } } } public static String execCmd(String command) { BufferedReader br = null; StringBuffer stringBuffer = new StringBuffer(); try { Process p = Runtime.getRuntime().exec(command); br = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = null; while ((line = br.readLine()) != null) { if("".equals(line.trim())) continue; stringBuffer.append(line+"\n"); } } catch (Exception e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (Exception e) { e.printStackTrace(); } } } return stringBuffer.toString(); } } </code></pre>
<p>api level小於16的狀況,我沒用到,略掉:)。</p>
<h3>殺掉進程</h3>
<p>linux: <code>ps -ef | grep get-android- | grep -v grep | awk '{print $2}' | xargs kill -9</code><br/> windows(後臺運行,實際上是cmd進程,注意下面會殺掉全部的cmd進程):</p>
<pre><code> taskkill /F /IM cmd.exe taskkill /F /IM adb.exe </code></pre>
<h2>結果收集</h2>
<p>使用java分析dat,給出分析cpu的代碼,工程已上傳到github,<a href="https://github.com/yeetrack/android-performance">https://github.com/yeetrack/android-performance</a>。</p>
<p>CpuInfo.java</p>
<pre><code> package com.meilishuo.android.performance; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.testng.annotations.Test; import java.io.*; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.TreeMap; /** * Created by victor on 14-9-9. */ public class CpuInfo { private static final Logger logger = LoggerFactory.getLogger(MemInfo.class); private static final String JSCHARTPATH = "target/android-info/cpu/html/"; private static final String MEMFILEPATH = "target/android-info/cpu/dat/cpu.dat"; TreeMap<String, List<String>> cpuMap = new TreeMap<String, List<String>>(); /** * 解析cpu.dat存儲到map中 */ public void parseCpuFile() { File file = new File(MEMFILEPATH); if(null==file) return; try { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file))); String line = null; while((line=bufferedReader.readLine())!=null) { String[] array = line.trim().split(" "); //空格分隔 if(null==array || array.length!=2 || !array[0].contains("%") || "".equals(array[1]) || "Name".equals(array[1])) continue; if(cpuMap.size()==0 || !cpuMap.containsKey(array[1])) { List<String> memList = new ArrayList<String>(); memList.add(array[0].substring(0, array[0].indexOf("%"))); cpuMap.put(array[1], memList); } else { cpuMap.get(array[1]).add(array[0].substring(0, array[0].indexOf("%"))); } } System.out.println(cpuMap.size()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 將map中的數據寫到到xml中 */ public void writeXmlFromMap() { if(null==cpuMap || cpuMap.size()==0) return; Iterator<String> it = cpuMap.keySet().iterator(); while(it.hasNext()) { String key = it.next(); List<String> value = cpuMap.get(key); if(null==value || value.size()<=1) continue; //寫入xml File file = new File(JSCHARTPATH+key.replace("/", "_").replace(":", "_")+"_cpu.xml"); try { BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true))); bufferedWriter.write( "<?xml version=\"1.0\"?>\n" + "<JSChart>\n" + "\t<dataset type=\"line\">"); int pos = 1; for(String index : value) { bufferedWriter.write("<data unit=\""+pos+"\" value=\""+index+"\"/>\n"); pos++; } bufferedWriter.write( "</dataset>\n" + "\t<optionset>\n" + "\t\t<option set=\"setLineColor\" value=\"'#8D9386'\"/>\n" + "\t\t<option set=\"setLineWidth\" value=\"4\"/>\n" + "\t\t<option set=\"setTitleColor\" value=\"'#7D7D7D'\"/>\n" + "\t\t<option set=\"setAxisColor\" value=\"'#9F0505'\"/>\n" + "\t\t<option set=\"setGridColor\" value=\"'#a4a4a4'\"/>\n" + "\t\t<option set=\"setAxisValuesColor\" value=\"'#333639'\"/>\n" + "\t\t<option set=\"setAxisNameColor\" value=\"'#333639'\"/>\n" + "\t\t<option set=\"setTextPaddingLeft\" value=\"0\"/>\n" + "\t</optionset>\n " + "</JSChart>"); bufferedWriter.flush(); bufferedWriter.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } /** * 生成jschart html文件 */ public void writeJsHtml() { File file = new File(JSCHARTPATH); File[] xmlFiles = file.listFiles(new FileFilter() { @Override public boolean accept(File pathname) { return pathname.getName().endsWith("_cpu.xml"); } }); if(null== xmlFiles || xmlFiles.length==0) return; for(File index : xmlFiles) { File htmlFile = new File(index.getAbsolutePath().replace(".xml", ".html")); try { BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(htmlFile)); </code></pre>