diff options
author | Gopinath Elanchezhian <gelanchezhian@google.com> | 2020-03-17 18:54:05 -0700 |
---|---|---|
committer | Gopinath Elanchezhian <gelanchezhian@google.com> | 2020-03-18 02:01:37 +0000 |
commit | b0387e224ef1467d38a4a6ccc9764df9b049ebb3 (patch) | |
tree | 1b3a3e269eef948a205fe2115e9bafa371e2ad01 | |
parent | 0c2761259069054c6c026b3b16bcdacdbe8505b9 (diff) | |
download | platform_testing-b0387e224ef1467d38a4a6ccc9764df9b049ebb3.tar.gz |
DO NOT MERGE: Backport perfetto listener changes.
Bug: b/151774445
Test: PerfettoHelperTest, PerfettoListenerTest
Change-Id: I0142edd0b369a59c39751d3af90c3e1e1eb9c3e8
Merged-In: I969a3b36dce9e2c7fb54fd486509bd765f8fe23c
4 files changed, 93 insertions, 30 deletions
diff --git a/libraries/collectors-helper/perfetto/src/com/android/helpers/PerfettoHelper.java b/libraries/collectors-helper/perfetto/src/com/android/helpers/PerfettoHelper.java index c1d7aeef8..abf92e25c 100644 --- a/libraries/collectors-helper/perfetto/src/com/android/helpers/PerfettoHelper.java +++ b/libraries/collectors-helper/perfetto/src/com/android/helpers/PerfettoHelper.java @@ -40,6 +40,8 @@ public class PerfettoHelper { private static final String PERFETTO_START_CMD = "perfetto --background -c %s%s -o %s"; private static final String PERFETTO_TMP_OUTPUT_FILE = "/data/misc/perfetto-traces/trace_output.pb"; + // Additional arg to indicate that the perfetto config file is text format. + private static final String PERFETTO_TXT_PROTO_ARG = " --txt"; // Command to stop (i.e kill) the perfetto tracing. private static final String PERFETTO_STOP_CMD = "pkill -INT perfetto"; // Command to check the perfetto process id. @@ -62,9 +64,10 @@ public class PerfettoHelper { * /data/misc/perfetto-traces/ folder in the device. * * @param configFileName used for collecting the perfetto trace. + * @param isTextProtoConfig true if the config file is textproto format otherwise false. * @return true if trace collection started successfully otherwise return false. */ - public boolean startCollecting(String configFileName) { + public boolean startCollecting(String configFileName, boolean isTextProtoConfig) { mUIDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); if (configFileName == null || configFileName.isEmpty()) { Log.e(LOG_TAG, "Perfetto config file name is null or empty."); @@ -79,15 +82,22 @@ public class PerfettoHelper { return false; } } + // Remove already existing temporary output trace file if any. String output = mUIDevice.executeShellCommand(String.format(REMOVE_CMD, PERFETTO_TMP_OUTPUT_FILE)); Log.i(LOG_TAG, String.format("Perfetto output file cleanup - %s", output)); + String perfettoCmd = String.format(PERFETTO_START_CMD, + PERFETTO_ROOT_DIR, configFileName, PERFETTO_TMP_OUTPUT_FILE); + + if(isTextProtoConfig) { + perfettoCmd = perfettoCmd + PERFETTO_TXT_PROTO_ARG; + } + // Start perfetto tracing. Log.i(LOG_TAG, "Starting perfetto tracing."); - String startOutput = mUIDevice.executeShellCommand(String.format(PERFETTO_START_CMD, - PERFETTO_ROOT_DIR, configFileName, PERFETTO_TMP_OUTPUT_FILE)); + String startOutput = mUIDevice.executeShellCommand(perfettoCmd); Log.i(LOG_TAG, String.format("Perfetto start command output - %s", startOutput)); // TODO : Once the output status is available use that for additional validation. if (!isPerfettoRunning()) { diff --git a/libraries/collectors-helper/perfetto/test/src/com/android/helpers/tests/PerfettoHelperTest.java b/libraries/collectors-helper/perfetto/test/src/com/android/helpers/tests/PerfettoHelperTest.java index 66b4f3bfe..a81499d01 100644 --- a/libraries/collectors-helper/perfetto/test/src/com/android/helpers/tests/PerfettoHelperTest.java +++ b/libraries/collectors-helper/perfetto/test/src/com/android/helpers/tests/PerfettoHelperTest.java @@ -37,6 +37,7 @@ import static org.junit.Assert.assertTrue; * * To run: * Have a valid perfetto config under /data/misc/perfetto-traces/valid_config.pb + * Have a valid text perfetto config under /data/misc/perfetto-traces/valid_text_config.textproto * TODO: b/119020380 to keep track of automating the above step. * atest CollectorsHelperTest:com.android.helpers.tests.PerfettoHelperTest */ @@ -64,7 +65,7 @@ public class PerfettoHelperTest { */ @Test public void testNullConfigName() throws Exception { - assertFalse(perfettoHelper.startCollecting(null)); + assertFalse(perfettoHelper.startCollecting(null, false)); } /** @@ -72,7 +73,7 @@ public class PerfettoHelperTest { */ @Test public void testEmptyConfigName() throws Exception { - assertFalse(perfettoHelper.startCollecting("")); + assertFalse(perfettoHelper.startCollecting("", false)); } /** @@ -80,7 +81,7 @@ public class PerfettoHelperTest { */ @Test public void testNoConfigFile() throws Exception { - assertFalse(perfettoHelper.startCollecting("no_config.pb")); + assertFalse(perfettoHelper.startCollecting("no_config.pb", false)); } /** @@ -88,7 +89,7 @@ public class PerfettoHelperTest { */ @Test public void testPerfettoStartSuccess() throws Exception { - assertTrue(perfettoHelper.startCollecting("valid_config.pb")); + assertTrue(perfettoHelper.startCollecting("valid_config.pb", false)); } /** @@ -96,7 +97,7 @@ public class PerfettoHelperTest { */ @Test public void testPerfettoValidOutputPath() throws Exception { - assertTrue(perfettoHelper.startCollecting("valid_config.pb")); + assertTrue(perfettoHelper.startCollecting("valid_config.pb", false)); assertTrue(perfettoHelper.stopCollecting(1000, "data/local/tmp/out.pb")); } @@ -105,7 +106,7 @@ public class PerfettoHelperTest { */ @Test public void testPerfettoInvalidOutputPath() throws Exception { - assertTrue(perfettoHelper.startCollecting("valid_config.pb")); + assertTrue(perfettoHelper.startCollecting("valid_config.pb", false)); // Don't have permission to create new folder under /data assertFalse(perfettoHelper.stopCollecting(1000, "/data/dummy/xyz/out.pb")); } @@ -116,7 +117,7 @@ public class PerfettoHelperTest { */ @Test public void testPerfettoSuccess() throws Exception { - assertTrue(perfettoHelper.startCollecting("valid_config.pb")); + assertTrue(perfettoHelper.startCollecting("valid_config.pb", false)); assertTrue(perfettoHelper.stopCollecting(1000, "/data/local/tmp/out.pb")); UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); String[] fileStats = uiDevice.executeShellCommand(String.format( @@ -125,4 +126,18 @@ public class PerfettoHelperTest { assertTrue(fileSize > 0); } + /** + * Test perfetto collection returns true and output file size greater than zero + * if the valid perfetto config file used. + */ + @Test + public void testTextProtoConfigSuccess() throws Exception { + assertTrue(perfettoHelper.startCollecting("valid_text_config.textproto", true)); + assertTrue(perfettoHelper.stopCollecting(1000, "/data/local/tmp/out.pb")); + UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); + String[] fileStats = uiDevice.executeShellCommand(String.format( + FILE_SIZE_IN_BYTES, "/data/local/tmp/out.pb")).split(" "); + int fileSize = Integer.parseInt(fileStats[0].trim()); + assertTrue(fileSize > 0); + } } diff --git a/libraries/device-collectors/src/main/java/android/device/collectors/PerfettoListener.java b/libraries/device-collectors/src/main/java/android/device/collectors/PerfettoListener.java index 8c0891e5f..9ba0e8b9d 100644 --- a/libraries/device-collectors/src/main/java/android/device/collectors/PerfettoListener.java +++ b/libraries/device-collectors/src/main/java/android/device/collectors/PerfettoListener.java @@ -46,6 +46,8 @@ public class PerfettoListener extends BaseMetricListener { private static final String DEFAULT_WAIT_TIME_MSECS = "3000"; // Default output folder to store the perfetto output traces. private static final String DEFAULT_OUTPUT_ROOT = "/sdcard/test_results"; + // Argument to indicate the perfetto config file is text proto file. + public static final String PERFETTO_CONFIG_TEXT_PROTO = "perfetto_config_text_proto"; // Argument to get custom config file name for collecting the trace. private static final String PERFETTO_CONFIG_FILE_ARG = "perfetto_config_file"; // Argument to get custom time in millisecs to wait before dumping the trace. @@ -75,6 +77,7 @@ public class PerfettoListener extends BaseMetricListener { // Store the method name and invocation count to create unique file name for each trace. private Map<String, Integer> mTestIdInvocationCount = new HashMap<>(); private boolean mPerfettoStartSuccess = false; + private boolean mIsConfigTextProto = false; private boolean mIsCollectPerRun; private PerfettoHelper mPerfettoHelper = new PerfettoHelper(); @@ -123,10 +126,14 @@ public class PerfettoListener extends BaseMetricListener { // Whether to collect the for the entire test run or per test. mIsCollectPerRun = Boolean.parseBoolean(args.getString(COLLECT_PER_RUN)); + // Whether the config is text proto or not. By default set to false. + mIsConfigTextProto = Boolean.parseBoolean(args.getString(PERFETTO_CONFIG_TEXT_PROTO)); + // Perfetto config file has to be under /data/misc/perfetto-traces/ // defaulted to trace_config.pb is perfetto_config_file is not passed. mConfigFileName = args.getString(PERFETTO_CONFIG_FILE_ARG, DEFAULT_CONFIG_FILE); + // Whether to hold wakelocks on all Prefetto tracing functions. You may want to enable // this if your device is not USB connected. This option prevents the device from // going into suspend mode while this listener is running intensive tasks. @@ -288,16 +295,22 @@ public class PerfettoListener extends BaseMetricListener { @VisibleForTesting public void acquireWakelock(WakeLock wakelock) { if (wakelock != null) { + Log.d(getTag(), "wakelock.isHeld: " + wakelock.isHeld()); Log.d(getTag(), "acquiring wakelock."); wakelock.acquire(); + Log.d(getTag(), "wakelock acquired."); + Log.d(getTag(), "wakelock.isHeld: " + wakelock.isHeld()); } } @VisibleForTesting public void releaseWakelock(WakeLock wakelock) { if (wakelock != null) { + Log.d(getTag(), "wakelock.isHeld: " + wakelock.isHeld()); Log.d(getTag(), "releasing wakelock."); wakelock.release(); + Log.d(getTag(), "wakelock released."); + Log.d(getTag(), "wakelock.isHeld: " + wakelock.isHeld()); } } @@ -313,7 +326,8 @@ public class PerfettoListener extends BaseMetricListener { * Start perfetto tracing using the given config file. */ private void startPerfettoTracing() { - mPerfettoStartSuccess = mPerfettoHelper.startCollecting(mConfigFileName); + mPerfettoStartSuccess = mPerfettoHelper.startCollecting(mConfigFileName, + mIsConfigTextProto); if (!mPerfettoStartSuccess) { Log.e(getTag(), "Perfetto did not start successfully."); } diff --git a/libraries/device-collectors/src/test/java/android/device/collectors/PerfettoListenerTest.java b/libraries/device-collectors/src/test/java/android/device/collectors/PerfettoListenerTest.java index c51ded8cf..20ae4d63c 100644 --- a/libraries/device-collectors/src/test/java/android/device/collectors/PerfettoListenerTest.java +++ b/libraries/device-collectors/src/test/java/android/device/collectors/PerfettoListenerTest.java @@ -18,6 +18,7 @@ package android.device.collectors; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.doAnswer; @@ -113,14 +114,14 @@ public class PerfettoListenerTest { public void testPerfettoPerTestSuccessFlow() throws Exception { Bundle b = new Bundle(); mListener = initListener(b); - doReturn(true).when(mPerfettoHelper).startCollecting(anyString()); + doReturn(true).when(mPerfettoHelper).startCollecting(anyString(), anyBoolean()); doReturn(true).when(mPerfettoHelper).stopCollecting(anyLong(), anyString()); // Test run start behavior mListener.testRunStarted(mRunDesc); // Test test start behavior mListener.testStarted(mTest1Desc); - verify(mPerfettoHelper, times(1)).startCollecting(anyString()); + verify(mPerfettoHelper, times(1)).startCollecting(anyString(), anyBoolean()); mListener.onTestEnd(mDataRecord, mTest1Desc); verify(mPerfettoHelper, times(1)).stopCollecting(anyLong(), anyString()); @@ -135,14 +136,14 @@ public class PerfettoListenerTest { Bundle b = new Bundle(); b.putString(PerfettoListener.COLLECT_PER_RUN, "true"); mListener = initListener(b); - doReturn(true).when(mPerfettoHelper).startCollecting(anyString()); + doReturn(true).when(mPerfettoHelper).startCollecting(anyString(), anyBoolean()); doReturn(true).when(mPerfettoHelper).stopCollecting(anyLong(), anyString()); // Test run start behavior mListener.onTestRunStart(mListener.createDataRecord(), FAKE_DESCRIPTION); - verify(mPerfettoHelper, times(1)).startCollecting(anyString()); + verify(mPerfettoHelper, times(1)).startCollecting(anyString(), anyBoolean()); mListener.testStarted(mTest1Desc); - verify(mPerfettoHelper, times(1)).startCollecting(anyString()); + verify(mPerfettoHelper, times(1)).startCollecting(anyString(), anyBoolean()); mListener.onTestEnd(mDataRecord, mTest1Desc); verify(mPerfettoHelper, times(0)).stopCollecting(anyLong(), anyString()); mListener.onTestRunEnd(mListener.createDataRecord(), new Result()); @@ -185,7 +186,7 @@ public class PerfettoListenerTest { b.putString(PerfettoListener.COLLECT_PER_RUN, "true"); mListener = initListener(b); - doReturn(true).when(mPerfettoHelper).startCollecting(anyString()); + doReturn(true).when(mPerfettoHelper).startCollecting(anyString(), anyBoolean()); doReturn(true).when(mPerfettoHelper).stopCollecting(anyLong(), anyString()); // Test run start behavior @@ -204,7 +205,7 @@ public class PerfettoListenerTest { Bundle b = new Bundle(); mListener = initListener(b); - doReturn(true).when(mPerfettoHelper).startCollecting(anyString()); + doReturn(true).when(mPerfettoHelper).startCollecting(anyString(), anyBoolean()); doReturn(true).when(mPerfettoHelper).stopCollecting(anyLong(), anyString()); // Test run start behavior @@ -225,7 +226,7 @@ public class PerfettoListenerTest { b.putString(PerfettoListener.COLLECT_PER_RUN, "true"); mListener = initListener(b); - doReturn(true).when(mPerfettoHelper).startCollecting(anyString()); + doReturn(true).when(mPerfettoHelper).startCollecting(anyString(), anyBoolean()); doReturn(true).when(mPerfettoHelper).stopCollecting(anyLong(), anyString()); // Test run start behavior @@ -243,7 +244,7 @@ public class PerfettoListenerTest { b.putString(PerfettoListener.HOLD_WAKELOCK_WHILE_COLLECTING, "true"); mListener = initListener(b); - doReturn(true).when(mPerfettoHelper).startCollecting(anyString()); + doReturn(true).when(mPerfettoHelper).startCollecting(anyString(), anyBoolean()); doReturn(true).when(mPerfettoHelper).stopCollecting(anyLong(), anyString()); // Test run start behavior @@ -264,7 +265,7 @@ public class PerfettoListenerTest { b.putString(PerfettoListener.HOLD_WAKELOCK_WHILE_COLLECTING, "true"); mListener = initListener(b); - doReturn(true).when(mPerfettoHelper).startCollecting(anyString()); + doReturn(true).when(mPerfettoHelper).startCollecting(anyString(), anyBoolean()); doReturn(true).when(mPerfettoHelper).stopCollecting(anyLong(), anyString()); // Test run start behavior @@ -290,7 +291,7 @@ public class PerfettoListenerTest { b.putString(PerfettoListener.COLLECT_PER_RUN, "true"); mListener = initListener(b); - doReturn(true).when(mPerfettoHelper).startCollecting(anyString()); + doReturn(true).when(mPerfettoHelper).startCollecting(anyString(), anyBoolean()); doReturn(true).when(mPerfettoHelper).stopCollecting(anyLong(), anyString()); // Test run start behavior @@ -315,11 +316,11 @@ public class PerfettoListenerTest { Bundle b = new Bundle(); b.putString(PerfettoListener.COLLECT_PER_RUN, "true"); mListener = initListener(b); - doReturn(false).when(mPerfettoHelper).startCollecting(anyString()); + doReturn(false).when(mPerfettoHelper).startCollecting(anyString(), anyBoolean()); // Test run start behavior mListener.onTestRunStart(mListener.createDataRecord(), FAKE_DESCRIPTION); - verify(mPerfettoHelper, times(1)).startCollecting(anyString()); + verify(mPerfettoHelper, times(1)).startCollecting(anyString(), anyBoolean()); mListener.onTestRunEnd(mListener.createDataRecord(), new Result()); verify(mPerfettoHelper, times(0)).stopCollecting(anyLong(), anyString()); } @@ -331,14 +332,14 @@ public class PerfettoListenerTest { public void testPerfettoStartFailureFlow() throws Exception { Bundle b = new Bundle(); mListener = initListener(b); - doReturn(false).when(mPerfettoHelper).startCollecting(anyString()); + doReturn(false).when(mPerfettoHelper).startCollecting(anyString(), anyBoolean()); // Test run start behavior mListener.testRunStarted(mRunDesc); // Test test start behavior mListener.testStarted(mTest1Desc); - verify(mPerfettoHelper, times(1)).startCollecting(anyString()); + verify(mPerfettoHelper, times(1)).startCollecting(anyString(), anyBoolean()); mListener.onTestEnd(mDataRecord, mTest1Desc); verify(mPerfettoHelper, times(0)).stopCollecting(anyLong(), anyString()); } @@ -351,7 +352,7 @@ public class PerfettoListenerTest { public void testPerfettoInvocationCount() throws Exception { Bundle b = new Bundle(); mListener = initListener(b); - doReturn(true).when(mPerfettoHelper).startCollecting(anyString()); + doReturn(true).when(mPerfettoHelper).startCollecting(anyString(), anyBoolean()); doReturn(true).when(mPerfettoHelper).stopCollecting(anyLong(), anyString()); // Test run start behavior @@ -359,19 +360,19 @@ public class PerfettoListenerTest { // Test1 invocation 1 start behavior mListener.testStarted(mTest1Desc); - verify(mPerfettoHelper, times(1)).startCollecting(anyString()); + verify(mPerfettoHelper, times(1)).startCollecting(anyString(), anyBoolean()); mListener.onTestEnd(mDataRecord, mTest1Desc); verify(mPerfettoHelper, times(1)).stopCollecting(anyLong(), anyString()); // Test1 invocation 2 start behaviour mListener.testStarted(mTest1Desc); - verify(mPerfettoHelper, times(2)).startCollecting(anyString()); + verify(mPerfettoHelper, times(2)).startCollecting(anyString(), anyBoolean()); mListener.onTestEnd(mDataRecord, mTest1Desc); verify(mPerfettoHelper, times(2)).stopCollecting(anyLong(), anyString()); // Test2 invocation 1 start behaviour mListener.testStarted(mTest2Desc); - verify(mPerfettoHelper, times(3)).startCollecting(anyString()); + verify(mPerfettoHelper, times(3)).startCollecting(anyString(), anyBoolean()); mDataRecord = mListener.createDataRecord(); mListener.onTestEnd(mDataRecord, mTest2Desc); verify(mPerfettoHelper, times(3)).stopCollecting(anyLong(), anyString()); @@ -381,4 +382,27 @@ public class PerfettoListenerTest { assertEquals(1, (int) mInvocationCount.get(mListener.getTestFileName(mTest2Desc))); } + + /* + * Verify perfetto start and stop collection methods called when the text + * proto config option is enabled + */ + @Test + public void testPerfettoSuccessFlowWithTextConfig() throws Exception { + Bundle b = new Bundle(); + b.putString(PerfettoListener.PERFETTO_CONFIG_TEXT_PROTO, "true"); + mListener = initListener(b); + doReturn(true).when(mPerfettoHelper).startCollecting(anyString(), anyBoolean()); + doReturn(true).when(mPerfettoHelper).stopCollecting(anyLong(), anyString()); + // Test run start behavior + mListener.testRunStarted(mRunDesc); + + // Test test start behavior + mListener.testStarted(mTest1Desc); + verify(mPerfettoHelper, times(1)).startCollecting(anyString(), anyBoolean()); + mListener.onTestEnd(mDataRecord, mTest1Desc); + verify(mPerfettoHelper, times(1)).stopCollecting(anyLong(), anyString()); + + } + } |