如何获取手机性能测试数据FPS

本人在做APP性能测试的过程中,为了测试APP在各个场景下的流畅度,需要收集手在各个运行场景下的fps数据,经常查资料,使用的是adb shell命令:

adb shell dumpsys gfxinfo 包名 分享代码,供大家参考。 测试方法:

        Fps fps = new Fps();
            fps.start();
            //do something
            fps.stopFps();
            fps.join();

多线程类的代码:

package monkeytest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import source.AppLocalMySql;
import source.Common;

public class Fps extends Thread {
    private boolean KEY = true;
    private static String testName = "normal";
    private int mark = Common.mark;

    @Override
    public void run() {
        while (KEY) {
            execCmdAdb("adb shell dumpsys gfxinfo com.happyjuzi.apps.juzi");
            Common.getInstance().sleep(5000);
        }
    }

    public int getMatcher(String text, Pattern pattern) {
        Pattern newattern = Pattern.compile(pattern.toString().trim());
        Matcher matcher = newattern.matcher(text);
        List<String> numbers = new ArrayList<>();
        while (matcher.find()) {
            String number = matcher.group(0);
            numbers.add(number);
        }
        double result = 0;
        for (int i = 0; i < numbers.size(); i++) {
            double num = Common.getInstance().changeStringToDouble(numbers.get(i));
            result += num;
        }
        int total = (int) result + new Random().nextInt(2);
        return total;
    }

    /**
     * 结束线程
     */
    public void stopFps() {
        this.KEY = false;
    }

    /**
     * 执行cmd命令
     * 
     * @param cmd
     *            命令
     */
    public void execCmdAdb(String cmd) {
        Pattern pattern = Pattern.compile(" ([0-9]{1,2}+\\.[0-9]{2})");
        output("正在执行:" + cmd);
        String OSname = System.getProperty("os.name");
        try {
            Process p = null;
            if (OSname.contains("Mac")) {
                p = Runtime.getRuntime().exec(Common.ADB_PATH + cmd);
            } else {
                p = Runtime.getRuntime().exec("cmd /c " + cmd);
            }
            // 正确输出流
            InputStream input = p.getInputStream();// 创建并实例化输入字节流
            BufferedReader reader = new BufferedReader(new InputStreamReader(input));// 先通过inputstreamreader进行流转化,在实例化bufferedreader,接收内容
            String line = "";
            while ((line = reader.readLine()) != null) {// 循环读取
                if (line.startsWith("   ")) {
                    Matcher matcher = pattern.matcher(line);
                    if (matcher.find()) {
                        AppLocalMySql.getInstance().saveFps(testName, mark);
                        getMatcher(line, pattern);// 输出
                    }
                }
            }
            reader.close();// 此处reader依赖于input,应先关闭
            input.close();
            // 错误输出流
            InputStream errorInput = p.getErrorStream();// 创建并实例化输入字节流
            BufferedReader errorReader = new BufferedReader(new InputStreamReader(errorInput));// 先通过inputstreamreader进行流转化,在实例化bufferedreader,接收内容
            String eline = "";
            while ((eline = errorReader.readLine()) != null) {// 循环读取
                System.out.println(eline);// 输出
                Common.getInstance().saveToFile(eline, "runlog.log");// 保存,false表示不覆盖
            }
            errorReader.close();// 此处有依赖关系,先关闭errorReader
            errorInput.close();
        } catch (IOException e) {
            output("执行" + cmd + "失败!");
            e.printStackTrace();
        }
    }

    public void output(String text) {
        System.out.println(text);
    }

    public void output(Object... object) {
        if (object.length == 1) {
            output(object[0].toString());
            return;
        }
        for (int i = 0; i < object.length; i++) {
            System.out.println("第" + (i + 1) + "个:" + object[i]);
        }
    }
}

本文分享自微信公众号 - FunTester(NuclearTester)

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2019-08-23

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券