1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package android.device.collectors; 17 18 import android.device.collectors.annotations.OptionClass; 19 import android.os.Bundle; 20 21 import androidx.annotation.VisibleForTesting; 22 23 import com.android.helpers.PerfettoHelper; 24 25 import org.junit.runner.Description; 26 import org.junit.runner.Result; 27 import org.junit.runner.notification.Failure; 28 29 /** 30 * A {@link PerfettoListener} that captures the perfetto trace during each test method and save the 31 * perfetto trace files under 32 * <root>/<test_name>/PerfettoTracingStrategy/<test_name>-<invocation_count>.perfetto-trace 33 */ 34 @OptionClass(alias = "perfetto-collector") 35 public class PerfettoListener extends BaseMetricListener { 36 public static final String COLLECT_PER_RUN = "per_run"; 37 public static final String COLLECT_PER_CLASS = "per_class"; 38 39 private PerfettoTracingStrategy mTracingStrategy; 40 PerfettoListener()41 public PerfettoListener() { 42 super(); 43 } 44 45 /** 46 * Constructor to simulate receiving the instrumentation arguments. Should not be used except 47 * for testing. 48 */ 49 @VisibleForTesting PerfettoListener(Bundle args, PerfettoTracingStrategy strategy)50 public PerfettoListener(Bundle args, PerfettoTracingStrategy strategy) { 51 super(args); 52 mTracingStrategy = strategy; 53 } 54 getPerfettoHelper()55 protected PerfettoHelper getPerfettoHelper() { 56 return mTracingStrategy.getPerfettoHelper(); 57 } 58 59 @Override onTestRunStart(DataRecord runData, Description description)60 public void onTestRunStart(DataRecord runData, Description description) { 61 mTracingStrategy.testRunStart(runData, description); 62 } 63 64 @Override onTestStart(DataRecord testData, Description description)65 public void onTestStart(DataRecord testData, Description description) { 66 mTracingStrategy.testStart(testData, description); 67 } 68 69 @Override onTestFail(DataRecord testData, Description description, Failure failure)70 public void onTestFail(DataRecord testData, Description description, Failure failure) { 71 mTracingStrategy.testFail(testData, description, failure); 72 } 73 74 @Override onTestEnd(DataRecord testData, Description description)75 public void onTestEnd(DataRecord testData, Description description) { 76 mTracingStrategy.testEnd(testData, description); 77 } 78 79 @Override onTestRunEnd(DataRecord runData, Result result)80 public void onTestRunEnd(DataRecord runData, Result result) { 81 mTracingStrategy.testRunEnd(runData, result); 82 } 83 84 @Override setupAdditionalArgs()85 public void setupAdditionalArgs() { 86 Bundle args = getArgsBundle(); 87 88 if (mTracingStrategy == null) { 89 initTracingStrategy(args); 90 } 91 92 mTracingStrategy.setup(args); 93 } 94 initTracingStrategy(Bundle args)95 private void initTracingStrategy(Bundle args) { 96 // Whether to collect the for the entire test run, per test, or per class. 97 if (Boolean.parseBoolean(args.getString(COLLECT_PER_RUN))) { 98 mTracingStrategy = new PerfettoTracingPerRunStrategy(getInstrumentation()); 99 } else if (Boolean.parseBoolean(args.getString(COLLECT_PER_CLASS))) { 100 mTracingStrategy = new PerfettoTracingPerClassStrategy(getInstrumentation()); 101 } else { 102 mTracingStrategy = new PerfettoTracingPerTestStrategy(getInstrumentation()); 103 } 104 } 105 106 @VisibleForTesting getTestFileName(Description description)107 String getTestFileName(Description description) { 108 return PerfettoTracingStrategy.getTestFileName(description); 109 } 110 111 @VisibleForTesting runWithWakeLock(Runnable runnable)112 void runWithWakeLock(Runnable runnable) { 113 mTracingStrategy.runWithWakeLock(runnable); 114 } 115 } 116