Debian Codegraph @

apct-tests/perftests/core/src/android/os/ParcelObtainPerfTest.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17 package android.os;
18
19 import androidx.test.InstrumentationRegistry;
20 import androidx.test.filters.LargeTest;
21 import androidx.test.runner.AndroidJUnit4;
22
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25
26 @RunWith(AndroidJUnit4.class)
27 @LargeTest
28 public class ParcelObtainPerfTest {
29 private static final int ITERATIONS = 1_000_000;
30
31 @Test
32 public void timeContention_01() throws Exception {
33 timeContention(1);
34 }
35
36 @Test
37 public void timeContention_04() throws Exception {
38 timeContention(4);
39 }
40
41 @Test
42 public void timeContention_16() throws Exception {
43 timeContention(16);
44 }
45
46 private static void timeContention(int numThreads) throws Exception {
47 final long start = SystemClock.elapsedRealtime();
48 {
49 final ObtainThread[] threads = new ObtainThread[numThreads];
50 for (int i = 0; i < numThreads; i++) {
51 final ObtainThread thread = new ObtainThread(ITERATIONS / numThreads);
52 thread.start();
53 threads[i] = thread;
54 }
55 for (int i = 0; i < numThreads; i++) {
56 threads[i].join();
57 }
58 }
59 final long duration = SystemClock.elapsedRealtime() - start;
60
61 final Bundle results = new Bundle();
62 results.putLong("duration", duration);
63 InstrumentationRegistry.getInstrumentation().sendStatus(0, results);
64 }
65
66 public static class ObtainThread extends Thread {
67 public int iterations;
68
69 public ObtainThread(int iterations) {
70 this.iterations = iterations;
71 }
72
73 @Override
74 public void run() {
75 while (iterations-- > 0) {
76 final Parcel data = Parcel.obtain();
77 final Parcel reply = Parcel.obtain();
78 try {
79 data.writeInt(32);
80 reply.writeInt(32);
81 } finally {
82 reply.recycle();
83 data.recycle();
84 }
85 }
86 }
87 }
88 }