| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
} |