1

我想在进行自动化测试时在我的 Android 手机上捕获 Systrace 报告。不知道测试需要多长时间,所以我无法为 Systrace 指定--time时间段。

深入研究systrace.py,我发现systrace正在使用atrace来获取内核日志。

我使用adb shell atrace --help并得到以下输出:

usage: atrace [options] [categories...]
options include:
  -a appname      enable app-level tracing for a comma separated list of cmdlines
  -b N            use a trace buffer size of N KB
  -c              trace into a circular buffer
  -k fname,...    trace the listed kernel functions
  -n              ignore signals
  -s N            sleep for N seconds before tracing [default 0]
  -t N            trace for N seconds [defualt 5]
  -z              compress the trace dump
  --async_start   start circular trace and return immediatly
  --async_dump    dump the current contents of circular trace buffer
  --async_stop    stop tracing and dump the current contents of circular
                    trace buffer
  --list_categories
                  list the available tracing categories

如何使用atrace在我的自动化测试开始时开始跟踪,并在我的自动化测试结束时停止跟踪和转储内核日志?

我尝试使用以下命令,但我认为它不能正常工作。只有 async_dump 在日志中给了我一些数据。async_stop 转储在日志中没有任何内容。如何正确启动跟踪,转储它,然后停止它?

adb shell atrace -b 10000 -c am shedgfx view --async_start > C:\Users\user1\Desktop\log.txt 

adb shell atrace -b 10000 -c am shedgfx view --async_dump  > C:\Users\user1\Desktop\log.txt 

adb shell atrace -b 10000 -c am shedgfx view --async_stop > C:\Users\user1\Desktop\log.txt 
4

1 回答 1

0
  1. schedgfx应该可能是 2 个单独的调用: schedgfx. 运行时查看设备的输出:

    $ adb shell atrace --list_categories

  2. 类别应该是命令行的最后一部分(在您的命令中,类别放在async_*选项之前):

    $ adb shell atrace --help usage: atrace [options] [categories...] options include: -a appname enable app-level tracing for a comma separated list of cmdlines -b N use a trace buffer size of N KB -c trace into a circular buffer -k fname,... trace the listed kernel functions -n ignore signals -s N sleep for N seconds before tracing [default 0] -t N trace for N seconds [defualt 5] -z compress the trace dump --async_start start circular trace and return immediatly --async_dump dump the current contents of circular trace buffer --async_stop stop tracing and dump the current contents of circular trace buffer --list_categories list the available tracing categories

  3. 该工具在循环缓冲区上运行,因此每次运行转储命令时,您只会获得当时缓冲区中的任何内容。在 Espressorules:0.5包中(假设您使用的是 Espresso),有一个AtraceLogger类可以帮助您将其作为测试工具的一部分自动执行,方法是调用以下atraceStart(...)方法:

    public void atraceStart(Set<String> traceCategoriesSet, int atraceBufferSize, int dumpIntervalSecs, File destDirectory, String traceFileName) throws IOException

    您可以通过创建@Rule 或@ClassRule 来做到这一点,或者通过其他方式(例如使用TestRunner)将其挂钩:

    @Override
    protected void before() throws Throwable {
        mAtrace = AtraceLogger.getAtraceLoggerInstance(InstrumentationRegistry.getInstrumentation());
        mAtrace.atraceStart(new HashSet<>(Arrays.asList("gfx", "sched", ...)),
                1024 /* bufferSize */, 1 /* dump interval */,
                RuleLoggingUtils.getTestRunDir(), "filename");
    }
    
    @Override
    protected void after() {
        try {
            mAtrace.atraceStop();
        } catch (IOException e) {
            Log.w(TAG, "Failed to stop Atrace", e);
        } catch (InterruptedException e) {
            Log.w(TAG, "Failed to stop Atrace", e);
        }
    }
    

    RuleLoggingUtils.getTestRunDir()方法会将捕获的转储文件放入应用程序的外部文件路径中,因此您可以在测试完成后提取这些文件,使用:

    $ adb pull /sdcard/Android/data/com.yourcompany.package/files/testdata/

然后可以使用 systrace 查看器通过使用该--from-file=<trace file>选项运行 systrace 来检查每个 atrace 文件。

于 2016-05-12T15:53:28.763 回答