qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          Android 應用性能測試方案一之 log 分析

           今天我主要來說下過年時候自己做的一些性能測試,由于時間緊迫,所以最終選擇了全部從log方面入手,從而最終達到一氣呵成的效果。
            分別有這樣幾個大項:
            1. Android應用啟動消耗時間
            我們分別在Activity的生命周期方法內添加Log.e(tag,message),如下效果:
          @Override
          public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          Log.e("AppStartTime","AppOnCreate");
          ...
          }
          @Override
          protected void onResume() {
          super.onResume();
          Log.e("AppStartTime","AppOnResume");
          ...
          }
            ,這里的tag我們使用AppStartTime,那么我們需要在應用啟動之后在command內輸入:
            adb logcat -v time -v threadtime *:E | grep ActivityStartTime>StartTimeFile.txt
            2. cpu和內存消耗
            在command中輸入如下命令:
            adb shell top -n 400 | grep <your package name>Cpu_MemoryFile.txt
            3. GC
            在command中輸入如下命令:
            adb logcat -v time -v threadtime *:D | grep GC>GCFile.txt
            這里需要注意的是,GC分析的時候需要關注三個值。
            average_GC_Freed
            average_GC_per
            average_GC_time
            4. 網絡流量
            在被測應用中增加一個獲取所有應用的網絡流量的service,添加一個getAppTrafficList( )方法,代碼如下:
          publicvoidgetAppTrafficList(){
          PackageManagerpm=getPackageManager();
          List<PackageInfo>pinfos=pm
          .getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES
          |PackageManager.GET_PERMISSIONS);
          for(PackageInfoinfo:pinfos){
          String[]premissions=info.requestedPermissions;
          if(premissions!=null&&premissions.length>0){
          for(Stringpremission:premissions){
          if("android.permission.INTERNET".equals(premission)){
          intuId=info.applicationInfo.uid;
          longrx=TrafficStats.getUidRxBytes(uId);
          longtx=TrafficStats.getUidTxBytes(uId);
          if(rx<0||tx<0){
          continue;
          }else{
          Log.e("網絡流量",info.applicationInfo.loadLabel(pm)+Formatter.formatFileSize(this,rx+tx)
          }
          }
          }
          }
          }
          }
           如果還要其他數據,那么全部可以按照以上的方法去獲取。然后我們來看如何使用python一次性分析這些文件從而直接獲取report。
            首先引入第三方繪制pdf的模塊:
            # -*- coding: utf-8 -*-
            from reportlab.graphics.shapes import *
            from reportlab.graphics.charts.lineplots import LinePlot
            from reportlab.graphics.charts.textlabels import Label
            from reportlab.graphics import renderPDF
            然后我們需要一個讀文件的方法:
            def FileRead(path):
            data_list = []
            number_list = []
            number = 0
            for line in open(path):
            data_list.append(line)
            number =number+1
            number_list.append(number)
            return data_list,number_list
            接著我們需要一個制作pdf的方法:
          def MakePDF(times,list,reportname,pdfname):
          drawing = Drawing(500,300)
          lp = LinePlot()
          lp.x = 50
          lp.y = 50
          lp.height = 125
          lp.width = 300
          lp.data = [zip(times, list)]
          lp.lines[0].strokeColor = colors.blue
          lp.lines[1].strokeColor = colors.red
          lp.lines[2].strokeColor = colors.green
          drawing.add(lp)
          drawing.add(String(350,150, reportname,fontSize=14,fillColor=colors.red))
          renderPDF.drawToFile(drawing,pdfname,reportname)
          #這里的times和list兩個參數都是list,是時間和監控獲取的數據一一對應的關系
          這些我們都有了之后,我們來看下分析AppStartTime的方法:
          def analysisStartFile(list):
          totalcount =0
          totaltime =0
          time_list =[]
          totalcount_list = []
          for i in range(len(list)):
          if 'AppStartTime' in list[i]:
          totalcount =totalcount+1
          totalcount_list.append(totalcount)
          if float(list[i+4].split(' ')[1][-6:])-float(list[i].split(' ')[1][-6:])>0:
          totaltime=totaltime+float(list[i+4].split(' ')[1][-6:])-float(list[i].split(' ')[1][-6:])
          time_list.append(float(list[i+4].split(' ')[1][-6:])-float(list[i].split(' ')[1][-6:]))
          return totalcount_list,'%.2f'%float(totaltime/totalcount),time_list
            所有的分析數據的思維都是使用split()方法分隔空格之后做分析。因為讀取文件之后是將所有的數據存在list中,但是當我們去用的時候由于空格在其中就變得非常的麻煩,那么我們可以先使用split將空格去掉,然后使用if key in list的方法進行過濾再做分析。
            最后在main()方法中基本就是如下的順序執行方法:
            if __name__== '__main__':
            list1,list2 = FileRead(<your file path>)
            print list1,list2
            list_count,average_start_time,time_list = analysisStartFile(list1)
            MakePDF(list_count,time_list,'average time:'+str(average_start_time)+'s',"啟動性能報告.pdf")
            最終我們就能夠批量的生成如下圖的報告了。

          posted on 2014-12-03 13:49 順其自然EVO 閱讀(312) 評論(0)  編輯  收藏 所屬分類: 測試學習專欄

          <2014年12月>
          30123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 明光市| 岐山县| 河津市| 拜泉县| 旌德县| 达拉特旗| 黄龙县| 永安市| 仁化县| 社会| 徐州市| 绥宁县| 新安县| 丁青县| 桃园市| 玉田县| 阳高县| 娱乐| 阿拉尔市| 肥乡县| 双峰县| 信阳市| 尤溪县| 奇台县| 屯昌县| 长乐市| 永寿县| 吉安县| 保定市| 宝兴县| 嘉定区| 从江县| 明水县| 涪陵区| 台北市| 株洲市| 罗城| 唐山市| 和龙市| 石门县| 金川县|