diff --git a/apct-tests/perftests/core/src/android/libcore/regression/IdnPerfTest.java b/apct-tests/perftests/core/src/android/libcore/regression/IdnPerfTest.java new file mode 100644 index 0000000000000..c60930f99682b --- /dev/null +++ b/apct-tests/perftests/core/src/android/libcore/regression/IdnPerfTest.java @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.libcore.regression; + +import android.perftests.utils.BenchmarkState; +import android.perftests.utils.PerfStatusReporter; +import android.test.suitebuilder.annotation.LargeTest; + +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.net.IDN; + +@RunWith(AndroidJUnit4.class) +@LargeTest +public class IdnPerfTest { + @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); + + @Test + public void timeToUnicode() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + IDN.toASCII("fass.de"); + IDN.toASCII("faß.de"); + IDN.toASCII("fäß.de"); + IDN.toASCII("a\u200Cb"); + IDN.toASCII("öbb.at"); + IDN.toASCII("abc・日本.co.jp"); + IDN.toASCII("日本.co.jp"); + IDN.toASCII("x\u0327\u0301.de"); + IDN.toASCII("σόλοσ.gr"); + } + } + + @Test + public void timeToAscii() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + IDN.toUnicode("xn--fss-qla.de"); + IDN.toUnicode("xn--n00d.com"); + IDN.toUnicode("xn--bb-eka.at"); + IDN.toUnicode("xn--og-09a.de"); + IDN.toUnicode("xn--53h.de"); + IDN.toUnicode("xn--iny-zx5a.de"); + IDN.toUnicode("xn--abc-rs4b422ycvb.co.jp"); + IDN.toUnicode("xn--wgv71a.co.jp"); + IDN.toUnicode("xn--x-xbb7i.de"); + IDN.toUnicode("xn--wxaikc6b.gr"); + IDN.toUnicode("xn--wxaikc6b.xn--gr-gtd9a1b0g.de"); + } + } +} diff --git a/apct-tests/perftests/core/src/android/libcore/regression/IntConstantDivisionPerfTest.java b/apct-tests/perftests/core/src/android/libcore/regression/IntConstantDivisionPerfTest.java new file mode 100644 index 0000000000000..abcc972b0ac36 --- /dev/null +++ b/apct-tests/perftests/core/src/android/libcore/regression/IntConstantDivisionPerfTest.java @@ -0,0 +1,107 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.libcore.regression; + +import android.perftests.utils.BenchmarkState; +import android.perftests.utils.PerfStatusReporter; +import android.test.suitebuilder.annotation.LargeTest; + +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(AndroidJUnit4.class) +@LargeTest +public class IntConstantDivisionPerfTest { + @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); + + @Test + public void timeDivideIntByConstant2() { + int result = 1; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result /= 2; + } + } + + @Test + public void timeDivideIntByConstant8() { + int result = 1; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result /= 8; + } + } + + @Test + public void timeDivideIntByConstant10() { + int result = 1; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result /= 10; + } + } + + @Test + public void timeDivideIntByConstant100() { + int result = 1; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result /= 100; + } + } + + @Test + public void timeDivideIntByConstant100_HandOptimized() { + int result = 1; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = (int) ((0x51eb851fL * result) >>> 37); + } + } + + @Test + public void timeDivideIntByConstant2048() { + int result = 1; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result /= 2048; + } + } + + @Test + public void timeDivideIntByVariable2() { + int result = 1; + int factor = 2; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result /= factor; + } + } + + @Test + public void timeDivideIntByVariable10() { + int result = 1; + int factor = 10; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result /= factor; + } + } +} diff --git a/apct-tests/perftests/core/src/android/libcore/regression/IntConstantMultiplicationPerfTest.java b/apct-tests/perftests/core/src/android/libcore/regression/IntConstantMultiplicationPerfTest.java new file mode 100644 index 0000000000000..c9f06166e33b7 --- /dev/null +++ b/apct-tests/perftests/core/src/android/libcore/regression/IntConstantMultiplicationPerfTest.java @@ -0,0 +1,134 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.libcore.regression; + +import android.perftests.utils.BenchmarkState; +import android.perftests.utils.PerfStatusReporter; +import android.test.suitebuilder.annotation.LargeTest; + +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(AndroidJUnit4.class) +@LargeTest +public class IntConstantMultiplicationPerfTest { + @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); + + @Test + public void timeMultiplyIntByConstant6() { + int result = 1; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result *= 6; + } + } + + @Test + public void timeMultiplyIntByConstant7() { + int result = 1; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result *= 7; + } + } + + @Test + public void timeMultiplyIntByConstant8() { + int result = 1; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result *= 8; + } + } + + @Test + public void timeMultiplyIntByConstant8_Shift() { + int result = 1; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result <<= 3; + } + } + + @Test + public void timeMultiplyIntByConstant10() { + int result = 1; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result *= 10; + } + } + + @Test + public void timeMultiplyIntByConstant10_Shift() { + int result = 1; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = (result + (result << 2)) << 1; + } + } + + @Test + public void timeMultiplyIntByConstant2047() { + int result = 1; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result *= 2047; + } + } + + @Test + public void timeMultiplyIntByConstant2048() { + int result = 1; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result *= 2048; + } + } + + @Test + public void timeMultiplyIntByConstant2049() { + int result = 1; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result *= 2049; + } + } + + @Test + public void timeMultiplyIntByVariable10() { + int result = 1; + int factor = 10; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result *= factor; + } + } + + @Test + public void timeMultiplyIntByVariable8() { + int result = 1; + int factor = 8; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result *= factor; + } + } +} diff --git a/apct-tests/perftests/core/src/android/libcore/regression/IntConstantRemainderPerfTest.java b/apct-tests/perftests/core/src/android/libcore/regression/IntConstantRemainderPerfTest.java new file mode 100644 index 0000000000000..78f744c1b938b --- /dev/null +++ b/apct-tests/perftests/core/src/android/libcore/regression/IntConstantRemainderPerfTest.java @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.libcore.regression; + +import android.perftests.utils.BenchmarkState; +import android.perftests.utils.PerfStatusReporter; +import android.test.suitebuilder.annotation.LargeTest; + +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(AndroidJUnit4.class) +@LargeTest +public class IntConstantRemainderPerfTest { + @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); + + @Test + public void timeRemainderIntByConstant2() { + int result = 1; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result %= 2; + } + } + + @Test + public void timeRemainderIntByConstant8() { + int result = 1; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result %= 8; + } + } + + @Test + public void timeRemainderIntByConstant10() { + int result = 1; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result %= 10; + } + } + + @Test + public void timeRemainderIntByConstant100() { + int result = 1; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result %= 100; + } + } + + @Test + public void timeRemainderIntByConstant2048() { + int result = 1; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result %= 2048; + } + } + + @Test + public void timeRemainderIntByVariable2() { + int result = 1; + int factor = 2; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result %= factor; + } + } + + @Test + public void timeRemainderIntByVariable10() { + int result = 1; + int factor = 10; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result %= factor; + } + } +} diff --git a/apct-tests/perftests/core/src/android/libcore/regression/IntegerPerfTest.java b/apct-tests/perftests/core/src/android/libcore/regression/IntegerPerfTest.java new file mode 100644 index 0000000000000..170bb58c46ed1 --- /dev/null +++ b/apct-tests/perftests/core/src/android/libcore/regression/IntegerPerfTest.java @@ -0,0 +1,173 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.libcore.regression; + +import android.perftests.utils.BenchmarkState; +import android.perftests.utils.PerfStatusReporter; + +import org.junit.Rule; +import org.junit.Test; + +public class IntegerPerfTest { + @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); + + @Test + public void timeLongSignumBranch() { + int t = 0; + int i = 0; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + t += signum1(-(++i)); + t += signum1(0); + t += signum1(i); + } + } + + @Test + public void timeLongSignumBranchFree() { + int t = 0; + int i = 0; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + t += signum2(-(++i)); + t += signum2(0); + t += signum2(i); + } + } + + private static int signum1(long v) { + return v < 0 ? -1 : (v == 0 ? 0 : 1); + } + + private static int signum2(long v) { + return ((int) (v >> 63)) | (int) (-v >>> 63); // Hacker's delight 2-7 + } + + @Test + public void timeLongBitCount_BitSet() { + int t = 0; + int i = 0; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + t += pop((long) ++i); + } + } + + private static int pop(long l) { + int count = popX(l & 0xffffffffL); + count += popX(l >>> 32); + return count; + } + + private static int popX(long x) { + // BEGIN android-note + // delegate to Integer.bitCount(i); consider using native code + // END android-note + x = x - ((x >>> 1) & 0x55555555); + x = (x & 0x33333333) + ((x >>> 2) & 0x33333333); + x = (x + (x >>> 4)) & 0x0f0f0f0f; + x = x + (x >>> 8); + x = x + (x >>> 16); + return (int) x & 0x0000003f; + } + + @Test + public void timeLongBitCount_2Int() { + int t = 0; + int i = 0; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + t += pop2((long) ++i); + } + } + + private static int pop2(long l) { + int count = Integer.bitCount((int) (l & 0xffffffffL)); + count += Integer.bitCount((int) (l >>> 32)); + return count; + } + + @Test + public void timeLongBitCount_Long() { + int t = 0; + int i = 0; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + t += Long.bitCount((long) ++i); + } + } + + /** + * Table for Seal's algorithm for Number of Trailing Zeros. Hacker's Delight online, Figure 5-18 + * (http://www.hackersdelight.org/revisions.pdf) The entries whose value is -1 are never + * referenced. + */ + private static final byte[] NTZ_TABLE = { + 32, 0, 1, 12, 2, 6, -1, 13, 3, -1, 7, -1, -1, -1, -1, 14, + 10, 4, -1, -1, 8, -1, -1, 25, -1, -1, -1, -1, -1, 21, 27, 15, + 31, 11, 5, -1, -1, -1, -1, -1, 9, -1, -1, 24, -1, -1, 20, 26, + 30, -1, -1, -1, -1, 23, -1, 19, 29, -1, 22, 18, 28, 17, 16, -1 + }; + + private static int numberOfTrailingZerosHD(int i) { + // Seal's algorithm - Hacker's Delight 5-18 + i &= -i; + i = (i << 4) + i; // x *= 17 + i = (i << 6) + i; // x *= 65 + i = (i << 16) - i; // x *= 65535 + return NTZ_TABLE[i >>> 26]; + } + + private static int numberOfTrailingZerosOL(int i) { + return NTZ_TABLE[((i & -i) * 0x0450FBAF) >>> 26]; + } + + @Test + public void timeNumberOfTrailingZerosHD() { + int t = 0; + int i = 0; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + t += numberOfTrailingZerosHD(++i); + } + } + + @Test + public void timeNumberOfTrailingZerosOL() { + int t = 0; + int i = 0; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + t += numberOfTrailingZerosOL(++i); + } + } + + @Test + public void timeIntegerValueOf() throws Exception { + String[] intStrings = + new String[] { + "0", "1", "12", "123", "1234", "12345", "123456", "1234567", "12345678" + }; + int t = 0; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + for (int j = 0; j < intStrings.length; ++j) { + t += Integer.valueOf(intStrings[j]); + } + } + } +} diff --git a/apct-tests/perftests/core/src/android/libcore/regression/IntegralToStringPerfTest.java b/apct-tests/perftests/core/src/android/libcore/regression/IntegralToStringPerfTest.java new file mode 100644 index 0000000000000..5129fcbfb2472 --- /dev/null +++ b/apct-tests/perftests/core/src/android/libcore/regression/IntegralToStringPerfTest.java @@ -0,0 +1,229 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.libcore.regression; + +import android.perftests.utils.BenchmarkState; +import android.perftests.utils.PerfStatusReporter; +import android.test.suitebuilder.annotation.LargeTest; + +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(AndroidJUnit4.class) +@LargeTest +public class IntegralToStringPerfTest { + @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); + + private static final int SMALL = 12; + private static final int MEDIUM = 12345; + private static final int LARGE = 12345678; + + @Test + public void time_IntegerToString_small() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Integer.toString(SMALL); + } + } + + @Test + public void time_IntegerToString_medium() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Integer.toString(MEDIUM); + } + } + + @Test + public void time_IntegerToString_large() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Integer.toString(LARGE); + } + } + + @Test + public void time_IntegerToString2_small() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Integer.toString(SMALL, 2); + } + } + + @Test + public void time_IntegerToString2_medium() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Integer.toString(MEDIUM, 2); + } + } + + @Test + public void time_IntegerToString2_large() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Integer.toString(LARGE, 2); + } + } + + @Test + public void time_IntegerToString10_small() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Integer.toString(SMALL, 10); + } + } + + @Test + public void time_IntegerToString10_medium() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Integer.toString(MEDIUM, 10); + } + } + + @Test + public void time_IntegerToString10_large() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Integer.toString(LARGE, 10); + } + } + + @Test + public void time_IntegerToString16_small() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Integer.toString(SMALL, 16); + } + } + + @Test + public void time_IntegerToString16_medium() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Integer.toString(MEDIUM, 16); + } + } + + @Test + public void time_IntegerToString16_large() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Integer.toString(LARGE, 16); + } + } + + @Test + public void time_IntegerToBinaryString_small() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Integer.toBinaryString(SMALL); + } + } + + @Test + public void time_IntegerToBinaryString_medium() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Integer.toBinaryString(MEDIUM); + } + } + + @Test + public void time_IntegerToBinaryString_large() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Integer.toBinaryString(LARGE); + } + } + + @Test + public void time_IntegerToHexString_small() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Integer.toHexString(SMALL); + } + } + + @Test + public void time_IntegerToHexString_medium() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Integer.toHexString(MEDIUM); + } + } + + @Test + public void time_IntegerToHexString_large() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Integer.toHexString(LARGE); + } + } + + @Test + public void time_StringBuilder_small() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + new StringBuilder().append(SMALL); + } + } + + @Test + public void time_StringBuilder_medium() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + new StringBuilder().append(MEDIUM); + } + } + + @Test + public void time_StringBuilder_large() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + new StringBuilder().append(LARGE); + } + } + + @Test + public void time_Formatter_small() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + String.format("%d", SMALL); + } + } + + @Test + public void time_Formatter_medium() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + String.format("%d", MEDIUM); + } + } + + @Test + public void time_Formatter_large() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + String.format("%d", LARGE); + } + } +} diff --git a/apct-tests/perftests/core/src/android/libcore/regression/KeyPairGeneratorPerfTest.java b/apct-tests/perftests/core/src/android/libcore/regression/KeyPairGeneratorPerfTest.java new file mode 100644 index 0000000000000..6fe9059cb3de5 --- /dev/null +++ b/apct-tests/perftests/core/src/android/libcore/regression/KeyPairGeneratorPerfTest.java @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.libcore.regression; + +import android.perftests.utils.BenchmarkState; +import android.perftests.utils.PerfStatusReporter; +import android.test.suitebuilder.annotation.LargeTest; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.SecureRandom; +import java.util.Arrays; +import java.util.Collection; + +@RunWith(Parameterized.class) +@LargeTest +public class KeyPairGeneratorPerfTest { + @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); + + @Parameters(name = "mAlgorithm={0}, mImplementation={1}") + public static Collection data() { + return Arrays.asList( + new Object[][] { + {Algorithm.RSA, Implementation.BouncyCastle}, + {Algorithm.DSA, Implementation.BouncyCastle}, + {Algorithm.RSA, Implementation.OpenSSL} + }); + } + + @Parameterized.Parameter(0) + public Algorithm mAlgorithm; + + @Parameterized.Parameter(1) + public Implementation mImplementation; + + public enum Algorithm { + RSA, + DSA, + }; + + public enum Implementation { + OpenSSL, + BouncyCastle + }; + + private String mGeneratorAlgorithm; + private KeyPairGenerator mGenerator; + private SecureRandom mRandom; + + @Before + public void setUp() throws Exception { + this.mGeneratorAlgorithm = mAlgorithm.toString(); + + final String provider; + if (mImplementation == Implementation.BouncyCastle) { + provider = "BC"; + } else { + provider = "AndroidOpenSSL"; + } + + this.mGenerator = KeyPairGenerator.getInstance(mGeneratorAlgorithm, provider); + this.mRandom = SecureRandom.getInstance("SHA1PRNG"); + this.mGenerator.initialize(1024); + } + + @Test + public void time() throws Exception { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + KeyPair keyPair = mGenerator.generateKeyPair(); + } + } +} diff --git a/apct-tests/perftests/core/src/android/libcore/regression/LoopingBackwardsPerfTest.java b/apct-tests/perftests/core/src/android/libcore/regression/LoopingBackwardsPerfTest.java new file mode 100644 index 0000000000000..414764d292b8a --- /dev/null +++ b/apct-tests/perftests/core/src/android/libcore/regression/LoopingBackwardsPerfTest.java @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.libcore.regression; + +import android.perftests.utils.BenchmarkState; +import android.perftests.utils.PerfStatusReporter; +import android.test.suitebuilder.annotation.LargeTest; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +import java.util.Arrays; +import java.util.Collection; + +/** + * Testing the old canard that looping backwards is faster. + * + * @author Kevin Bourrillion + */ +@RunWith(Parameterized.class) +@LargeTest +public class LoopingBackwardsPerfTest { + @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); + + @Parameters(name = "mMax={0}") + public static Collection data() { + return Arrays.asList(new Object[][] {{2}, {20}, {2000}, {20000000}}); + } + + @Parameterized.Parameter(0) + public int mMax; + + @Test + public void timeForwards() { + int fake = 0; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + for (int j = 0; j < mMax; j++) { + fake += j; + } + } + } + + @Test + public void timeBackwards() { + int fake = 0; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + for (int j = mMax - 1; j >= 0; j--) { + fake += j; + } + } + } +} diff --git a/apct-tests/perftests/core/src/android/libcore/regression/MathPerfTest.java b/apct-tests/perftests/core/src/android/libcore/regression/MathPerfTest.java new file mode 100644 index 0000000000000..4c2d7fb7e6631 --- /dev/null +++ b/apct-tests/perftests/core/src/android/libcore/regression/MathPerfTest.java @@ -0,0 +1,551 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.libcore.regression; + +import android.perftests.utils.BenchmarkState; +import android.perftests.utils.PerfStatusReporter; +import android.test.suitebuilder.annotation.LargeTest; + +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * Many of these tests are bogus in that the cost will vary wildly depending on inputs. For _my_ + * current purposes, that's okay. But beware! + */ +@RunWith(AndroidJUnit4.class) +@LargeTest +public class MathPerfTest { + @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); + + private final double mDouble = 1.2; + private final float mFloat = 1.2f; + private final int mInt = 1; + private final long mLong = 1L; + + // NOTE: To avoid the benchmarked function from being optimized away, we store the result + // and use it as the benchmark's return value. This is good enough for now but may not be in + // the future, a smart compiler could determine that the result value will depend on whether + // we get into the loop or not and turn the whole loop into an if statement. + + @Test + public void timeAbsD() { + double result = mDouble; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.abs(mDouble); + } + } + + @Test + public void timeAbsF() { + float result = mFloat; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.abs(mFloat); + } + } + + @Test + public void timeAbsI() { + int result = mInt; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.abs(mInt); + } + } + + @Test + public void timeAbsL() { + long result = mLong; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.abs(mLong); + } + } + + @Test + public void timeAcos() { + double result = mDouble; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.acos(mDouble); + } + } + + @Test + public void timeAsin() { + double result = mDouble; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.asin(mDouble); + } + } + + @Test + public void timeAtan() { + double result = mDouble; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.atan(mDouble); + } + } + + @Test + public void timeAtan2() { + double result = mDouble; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.atan2(3, 4); + } + } + + @Test + public void timeCbrt() { + double result = mDouble; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.cbrt(mDouble); + } + } + + @Test + public void timeCeil() { + double result = mDouble; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.ceil(mDouble); + } + } + + @Test + public void timeCopySignD() { + double result = mDouble; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.copySign(mDouble, mDouble); + } + } + + @Test + public void timeCopySignF() { + float result = mFloat; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.copySign(mFloat, mFloat); + } + } + + @Test + public void timeCopySignD_strict() { + double result = mDouble; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = StrictMath.copySign(mDouble, mDouble); + } + } + + @Test + public void timeCopySignF_strict() { + float result = mFloat; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = StrictMath.copySign(mFloat, mFloat); + } + } + + @Test + public void timeCos() { + double result = mDouble; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.cos(mDouble); + } + } + + @Test + public void timeCosh() { + double result = mDouble; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.cosh(mDouble); + } + } + + @Test + public void timeExp() { + double result = mDouble; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.exp(mDouble); + } + } + + @Test + public void timeExpm1() { + double result = mDouble; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.expm1(mDouble); + } + } + + @Test + public void timeFloor() { + double result = mDouble; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.floor(mDouble); + } + } + + @Test + public void timeGetExponentD() { + int result = mInt; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.getExponent(mDouble); + } + } + + @Test + public void timeGetExponentF() { + int result = mInt; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.getExponent(mFloat); + } + } + + @Test + public void timeHypot() { + double result = mDouble; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.hypot(mDouble, mDouble); + } + } + + @Test + public void timeIEEEremainder() { + double result = mDouble; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.IEEEremainder(mDouble, mDouble); + } + } + + @Test + public void timeLog() { + double result = mDouble; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.log(mDouble); + } + } + + @Test + public void timeLog10() { + double result = mDouble; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.log10(mDouble); + } + } + + @Test + public void timeLog1p() { + double result = mDouble; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.log1p(mDouble); + } + } + + @Test + public void timeMaxD() { + double result = mDouble; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.max(mDouble, mDouble); + } + } + + @Test + public void timeMaxF() { + float result = mFloat; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.max(mFloat, mFloat); + } + } + + @Test + public void timeMaxI() { + int result = mInt; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.max(mInt, mInt); + } + } + + @Test + public void timeMaxL() { + long result = mLong; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.max(mLong, mLong); + } + } + + @Test + public void timeMinD() { + double result = mDouble; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.min(mDouble, mDouble); + } + } + + @Test + public void timeMinF() { + float result = mFloat; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.min(mFloat, mFloat); + } + } + + @Test + public void timeMinI() { + int result = mInt; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.min(mInt, mInt); + } + } + + @Test + public void timeMinL() { + long result = mLong; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.min(mLong, mLong); + } + } + + @Test + public void timeNextAfterD() { + double result = mDouble; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.nextAfter(mDouble, mDouble); + } + } + + @Test + public void timeNextAfterF() { + float result = mFloat; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.nextAfter(mFloat, mFloat); + } + } + + @Test + public void timeNextUpD() { + double result = mDouble; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.nextUp(mDouble); + } + } + + @Test + public void timeNextUpF() { + float result = mFloat; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.nextUp(mFloat); + } + } + + @Test + public void timePow() { + double result = mDouble; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.pow(mDouble, mDouble); + } + } + + @Test + public void timeRandom() { + double result = mDouble; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.random(); + } + } + + @Test + public void timeRint() { + double result = mDouble; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.rint(mDouble); + } + } + + @Test + public void timeRoundD() { + long result = mLong; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.round(mDouble); + } + } + + @Test + public void timeRoundF() { + int result = mInt; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.round(mFloat); + } + } + + @Test + public void timeScalbD() { + double result = mDouble; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.scalb(mDouble, 5); + } + } + + @Test + public void timeScalbF() { + float result = mFloat; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.scalb(mFloat, 5); + } + } + + @Test + public void timeSignumD() { + double result = mDouble; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.signum(mDouble); + } + } + + @Test + public void timeSignumF() { + float result = mFloat; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.signum(mFloat); + } + } + + @Test + public void timeSin() { + double result = mDouble; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.sin(mDouble); + } + } + + @Test + public void timeSinh() { + double result = mDouble; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.sinh(mDouble); + } + } + + @Test + public void timeSqrt() { + double result = mDouble; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.sqrt(mDouble); + } + } + + @Test + public void timeTan() { + double result = mDouble; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.tan(mDouble); + } + } + + @Test + public void timeTanh() { + double result = mDouble; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.tanh(mDouble); + } + } + + @Test + public void timeToDegrees() { + double result = mDouble; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.toDegrees(mDouble); + } + } + + @Test + public void timeToRadians() { + double result = mDouble; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.toRadians(mDouble); + } + } + + @Test + public void timeUlpD() { + double result = mDouble; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.ulp(mDouble); + } + } + + @Test + public void timeUlpF() { + float result = mFloat; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Math.ulp(mFloat); + } + } +} diff --git a/apct-tests/perftests/core/src/android/libcore/regression/MessageDigestPerfTest.java b/apct-tests/perftests/core/src/android/libcore/regression/MessageDigestPerfTest.java new file mode 100644 index 0000000000000..a27e16a46a045 --- /dev/null +++ b/apct-tests/perftests/core/src/android/libcore/regression/MessageDigestPerfTest.java @@ -0,0 +1,209 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.libcore.regression; + +import android.perftests.utils.BenchmarkState; +import android.perftests.utils.PerfStatusReporter; +import android.test.suitebuilder.annotation.LargeTest; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +import java.nio.ByteBuffer; +import java.security.MessageDigest; +import java.util.Arrays; +import java.util.Collection; + +@RunWith(Parameterized.class) +@LargeTest +public class MessageDigestPerfTest { + @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); + + @Parameters(name = "mAlgorithm={0}") + public static Collection data() { + return Arrays.asList( + new Object[][] { + {Algorithm.MD5}, + {Algorithm.SHA1}, + {Algorithm.SHA256}, + {Algorithm.SHA384}, + {Algorithm.SHA512} + }); + } + + @Parameterized.Parameter(0) + public Algorithm mAlgorithm; + + public String mProvider = "AndroidSSL"; + + private static final int DATA_SIZE = 8192; + private static final byte[] DATA = new byte[DATA_SIZE]; + + static { + for (int i = 0; i < DATA_SIZE; i++) { + DATA[i] = (byte) i; + } + } + + private static final int LARGE_DATA_SIZE = 256 * 1024; + private static final byte[] LARGE_DATA = new byte[LARGE_DATA_SIZE]; + + static { + for (int i = 0; i < LARGE_DATA_SIZE; i++) { + LARGE_DATA[i] = (byte) i; + } + } + + private static final ByteBuffer SMALL_BUFFER = ByteBuffer.wrap(DATA); + private static final ByteBuffer SMALL_DIRECT_BUFFER = ByteBuffer.allocateDirect(DATA_SIZE); + + static { + SMALL_DIRECT_BUFFER.put(DATA); + SMALL_DIRECT_BUFFER.flip(); + } + + private static final ByteBuffer LARGE_BUFFER = ByteBuffer.wrap(LARGE_DATA); + private static final ByteBuffer LARGE_DIRECT_BUFFER = + ByteBuffer.allocateDirect(LARGE_DATA_SIZE); + + static { + LARGE_DIRECT_BUFFER.put(LARGE_DATA); + LARGE_DIRECT_BUFFER.flip(); + } + + public enum Algorithm { + MD5, + SHA1, + SHA256, + SHA384, + SHA512 + }; + + @Test + public void time() throws Exception { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + MessageDigest digest = + MessageDigest.getInstance(mAlgorithm.toString(), mProvider); + digest.update(DATA, 0, DATA_SIZE); + digest.digest(); + } + } + + @Test + public void timeLargeArray() throws Exception { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + MessageDigest digest = + MessageDigest.getInstance(mAlgorithm.toString(), mProvider); + digest.update(LARGE_DATA, 0, LARGE_DATA_SIZE); + digest.digest(); + } + } + + @Test + public void timeSmallChunkOfLargeArray() throws Exception { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + MessageDigest digest = + MessageDigest.getInstance(mAlgorithm.toString(), mProvider); + digest.update(LARGE_DATA, LARGE_DATA_SIZE / 2, DATA_SIZE); + digest.digest(); + } + } + + @Test + public void timeSmallByteBuffer() throws Exception { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + MessageDigest digest = + MessageDigest.getInstance(mAlgorithm.toString(), mProvider); + SMALL_BUFFER.position(0); + SMALL_BUFFER.limit(SMALL_BUFFER.capacity()); + digest.update(SMALL_BUFFER); + digest.digest(); + } + } + + @Test + public void timeSmallDirectByteBuffer() throws Exception { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + MessageDigest digest = + MessageDigest.getInstance(mAlgorithm.toString(), mProvider); + SMALL_DIRECT_BUFFER.position(0); + SMALL_DIRECT_BUFFER.limit(SMALL_DIRECT_BUFFER.capacity()); + digest.update(SMALL_DIRECT_BUFFER); + digest.digest(); + } + } + + @Test + public void timeLargeByteBuffer() throws Exception { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + MessageDigest digest = + MessageDigest.getInstance(mAlgorithm.toString(), mProvider); + LARGE_BUFFER.position(0); + LARGE_BUFFER.limit(LARGE_BUFFER.capacity()); + digest.update(LARGE_BUFFER); + digest.digest(); + } + } + + @Test + public void timeLargeDirectByteBuffer() throws Exception { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + MessageDigest digest = + MessageDigest.getInstance(mAlgorithm.toString(), mProvider); + LARGE_DIRECT_BUFFER.position(0); + LARGE_DIRECT_BUFFER.limit(LARGE_DIRECT_BUFFER.capacity()); + digest.update(LARGE_DIRECT_BUFFER); + digest.digest(); + } + } + + @Test + public void timeSmallChunkOfLargeByteBuffer() throws Exception { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + MessageDigest digest = + MessageDigest.getInstance(mAlgorithm.toString(), mProvider); + LARGE_BUFFER.position(LARGE_BUFFER.capacity() / 2); + LARGE_BUFFER.limit(LARGE_BUFFER.position() + DATA_SIZE); + digest.update(LARGE_BUFFER); + digest.digest(); + } + } + + @Test + public void timeSmallChunkOfLargeDirectByteBuffer() throws Exception { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + MessageDigest digest = + MessageDigest.getInstance(mAlgorithm.toString(), mProvider); + LARGE_DIRECT_BUFFER.position(LARGE_DIRECT_BUFFER.capacity() / 2); + LARGE_DIRECT_BUFFER.limit(LARGE_DIRECT_BUFFER.position() + DATA_SIZE); + digest.update(LARGE_DIRECT_BUFFER); + digest.digest(); + } + } +} diff --git a/apct-tests/perftests/core/src/android/libcore/regression/MutableIntPerfTest.java b/apct-tests/perftests/core/src/android/libcore/regression/MutableIntPerfTest.java new file mode 100644 index 0000000000000..37bd73c8731a2 --- /dev/null +++ b/apct-tests/perftests/core/src/android/libcore/regression/MutableIntPerfTest.java @@ -0,0 +1,124 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.libcore.regression; + +import android.perftests.utils.BenchmarkState; +import android.perftests.utils.PerfStatusReporter; +import android.test.suitebuilder.annotation.LargeTest; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +import java.util.Arrays; +import java.util.Collection; +import java.util.concurrent.atomic.AtomicInteger; + +@RunWith(Parameterized.class) +@LargeTest +public final class MutableIntPerfTest { + @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); + + enum Kind { + ARRAY() { + int[] mValue = new int[1]; + + @Override + void timeCreate(BenchmarkState state) { + while (state.keepRunning()) { + mValue = new int[] {5}; + } + } + + @Override + void timeIncrement(BenchmarkState state) { + while (state.keepRunning()) { + mValue[0]++; + } + } + + @Override + int timeGet(BenchmarkState state) { + int sum = 0; + while (state.keepRunning()) { + sum += mValue[0]; + } + return sum; + } + }, + ATOMIC() { + AtomicInteger mValue = new AtomicInteger(); + + @Override + void timeCreate(BenchmarkState state) { + while (state.keepRunning()) { + mValue = new AtomicInteger(5); + } + } + + @Override + void timeIncrement(BenchmarkState state) { + while (state.keepRunning()) { + mValue.incrementAndGet(); + } + } + + @Override + int timeGet(BenchmarkState state) { + int sum = 0; + while (state.keepRunning()) { + sum += mValue.intValue(); + } + return sum; + } + }; + + abstract void timeCreate(BenchmarkState state); + + abstract void timeIncrement(BenchmarkState state); + + abstract int timeGet(BenchmarkState state); + } + + @Parameters(name = "mKind={0}") + public static Collection data() { + return Arrays.asList(new Object[][] {{Kind.ARRAY}, {Kind.ATOMIC}}); + } + + @Parameterized.Parameter(0) + public Kind mKind; + + @Test + public void timeCreate() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + mKind.timeCreate(state); + } + + @Test + public void timeIncrement() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + mKind.timeIncrement(state); + } + + @Test + public void timeGet() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + mKind.timeGet(state); + } +} diff --git a/apct-tests/perftests/core/src/android/libcore/regression/NumberFormatPerfTest.java b/apct-tests/perftests/core/src/android/libcore/regression/NumberFormatPerfTest.java new file mode 100644 index 0000000000000..dae185e1209c4 --- /dev/null +++ b/apct-tests/perftests/core/src/android/libcore/regression/NumberFormatPerfTest.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.libcore.regression; + +import android.perftests.utils.BenchmarkState; +import android.perftests.utils.PerfStatusReporter; +import android.test.suitebuilder.annotation.LargeTest; + +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.text.NumberFormat; +import java.util.Locale; + +@RunWith(AndroidJUnit4.class) +@LargeTest +public class NumberFormatPerfTest { + @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); + + private static Locale sLocale = Locale.getDefault(Locale.Category.FORMAT); + + @Test + public void time_instantiation() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + NumberFormat.getInstance(sLocale); + } + } +} diff --git a/apct-tests/perftests/core/src/android/libcore/regression/PriorityQueuePerfTest.java b/apct-tests/perftests/core/src/android/libcore/regression/PriorityQueuePerfTest.java new file mode 100644 index 0000000000000..8801a5690cb2d --- /dev/null +++ b/apct-tests/perftests/core/src/android/libcore/regression/PriorityQueuePerfTest.java @@ -0,0 +1,127 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.libcore.regression; + +import android.perftests.utils.BenchmarkState; +import android.perftests.utils.PerfStatusReporter; +import android.test.suitebuilder.annotation.LargeTest; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.PriorityQueue; +import java.util.Random; + +@RunWith(Parameterized.class) +@LargeTest +public class PriorityQueuePerfTest { + @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); + + @Parameters(name = "mQueueSize={0}, mHitRate={1}") + public static Collection data() { + return Arrays.asList( + new Object[][] { + {100, 0}, + {1000, 0}, + {10000, 0}, + {100, 25}, + {1000, 25}, + {10000, 25}, + {100, 50}, + {1000, 50}, + {10000, 50}, + {100, 75}, + {1000, 75}, + {10000, 75}, + {100, 100}, + {1000, 100}, + {10000, 100} + }); + } + + @Parameterized.Parameter(0) + public int mQueueSize; + + @Parameterized.Parameter(1) + public int mHitRate; + + private PriorityQueue mPq; + private PriorityQueue mUsepq; + private List mSeekElements; + private Random mRandom = new Random(189279387L); + + @Before + public void setUp() throws Exception { + mPq = new PriorityQueue(); + mUsepq = new PriorityQueue(); + mSeekElements = new ArrayList(); + List allElements = new ArrayList(); + int numShared = (int) (mQueueSize * ((double) mHitRate / 100)); + // the total number of elements we require to engineer a hit rate of mHitRate% + int totalElements = 2 * mQueueSize - numShared; + for (int i = 0; i < totalElements; i++) { + allElements.add(i); + } + // shuffle these elements so that we get a reasonable distribution of missed elements + Collections.shuffle(allElements, mRandom); + // add shared elements + for (int i = 0; i < numShared; i++) { + mPq.add(allElements.get(i)); + mSeekElements.add(allElements.get(i)); + } + // add priority queue only elements (these won't be touched) + for (int i = numShared; i < mQueueSize; i++) { + mPq.add(allElements.get(i)); + } + // add non-priority queue elements (these will be misses) + for (int i = mQueueSize; i < totalElements; i++) { + mSeekElements.add(allElements.get(i)); + } + mUsepq = new PriorityQueue(mPq); + // shuffle again so that elements are accessed in a different pattern than they were + // inserted + Collections.shuffle(mSeekElements, mRandom); + } + + @Test + public void timeRemove() { + boolean fake = false; + int elementsSize = mSeekElements.size(); + // At most allow the queue to empty 10%. + int resizingThreshold = mQueueSize / 10; + int i = 0; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + // Reset queue every so often. This will be called more often for smaller + // mQueueSizes, but since a copy is linear, it will also cost proportionally + // less, and hopefully it will approximately balance out. + if (++i % resizingThreshold == 0) { + mUsepq = new PriorityQueue(mPq); + } + fake = mUsepq.remove(mSeekElements.get(i % elementsSize)); + } + } +} diff --git a/apct-tests/perftests/core/src/android/libcore/regression/PropertyAccessPerfTest.java b/apct-tests/perftests/core/src/android/libcore/regression/PropertyAccessPerfTest.java new file mode 100644 index 0000000000000..21ccba5cc2e75 --- /dev/null +++ b/apct-tests/perftests/core/src/android/libcore/regression/PropertyAccessPerfTest.java @@ -0,0 +1,149 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.libcore.regression; + +import android.perftests.utils.BenchmarkState; +import android.perftests.utils.PerfStatusReporter; +import android.test.suitebuilder.annotation.LargeTest; + +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +@RunWith(AndroidJUnit4.class) +@LargeTest +public final class PropertyAccessPerfTest { + @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); + + private View mView = new View(); + private Method mSetX; + private GeneratedProperty mGeneratedSetter = new GeneratedSetter(); + private GeneratedProperty mGeneratedField = new GeneratedField(); + private Field mX; + private Object[] mArgsBomX = new Object[1]; + + @Before + public void setUp() throws Exception { + mSetX = View.class.getDeclaredMethod("mSetX", float.class); + mX = View.class.getDeclaredField("mX"); + } + + @Test + public void timeDirectSetter() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + mView.mSetX(0.1f); + } + } + + @Test + public void timeDirectFieldSet() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + mView.mX = 0.1f; + } + } + + @Test + public void timeDirectSetterAndBomXing() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Float value = 0.1f; + mView.mSetX(value); + } + } + + @Test + public void timeDirectFieldSetAndBomXing() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Float value = 0.1f; + mView.mX = value; + } + } + + @Test + public void timeReflectionSetterAndTwoBomXes() throws Exception { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + mSetX.invoke(mView, 0.1f); + } + } + + @Test + public void timeReflectionSetterAndOneBomX() throws Exception { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + mArgsBomX[0] = 0.1f; + mSetX.invoke(mView, mArgsBomX); + } + } + + @Test + public void timeReflectionFieldSet() throws Exception { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + mX.setFloat(mView, 0.1f); + } + } + + @Test + public void timeGeneratedSetter() throws Exception { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + mGeneratedSetter.setFloat(mView, 0.1f); + } + } + + @Test + public void timeGeneratedFieldSet() throws Exception { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + mGeneratedField.setFloat(mView, 0.1f); + } + } + + static class View { + float mX; + + public void mSetX(float mX) { + this.mX = mX; + } + } + + interface GeneratedProperty { + void setFloat(View v, float f); + } + + static class GeneratedSetter implements GeneratedProperty { + public void setFloat(View v, float f) { + v.mSetX(f); + } + } + + static class GeneratedField implements GeneratedProperty { + public void setFloat(View v, float f) { + v.mX = f; + } + } +} diff --git a/apct-tests/perftests/core/src/android/libcore/regression/ProviderPerfTest.java b/apct-tests/perftests/core/src/android/libcore/regression/ProviderPerfTest.java new file mode 100644 index 0000000000000..f7bcf12c858eb --- /dev/null +++ b/apct-tests/perftests/core/src/android/libcore/regression/ProviderPerfTest.java @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.libcore.regression; + +import android.perftests.utils.BenchmarkState; +import android.perftests.utils.PerfStatusReporter; +import android.test.suitebuilder.annotation.LargeTest; + +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.security.Provider; +import java.security.Security; + +import javax.crypto.Cipher; + +@RunWith(AndroidJUnit4.class) +@LargeTest +public class ProviderPerfTest { + @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); + + @Test + public void timeStableProviders() throws Exception { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Cipher c = Cipher.getInstance("RSA"); + } + } + + @Test + public void timeWithNewProvider() throws Exception { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Security.addProvider(new MockProvider()); + try { + Cipher c = Cipher.getInstance("RSA"); + } finally { + Security.removeProvider("Mock"); + } + } + } + + private static class MockProvider extends Provider { + MockProvider() { + super("Mock", 1.0, "Mock me!"); + } + } +} diff --git a/apct-tests/perftests/core/src/android/libcore/regression/R.java b/apct-tests/perftests/core/src/android/libcore/regression/R.java new file mode 100644 index 0000000000000..d1641b32d06ec --- /dev/null +++ b/apct-tests/perftests/core/src/android/libcore/regression/R.java @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.libcore.regression; + +/** + * This file is a subset of the frameworks' R.java (resource definition) file + * with references to android specific annotations stripped out. + */ +public final class R { + private R() {} + + public final int mTextAppearanceLargePopupMenu = 0; + public static final int WEEK_NUMBER_COLOR = 0; +} diff --git a/apct-tests/perftests/core/src/android/libcore/regression/RandomPerfTest.java b/apct-tests/perftests/core/src/android/libcore/regression/RandomPerfTest.java new file mode 100644 index 0000000000000..d8bff4cc20d13 --- /dev/null +++ b/apct-tests/perftests/core/src/android/libcore/regression/RandomPerfTest.java @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.libcore.regression; + +import android.perftests.utils.BenchmarkState; +import android.perftests.utils.PerfStatusReporter; +import android.test.suitebuilder.annotation.LargeTest; + +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.security.SecureRandom; +import java.util.Random; + +@RunWith(AndroidJUnit4.class) +@LargeTest +public class RandomPerfTest { + @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); + + @Test + public void timeNewRandom() throws Exception { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Random rng = new Random(); + rng.nextInt(); + } + } + + @Test + public void timeReusedRandom() throws Exception { + Random rng = new Random(); + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + rng.nextInt(); + } + } + + @Test + public void timeReusedSecureRandom() throws Exception { + SecureRandom rng = new SecureRandom(); + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + rng.nextInt(); + } + } + + @Test + public void timeNewSecureRandom() throws Exception { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + SecureRandom rng = new SecureRandom(); + rng.nextInt(); + } + } +} diff --git a/apct-tests/perftests/core/src/android/libcore/regression/RealToStringPerfTest.java b/apct-tests/perftests/core/src/android/libcore/regression/RealToStringPerfTest.java new file mode 100644 index 0000000000000..2542df90896ff --- /dev/null +++ b/apct-tests/perftests/core/src/android/libcore/regression/RealToStringPerfTest.java @@ -0,0 +1,173 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.libcore.regression; + +import android.perftests.utils.BenchmarkState; +import android.perftests.utils.PerfStatusReporter; +import android.test.suitebuilder.annotation.LargeTest; + +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(AndroidJUnit4.class) +@LargeTest +public class RealToStringPerfTest { + @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); + + private static final float SMALL = -123.45f; + private static final float MEDIUM = -123.45e8f; + private static final float LARGE = -123.45e36f; + + @Test + public void timeFloat_toString_NaN() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Float.toString(Float.NaN); + } + } + + @Test + public void timeFloat_toString_NEGATIVE_INFINITY() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Float.toString(Float.NEGATIVE_INFINITY); + } + } + + @Test + public void timeFloat_toString_POSITIVE_INFINITY() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Float.toString(Float.POSITIVE_INFINITY); + } + } + + @Test + public void timeFloat_toString_zero() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Float.toString(0.0f); + } + } + + @Test + public void timeFloat_toString_minusZero() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Float.toString(-0.0f); + } + } + + @Test + public void timeFloat_toString_small() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Float.toString(SMALL); + } + } + + @Test + public void timeFloat_toString_medium() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Float.toString(MEDIUM); + } + } + + @Test + public void timeFloat_toString_large() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Float.toString(LARGE); + } + } + + @Test + public void timeStringBuilder_small() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + new StringBuilder().append(SMALL); + } + } + + @Test + public void timeStringBuilder_medium() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + new StringBuilder().append(MEDIUM); + } + } + + @Test + public void timeStringBuilder_large() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + new StringBuilder().append(LARGE); + } + } + + @Test + public void timeFormatter_small() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + String.format("%f", SMALL); + } + } + + @Test + public void timeFormatter_medium() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + String.format("%f", MEDIUM); + } + } + + @Test + public void timeFormatter_large() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + String.format("%f", LARGE); + } + } + + @Test + public void timeFormatter_dot2f_small() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + String.format("%.2f", SMALL); + } + } + + @Test + public void timeFormatter_dot2f_medium() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + String.format("%.2f", MEDIUM); + } + } + + @Test + public void timeFormatter_dot2f_large() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + String.format("%.2f", LARGE); + } + } +} diff --git a/apct-tests/perftests/core/src/android/libcore/regression/ReflectionPerfTest.java b/apct-tests/perftests/core/src/android/libcore/regression/ReflectionPerfTest.java new file mode 100644 index 0000000000000..b06662ccd9b5b --- /dev/null +++ b/apct-tests/perftests/core/src/android/libcore/regression/ReflectionPerfTest.java @@ -0,0 +1,296 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.libcore.regression; + +import android.perftests.utils.BenchmarkState; +import android.perftests.utils.PerfStatusReporter; +import android.test.suitebuilder.annotation.LargeTest; + +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +@RunWith(AndroidJUnit4.class) +@LargeTest +public class ReflectionPerfTest { + @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); + + @Test + public void timeObject_getClass() throws Exception { + C c = new C(); + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + c.getClass(); + } + } + + @Test + public void timeClass_getField() throws Exception { + Class klass = C.class; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + klass.getField("f"); + } + } + + @Test + public void timeClass_getDeclaredField() throws Exception { + Class klass = C.class; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + klass.getDeclaredField("f"); + } + } + + @Test + public void timeClass_getConstructor() throws Exception { + Class klass = C.class; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + klass.getConstructor(); + } + } + + @Test + public void timeClass_newInstance() throws Exception { + Class klass = C.class; + Constructor constructor = klass.getConstructor(); + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + constructor.newInstance(); + } + } + + @Test + public void timeClass_getMethod() throws Exception { + Class klass = C.class; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + klass.getMethod("m"); + } + } + + @Test + public void timeClass_getDeclaredMethod() throws Exception { + Class klass = C.class; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + klass.getDeclaredMethod("m"); + } + } + + @Test + public void timeField_setInt() throws Exception { + Class klass = C.class; + Field f = klass.getDeclaredField("f"); + C instance = new C(); + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + f.setInt(instance, 1); + } + } + + @Test + public void timeField_getInt() throws Exception { + Class klass = C.class; + Field f = klass.getDeclaredField("f"); + C instance = new C(); + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + f.getInt(instance); + } + } + + @Test + public void timeMethod_invokeV() throws Exception { + Class klass = C.class; + Method m = klass.getDeclaredMethod("m"); + C instance = new C(); + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + m.invoke(instance); + } + } + + @Test + public void timeMethod_invokeStaticV() throws Exception { + Class klass = C.class; + Method m = klass.getDeclaredMethod("sm"); + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + m.invoke(null); + } + } + + @Test + public void timeMethod_invokeI() throws Exception { + Class klass = C.class; + Method m = klass.getDeclaredMethod("setField", int.class); + C instance = new C(); + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + m.invoke(instance, 1); + } + } + + @Test + public void timeMethod_invokePreBoxedI() throws Exception { + Class klass = C.class; + Method m = klass.getDeclaredMethod("setField", int.class); + C instance = new C(); + Integer one = Integer.valueOf(1); + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + m.invoke(instance, one); + } + } + + @Test + public void timeMethod_invokeStaticI() throws Exception { + Class klass = C.class; + Method m = klass.getDeclaredMethod("setStaticField", int.class); + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + m.invoke(null, 1); + } + } + + @Test + public void timeMethod_invokeStaticPreBoxedI() throws Exception { + Class klass = C.class; + Method m = klass.getDeclaredMethod("setStaticField", int.class); + Integer one = Integer.valueOf(1); + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + m.invoke(null, one); + } + } + + @Test + public void timeRegularMethodInvocation() throws Exception { + C instance = new C(); + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + instance.setField(1); + } + } + + @Test + public void timeRegularConstructor() throws Exception { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + new C(); + } + } + + @Test + public void timeClass_classNewInstance() throws Exception { + Class klass = C.class; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + klass.newInstance(); + } + } + + @Test + public void timeClass_isInstance() throws Exception { + D d = new D(); + Class klass = IC.class; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + klass.isInstance(d); + } + } + + @Test + public void timeGetInstanceField() throws Exception { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + // TODO: Write a test script that generates both the classes we're + // reflecting on and the test case for each of its fields. + R.class.getField("mTextAppearanceLargePopupMenu"); + } + } + + @Test + public void timeGetStaticField() throws Exception { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + R.class.getField("WEEK_NUMBER_COLOR"); + } + } + + @Test + public void timeGetInterfaceStaticField() throws Exception { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + F.class.getField("SF"); + } + } + + @Test + public void timeGetSuperClassField() throws Exception { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + G.class.getField("f"); + } + } + + public static class C { + public static int sf = 0; + public int f = 0; + + public C() { + // A non-empty constructor so we don't get optimized away. + f = 1; + } + + public void m() {} + + public static void sm() {} + + public void setField(int value) { + f = value; + } + + public static void setStaticField(int value) { + sf = value; + } + } + + interface IA {} + + interface IB extends IA {} + + interface IC extends IB { + int SF = 0; + } + + class D implements IC {} + + class E extends D {} + + class F extends E implements IB {} + + class G extends C {} +}