Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 14fff66f authored by Miguel Aranda's avatar Miguel Aranda Committed by Gerrit Code Review
Browse files

Merge "Even more libcore benchmark tests."

parents c45eb287 2030cb40
Loading
Loading
Loading
Loading
+331 −0
Original line number Diff line number Diff line
/*
 * 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.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

@RunWith(AndroidJUnit4.class)
@LargeTest
public class AnnotatedElementPerfTest {
    @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();

    private Class<?> mType;
    private Field mField;
    private Method mMethod;

    @Before
    public void setUp() throws Exception {
        mType = Type.class;
        mField = Type.class.getField("field");
        mMethod = Type.class.getMethod("method", String.class);
    }

    // get annotations by member type and method

    @Test
    public void timeGetTypeAnnotations() {
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            mType.getAnnotations();
        }
    }

    @Test
    public void timeGetFieldAnnotations() {
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            mField.getAnnotations();
        }
    }

    @Test
    public void timeGetMethodAnnotations() {
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            mMethod.getAnnotations();
        }
    }

    @Test
    public void timeGetParameterAnnotations() {
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            mMethod.getParameterAnnotations();
        }
    }

    @Test
    public void timeGetTypeAnnotation() {
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            mType.getAnnotation(Marker.class);
        }
    }

    @Test
    public void timeGetFieldAnnotation() {
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            mField.getAnnotation(Marker.class);
        }
    }

    @Test
    public void timeGetMethodAnnotation() {
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            mMethod.getAnnotation(Marker.class);
        }
    }

    @Test
    public void timeIsTypeAnnotationPresent() {
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            mType.isAnnotationPresent(Marker.class);
        }
    }

    @Test
    public void timeIsFieldAnnotationPresent() {
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            mField.isAnnotationPresent(Marker.class);
        }
    }

    @Test
    public void timeIsMethodAnnotationPresent() {
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            mMethod.isAnnotationPresent(Marker.class);
        }
    }

    // get annotations by result size

    @Test
    public void timeGetAllReturnsLargeAnnotation() {
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            HasLargeAnnotation.class.getAnnotations();
        }
    }

    @Test
    public void timeGetAllReturnsSmallAnnotation() {
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            HasSmallAnnotation.class.getAnnotations();
        }
    }

    @Test
    public void timeGetAllReturnsMarkerAnnotation() {
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            HasMarkerAnnotation.class.getAnnotations();
        }
    }

    @Test
    public void timeGetAllReturnsNoAnnotation() {
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            HasNoAnnotations.class.getAnnotations();
        }
    }

    @Test
    public void timeGetAllReturnsThreeAnnotations() {
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            HasThreeAnnotations.class.getAnnotations();
        }
    }

    // get annotations with inheritance

    @Test
    public void timeGetAnnotationsOnSubclass() {
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            ExtendsHasThreeAnnotations.class.getAnnotations();
        }
    }

    @Test
    public void timeGetDeclaredAnnotationsOnSubclass() {
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            ExtendsHasThreeAnnotations.class.getDeclaredAnnotations();
        }
    }

    // get annotations with enclosing / inner classes

    @Test
    public void timeGetDeclaredClasses() {
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            AnnotatedElementPerfTest.class.getDeclaredClasses();
        }
    }

    @Test
    public void timeGetDeclaringClass() {
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            HasSmallAnnotation.class.getDeclaringClass();
        }
    }

    @Test
    public void timeGetEnclosingClass() {
        Object anonymousClass = new Object() {};
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            anonymousClass.getClass().getEnclosingClass();
        }
    }

    @Test
    public void timeGetEnclosingConstructor() {
        Object anonymousClass = new Object() {};
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            anonymousClass.getClass().getEnclosingConstructor();
        }
    }

    @Test
    public void timeGetEnclosingMethod() {
        Object anonymousClass = new Object() {};
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            anonymousClass.getClass().getEnclosingMethod();
        }
    }

    @Test
    public void timeGetModifiers() {
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            HasSmallAnnotation.class.getModifiers();
        }
    }

    @Test
    public void timeGetSimpleName() {
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            HasSmallAnnotation.class.getSimpleName();
        }
    }

    @Test
    public void timeIsAnonymousClass() {
        Object anonymousClass = new Object() {};
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            anonymousClass.getClass().isAnonymousClass();
        }
    }

    @Test
    public void timeIsLocalClass() {
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            HasSmallAnnotation.class.isLocalClass();
        }
    }

    // the annotated elements

    @Marker
    public class Type {
        @Marker public String field;

        @Marker
        public void method(@Marker String parameter) {}
    }

    @Large(
            a = "on class",
            b = {"A", "B", "C"},
            c = @Small(e = "E1", f = 1695938256, g = 7264081114510713000L),
            d = {@Small(e = "E2", f = 1695938256, g = 7264081114510713000L)})
    public class HasLargeAnnotation {}

    @Small(e = "E1", f = 1695938256, g = 7264081114510713000L)
    public class HasSmallAnnotation {}

    @Marker
    public class HasMarkerAnnotation {}

    public class HasNoAnnotations {}

    @Large(
            a = "on class",
            b = {"A", "B", "C"},
            c = @Small(e = "E1", f = 1695938256, g = 7264081114510713000L),
            d = {@Small(e = "E2", f = 1695938256, g = 7264081114510713000L)})
    @Small(e = "E1", f = 1695938256, g = 7264081114510713000L)
    @Marker
    public class HasThreeAnnotations {}

    public class ExtendsHasThreeAnnotations extends HasThreeAnnotations {}

    // the annotations

    @Retention(RetentionPolicy.RUNTIME)
    public @interface Marker {}

    @Retention(RetentionPolicy.RUNTIME)
    public @interface Large {
        String a() default "";

        String[] b() default {};

        Small c() default @Small;

        Small[] d() default {};
    }

    @Retention(RetentionPolicy.RUNTIME)
    public @interface Small {
        String e() default "";

        int f() default 0;

        long g() default 0L;
    }
}
+128 −0
Original line number Diff line number Diff line
/*
 * 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.math.BigDecimal;
import java.text.AttributedCharacterIterator;
import java.text.Bidi;
import java.text.DecimalFormat;

@RunWith(AndroidJUnit4.class)
@LargeTest
public class BidiPerfTest {
    @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();

    private static final AttributedCharacterIterator CHAR_ITER =
            DecimalFormat.getInstance().formatToCharacterIterator(new BigDecimal(Math.PI));

    @Test
    public void time_createBidiFromIter() {
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            Bidi bidi = new Bidi(CHAR_ITER);
        }
    }

    @Test
    public void time_createBidiFromCharArray() {
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            Bidi bd =
                    new Bidi(
                            new char[] {'s', 's', 's'},
                            0,
                            new byte[] {(byte) 1, (byte) 2, (byte) 3},
                            0,
                            3,
                            Bidi.DIRECTION_RIGHT_TO_LEFT);
        }
    }

    @Test
    public void time_createBidiFromString() {
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            Bidi bidi = new Bidi("Hello", Bidi.DIRECTION_LEFT_TO_RIGHT);
        }
    }

    @Test
    public void time_reorderVisually() {
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            Bidi.reorderVisually(
                    new byte[] {2, 1, 3, 0, 4}, 0, new String[] {"H", "e", "l", "l", "o"}, 0, 5);
        }
    }

    @Test
    public void time_hebrewBidi() {
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            Bidi bd =
                    new Bidi(
                            new char[] {'\u05D0', '\u05D0', '\u05D0'},
                            0,
                            new byte[] {(byte) -1, (byte) -2, (byte) -3},
                            0,
                            3,
                            Bidi.DIRECTION_DEFAULT_RIGHT_TO_LEFT);
            bd =
                    new Bidi(
                            new char[] {'\u05D0', '\u05D0', '\u05D0'},
                            0,
                            new byte[] {(byte) -1, (byte) -2, (byte) -3},
                            0,
                            3,
                            Bidi.DIRECTION_LEFT_TO_RIGHT);
        }
    }

    @Test
    public void time_complicatedOverrideBidi() {
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            Bidi bd =
                    new Bidi(
                            "a\u05D0a\"a\u05D0\"\u05D0a".toCharArray(),
                            0,
                            new byte[] {0, 0, 0, -3, -3, 2, 2, 0, 3},
                            0,
                            9,
                            Bidi.DIRECTION_RIGHT_TO_LEFT);
        }
    }

    @Test
    public void time_requiresBidi() {
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            Bidi.requiresBidi("\u05D0".toCharArray(), 1, 1); // false.
            Bidi.requiresBidi("\u05D0".toCharArray(), 0, 1); // true.
        }
    }
}
+69 −0
Original line number Diff line number Diff line
/*
 * 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.math.BigInteger;
import java.util.Random;

@RunWith(AndroidJUnit4.class)
@LargeTest
public class BigIntegerPerfTest {
    @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();

    @Test
    public void timeRandomDivision() throws Exception {
        Random r = new Random();
        BigInteger x = new BigInteger(1024, r);
        BigInteger y = new BigInteger(1024, r);
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            x.divide(y);
        }
    }

    @Test
    public void timeRandomGcd() throws Exception {
        Random r = new Random();
        BigInteger x = new BigInteger(1024, r);
        BigInteger y = new BigInteger(1024, r);
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            x.gcd(y);
        }
    }

    @Test
    public void timeRandomMultiplication() throws Exception {
        Random r = new Random();
        BigInteger x = new BigInteger(1024, r);
        BigInteger y = new BigInteger(1024, r);
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            x.multiply(y);
        }
    }
}
+115 −0
Original line number Diff line number Diff line
/*
 * 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.Arrays;
import java.util.BitSet;
import java.util.Collection;

@RunWith(Parameterized.class)
@LargeTest
public class BitSetPerfTest {
    @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();

    @Parameters(name = "mSize={0}")
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][] {{1000}, {10000}});
    }

    @Parameterized.Parameter(0)
    public int mSize;

    private BitSet mBitSet;

    @Before
    public void setUp() throws Exception {
        mBitSet = new BitSet(mSize);
    }

    @Test
    public void timeIsEmptyTrue() {
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            if (!mBitSet.isEmpty()) throw new RuntimeException();
        }
    }

    @Test
    public void timeIsEmptyFalse() {
        mBitSet.set(mBitSet.size() - 1);
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            if (mBitSet.isEmpty()) throw new RuntimeException();
        }
    }

    @Test
    public void timeGet() {
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        int i = 1;
        while (state.keepRunning()) {
            mBitSet.get(++i % mSize);
        }
    }

    @Test
    public void timeClear() {
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        int i = 1;
        while (state.keepRunning()) {
            mBitSet.clear(++i % mSize);
        }
    }

    @Test
    public void timeSet() {
        int i = 1;
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            mBitSet.set(++i % mSize);
        }
    }

    @Test
    public void timeSetOn() {
        int i = 1;
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            mBitSet.set(++i % mSize, true);
        }
    }

    @Test
    public void timeSetOff() {
        int i = 1;
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        while (state.keepRunning()) {
            mBitSet.set(++i % mSize, false);
        }
    }
}
+193 −0

File added.

Preview size limit exceeded, changes collapsed.

Loading