1 /* 2 * Copyright (C) 2019 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 17 package android.wm; 18 19 import static android.perftests.utils.ManualBenchmarkState.StatsReport; 20 21 import android.os.ParcelFileDescriptor; 22 import android.os.SystemClock; 23 import android.perftests.utils.ManualBenchmarkState; 24 import android.perftests.utils.ManualBenchmarkState.ManualBenchmarkTest; 25 import android.perftests.utils.PerfManualStatusReporter; 26 import android.perftests.utils.TraceMarkParser; 27 import android.perftests.utils.TraceMarkParser.TraceMarkSlice; 28 import android.util.Log; 29 30 import androidx.test.filters.LargeTest; 31 import androidx.test.runner.lifecycle.Stage; 32 33 import org.junit.Rule; 34 import org.junit.Test; 35 36 import java.io.BufferedReader; 37 import java.io.IOException; 38 import java.io.InputStream; 39 import java.io.InputStreamReader; 40 import java.util.concurrent.TimeUnit; 41 42 /** Measure the performance of internal methods in window manager service by trace tag. */ 43 @LargeTest 44 public class InternalWindowOperationPerfTest extends WindowManagerPerfTestBase { 45 private static final String TAG = InternalWindowOperationPerfTest.class.getSimpleName(); 46 47 @Rule 48 public final PerfManualStatusReporter mPerfStatusReporter = new PerfManualStatusReporter(); 49 50 @Rule 51 public final PerfTestActivityRule mActivityRule = new PerfTestActivityRule(); 52 53 private final TraceMarkParser mTraceMarkParser = new TraceMarkParser( 54 "applyPostLayoutPolicy", 55 "applySurfaceChanges", 56 "AppTransitionReady", 57 "closeSurfaceTransactiom", 58 "openSurfaceTransaction", 59 "performLayout", 60 "performSurfacePlacement", 61 "prepareSurfaces", 62 "updateInputWindows", 63 "WSA#startAnimation", 64 "activityIdle", 65 "activityPaused", 66 "activityStopped", 67 "activityDestroyed", 68 "finishActivity", 69 "startActivityInner"); 70 71 @Test 72 @ManualBenchmarkTest( 73 targetTestDurationNs = 20 * TIME_1_S_IN_NS, 74 statsReport = @StatsReport( 75 flags = StatsReport.FLAG_ITERATION | StatsReport.FLAG_MEAN 76 | StatsReport.FLAG_MAX | StatsReport.FLAG_COEFFICIENT_VAR)) testLaunchAndFinishActivity()77 public void testLaunchAndFinishActivity() throws Throwable { 78 final ManualBenchmarkState state = mPerfStatusReporter.getBenchmarkState(); 79 long measuredTimeNs = 0; 80 boolean isTraceStarted = false; 81 82 while (state.keepRunning(measuredTimeNs)) { 83 if (!isTraceStarted && !state.isWarmingUp()) { 84 startAsyncAtrace(); 85 isTraceStarted = true; 86 } 87 final long startTime = SystemClock.elapsedRealtimeNanos(); 88 mActivityRule.launchActivity(); 89 mActivityRule.finishActivity(); 90 mActivityRule.waitForIdleSync(Stage.DESTROYED); 91 measuredTimeNs = SystemClock.elapsedRealtimeNanos() - startTime; 92 } 93 94 stopAsyncAtrace(); 95 96 mTraceMarkParser.forAllSlices((key, slices) -> { 97 for (TraceMarkSlice slice : slices) { 98 state.addExtraResult(key, (long) (slice.getDurationInSeconds() * NANOS_PER_S)); 99 } 100 }); 101 102 Log.i(TAG, String.valueOf(mTraceMarkParser)); 103 } 104 startAsyncAtrace()105 private void startAsyncAtrace() throws IOException { 106 sUiAutomation.executeShellCommand("atrace -b 32768 --async_start wm"); 107 // Avoid atrace isn't ready immediately. 108 SystemClock.sleep(TimeUnit.NANOSECONDS.toMillis(TIME_1_S_IN_NS)); 109 } 110 stopAsyncAtrace()111 private void stopAsyncAtrace() throws IOException { 112 final ParcelFileDescriptor pfd = sUiAutomation.executeShellCommand("atrace --async_stop"); 113 final InputStream inputStream = new ParcelFileDescriptor.AutoCloseInputStream(pfd); 114 try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { 115 String line; 116 while ((line = reader.readLine()) != null) { 117 mTraceMarkParser.visit(line); 118 } 119 } 120 } 121 } 122