有关Monkey命令的两个隐藏选项

一般Android App的测试过程,常用的Monkey选项如同官网中所列,但如果看Monkey源代码或则通过 monkey --help,还是能找到一些有意思的东西。
其中,有2个网页中没有提到的选项。

  1. --bugreport
  2. --hprof

bugreport 其实对应的是一个叫做bugreport的工具,可以运行adb bugreport观察,它提供了那个时刻系统非常多的信息,从meminfo,cpuinfo,vmstat到dmesg,buildprop等信息,多到想不到。有兴趣的,可以google之。
当这个标签在Monkey的运行中标识的时候,每当Monkey运行时发现AppCrash,anr,system not responding的时候,Monkey就会自动记录错误时的信息。

    // Write the bugreport to the sdcard.
    private void getBugreport(String reportName) {
        reportName += MonkeyUtils.toCalendarTime(System.currentTimeMillis());
        String bugreportName = reportName.replaceAll("[ ,:]", "_");
        commandLineReport(bugreportName + ".txt", "bugreport");
    }

可以在 /mnt/sdcard里面找到类似于app_crashnet.crimoon.pm.a91_2014-07-29_01_01_22.990_.txt 这样的文件。每个问价有15MB到20MB大小,内容翔实,乃是开发debug之宝。

hprof的作用就是,在monkey开始之前和结束之后(正常结束,被kill的不算),会调用如下代码

    /**
     * Send SIGNAL_USR1 to all processes. This will generate large (5mb)
     * profiling reports in data/misc, so use with care.
     */
    private void signalPersistentProcesses() {
        try {
            mAm.signalPersistentProcesses(Process.SIGNAL_USR1);

            synchronized (this) {
                wait(2000);
            }
        } catch (RemoteException e) {
            System.err.println("** Failed talking with activity manager!");
        } catch (InterruptedException e) {
        }
    }  

这个代码的作用,就是对于所有persistent的process,获取它的hprof信息,类似于对于所有的persistent应用 kill -10 pid。它的好处也不言而喻,hprof文件出来了,找内存泄漏还远么。
有一点要吐槽的是,注释里面明明说 /data/misc里面记录该文件,实际上,还是保存在了 /mnt/sdcard 里面。文件名类似于 com.aliyun.ams.assistantservice_hprof_dump20140728203235.prof。一般每个文件大约5MB,也看到过 com.android.systemui,比较夸张,有25MB。
可见,这个开关对于测试整个系统的内存泄漏,还是会有比较大的帮助的。

posted on 2016-03-18 19:04  Ruskee  阅读(830)  评论(0编辑  收藏  举报