blob: 4ed3b4e09d11e095936707b6b7b61e80b98857bb [file] [log] [blame]
Riddle Hsuf5622d22019-09-24 17:56:05 -06001/*
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
17package android.wm;
18
19import static android.perftests.utils.ManualBenchmarkState.StatsReport;
20
21import android.os.ParcelFileDescriptor;
22import android.os.SystemClock;
23import android.perftests.utils.ManualBenchmarkState;
24import android.perftests.utils.ManualBenchmarkState.ManualBenchmarkTest;
25import android.perftests.utils.PerfManualStatusReporter;
26import android.perftests.utils.TraceMarkParser;
27import android.perftests.utils.TraceMarkParser.TraceMarkSlice;
28import android.util.Log;
29
30import androidx.test.filters.LargeTest;
31import androidx.test.runner.lifecycle.Stage;
32
33import org.junit.Rule;
34import org.junit.Test;
35
36import java.io.BufferedReader;
37import java.io.IOException;
38import java.io.InputStream;
39import java.io.InputStreamReader;
40import java.util.concurrent.TimeUnit;
41
42/** Measure the performance of internal methods in window manager service by trace tag. */
43@LargeTest
44public 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))
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) {
Jing Ji84121312019-10-17 15:36:57 -070098 state.addExtraResult(key, (long) (slice.getDurationInSeconds() * NANOS_PER_S));
Riddle Hsuf5622d22019-09-24 17:56:05 -060099 }
100 });
101
102 Log.i(TAG, String.valueOf(mTraceMarkParser));
103 }
104
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
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}