From 1935f73e5b21d9359456344e911f51b9042a406f Mon Sep 17 00:00:00 2001 From: Miguel Date: Tue, 15 Feb 2022 17:44:04 +0000 Subject: [PATCH] Add first set of libcore benchmarks to the System Health CB framework. These will later be referenced by a google3 gcl file as part of a new libcore test set. Doc: go/libcore-benchmarks-doc Test: N/A Bug: 210606618 Change-Id: I4f38b797f8f4978d00e92da07c3db8df366a6897 --- .../src/android/libcore/AdditionPerfTest.java | 115 ++++++++++++++++++ .../android/libcore/ArrayCopyPerfTest.java | 77 ++++++++++++ .../libcore/ArrayIterationPerfTest.java | 80 ++++++++++++ .../libcore/ArrayListIterationPerfTest.java | 70 +++++++++++ 4 files changed, 342 insertions(+) create mode 100644 apct-tests/perftests/core/src/android/libcore/AdditionPerfTest.java create mode 100644 apct-tests/perftests/core/src/android/libcore/ArrayCopyPerfTest.java create mode 100644 apct-tests/perftests/core/src/android/libcore/ArrayIterationPerfTest.java create mode 100644 apct-tests/perftests/core/src/android/libcore/ArrayListIterationPerfTest.java diff --git a/apct-tests/perftests/core/src/android/libcore/AdditionPerfTest.java b/apct-tests/perftests/core/src/android/libcore/AdditionPerfTest.java new file mode 100644 index 0000000000000..ea3d172b2e5f0 --- /dev/null +++ b/apct-tests/perftests/core/src/android/libcore/AdditionPerfTest.java @@ -0,0 +1,115 @@ +/* + * Copyright (C) 2016 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; + +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; + +/** + * What do various kinds of addition cost? + */ +@RunWith(AndroidJUnit4.class) +@LargeTest +public class AdditionPerfTest { + + @Rule + public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); + + @Test + public int timeAddConstantToLocalInt() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + int result = 0; + while (state.keepRunning()) { + result += 123; + } + return result; + } + @Test + public int timeAddTwoLocalInts() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + int result = 0; + int constant = 123; + while (state.keepRunning()) { + result += constant; + } + return result; + } + @Test + public long timeAddConstantToLocalLong() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + long result = 0; + while (state.keepRunning()) { + result += 123L; + } + return result; + } + @Test + public long timeAddTwoLocalLongs() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + long result = 0; + long constant = 123L; + while (state.keepRunning()) { + result += constant; + } + return result; + } + @Test + public float timeAddConstantToLocalFloat() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + float result = 0.0f; + while (state.keepRunning()) { + result += 123.0f; + } + return result; + } + @Test + public float timeAddTwoLocalFloats() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + float result = 0.0f; + float constant = 123.0f; + while (state.keepRunning()) { + result += constant; + } + return result; + } + @Test + public double timeAddConstantToLocalDouble() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + double result = 0.0; + while (state.keepRunning()) { + result += 123.0; + } + return result; + } + @Test + public double timeAddTwoLocalDoubles() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + double result = 0.0; + double constant = 123.0; + while (state.keepRunning()) { + result += constant; + } + return result; + } +} diff --git a/apct-tests/perftests/core/src/android/libcore/ArrayCopyPerfTest.java b/apct-tests/perftests/core/src/android/libcore/ArrayCopyPerfTest.java new file mode 100644 index 0000000000000..97ab6c7cd6b4a --- /dev/null +++ b/apct-tests/perftests/core/src/android/libcore/ArrayCopyPerfTest.java @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2016 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; + +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.util.Arrays; + +@RunWith(AndroidJUnit4.class) +@LargeTest +public class ArrayCopyPerfTest { + + @Rule + public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); + + @Test + public void timeManualArrayCopy() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + char[] src = new char[8192]; + while (state.keepRunning()) { + char[] dst = new char[8192]; + for (int i = 0; i < 8192; ++i) { + dst[i] = src[i]; + } + } + } + + @Test + public void time_System_arrayCopy() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + char[] src = new char[8192]; + while (state.keepRunning()) { + char[] dst = new char[8192]; + System.arraycopy(src, 0, dst, 0, 8192); + } + } + + @Test + public void time_Arrays_copyOf() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + char[] src = new char[8192]; + while (state.keepRunning()) { + char[] dst = Arrays.copyOf(src, 8192); + } + } + + @Test + public void time_Arrays_copyOfRange() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + char[] src = new char[8192]; + while (state.keepRunning()) { + char[] dst = Arrays.copyOfRange(src, 0, 8192); + } + } +} diff --git a/apct-tests/perftests/core/src/android/libcore/ArrayIterationPerfTest.java b/apct-tests/perftests/core/src/android/libcore/ArrayIterationPerfTest.java new file mode 100644 index 0000000000000..bb452d394d473 --- /dev/null +++ b/apct-tests/perftests/core/src/android/libcore/ArrayIterationPerfTest.java @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2016 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; + +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; + +/** + * How do various ways of iterating through an array compare? + */ +@RunWith(AndroidJUnit4.class) +@LargeTest +public class ArrayIterationPerfTest { + + public class Foo { + int mSplat; + } + + @Rule + public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); + + Foo[] mArray = new Foo[27]; + { + for (int i = 0; i < mArray.length; ++i) mArray[i] = new Foo(); + } + @Test + public void timeArrayIteration() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + int sum = 0; + for (int i = 0; i < mArray.length; i++) { + sum += mArray[i].mSplat; + } + } + } + @Test + public void timeArrayIterationCached() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + int sum = 0; + Foo[] localArray = mArray; + int len = localArray.length; + + for (int i = 0; i < len; i++) { + sum += localArray[i].mSplat; + } + } + } + @Test + public void timeArrayIterationForEach() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + int sum = 0; + for (Foo a: mArray) { + sum += a.mSplat; + } + } + } +} diff --git a/apct-tests/perftests/core/src/android/libcore/ArrayListIterationPerfTest.java b/apct-tests/perftests/core/src/android/libcore/ArrayListIterationPerfTest.java new file mode 100644 index 0000000000000..ff6d46f6db7fb --- /dev/null +++ b/apct-tests/perftests/core/src/android/libcore/ArrayListIterationPerfTest.java @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2016 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; + +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.util.ArrayList; + +/** + * Is a hand-coded counted loop through an ArrayList cheaper than enhanced for? + */ +@RunWith(AndroidJUnit4.class) +@LargeTest +public class ArrayListIterationPerfTest { + + public class Foo { + int mSplat; + } + @Rule + public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); + + ArrayList mList = new ArrayList(); + { + for (int i = 0; i < 27; ++i) mList.add(new Foo()); + } + @Test + public void timeArrayListIterationIndexed() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + int sum = 0; + ArrayList list = mList; + int len = list.size(); + for (int i = 0; i < len; ++i) { + sum += list.get(i).mSplat; + } + } + } + @Test + public void timeArrayListIterationForEach() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + int sum = 0; + for (Foo a : mList) { + sum += a.mSplat; + } + } + } +}