Android app performance-automated testing (unofficial, very practical)

I will introduce the performance of my Android app from the following aspects -automated testing
1. Why do app performance tests?
2. What does app performance test include?
3. What are the common methods / tools for testing app performance?
4. How to automate app performance testing?
5. Results display

  1. Why do app performance tests?
    App performance testing is an important part of improving product quality.
    Especially in the current fierce competition in the app market, if the app is always stuck, flashed back, and severely burned during the use of the app, it is estimated that it will be uninstalled.

  2. What are the app performance tests?
    Memory, CPU, flow, fluency, power consumption, response speed, weak network test, etc.

  3. What are the common methods / tools for testing app performance?
    The mobile system comes with fluency, power consumption and flow detection, adb command, Tencent GT (requires root), Emmagee (does not support 7.0 or more), capture audio and video packets and then calculate the frame rate and code rate.

  4. How to automate app performance testing?
    The adb command is actually very powerful, and there are commands for viewing various performances. However, it is inconvenient to use the command line by line. If you want to test systematically and output the results in batches for data analysis, you must definitely consider automation.
    I am using java embedded adb command.

  5. The results show the
    memory memory test as an example:

(1) Write a configuration file packageName.properties to store the package name of the app and place it under the src of the project, so that when testing different apps, you only need to change the configuration file.
Write a tool class class ReadPackName to specifically read the configuration file
Insert picture description here

public class ReadPackName {

    //读配置文件中的包名
    public static final String [] message = readName();

    private static String[] readName() {
        Properties prop=new Properties();
        String [] message=new String[1];
        int i=0;
        try {
            InputStream in=new BufferedInputStream(new FileInputStream("src/packageName.properties"));
            prop.load(in);
            message[0]=prop.getProperty("packName");

            in.close();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return message;
    }

(2) Write a class TestMemory, execute the adb command to test memory

class TestMemory {

    private static final String[] myPackage = ReadPackName.message;

    public static void  TeM(){
        Process p1;
        Process p2;
        String adb_path = "adb";

            //用adb命令查看包名的内存,将结果存为Menory.txt放在手机里
            try{
                p1=Commons.excuteShell(adb_path+" shell dumpsys meminfo "+myPackage[0]+" > /sdcard/Menory.txt");    
                Thread.sleep(3000);
                }catch (Exception e)
                {
                    System.out.println("error");
                }
				//把手机里的日志导入到电脑D盘中
                p2=Commons.excuteShell(adb_path+" pull /mnt/sdcard/Menory.txt  D:/Menory.txt");  
                System.out.println("-----getMenory----success!---------");


                         }

(3) Write a class CatchMemoryTotal and read the desired value of D: /Menory.txt.
As shown in the figure, what I want is the total value of memory.
Insert picture description here

public class CatchMemoryTotal {

    String total=null;
    public String getNum(){
    String s = null,s2 = null;

        BufferedReader readFile = null;
        try {
            readFile = new BufferedReader(new FileReader("D://Memory.txt"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        while(true) {
            try {
                if (!((s = readFile.readLine()) != null)) break;
            } catch (IOException e) {
                e.printStackTrace();
            }
            s2 += s + "\n";
        }
        if ( s2.contains("TOTAL:")){
            String[] use= s2.split("TOTAL:");    //根据唯一字符,一段段切,直到取到文本中想要的值
            String[] realuse = use[1].split("TOTAL SWAP PSS:");

            total=realuse[0].trim(); //去掉空格
       
            System.out.println("--------------"+total+"-------------- " );
           // System.out.println(s2);

        }
        try {
            readFile.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return total;
   }

(4) Write a class CreatExcel, save the obtained value to the total value, and save it to the excel table, which is convenient for data analysis or interface image output in the future.
Insert picture description here

public class CreatExcel {
    /**
     * @功能:手工构建一个简单格式的Excel
     */
    public static String getMe()
    {
        CatchMemoryTotal catchMemoryTotal = new CatchMemoryTotal();
        String a = catchMemoryTotal.getNum();
        System.out.println("--------已保存为excel表---------------");
        return a;
    }

    public static void main(String[] args) throws Exception
    {
        // 第一步,创建一个webbook,对应一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet("表一");       //表名
        // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
        HSSFRow row = sheet.createRow((int) 0);
        // 第四步,创建单元格,并设置值表头 设置表头居中
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

        HSSFCell cell = row.createCell((short) 0);
        cell.setCellValue("内存总消耗");      //列名
        cell.setCellStyle(style);

        // 第五步,写入实体数据 
            row = sheet.createRow(1);
            row.createCell((short) 0).setCellValue((String) CreatExcel.getMe());
            
        // 第六步,将文件存到指定位置D:/Members.xls
        try
        {
            FileOutputStream fout = new FileOutputStream("D:/Members.xls");
            wb.write(fout);
            fout.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

This little white java level is average, the code is relatively simple and a bit messy. Slowly optimize later! I hope this blog post will be helpful or inspiring for everyone to do Android performance automation. You are welcome to make progress with me and learn about testing techniques with each other.

Published 22 original articles · praised 5 · visits 4311

Guess you like

Origin blog.csdn.net/weixin_42231208/article/details/99072416