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

Commit 844e9f47 authored by Miguel's avatar Miguel Committed by Miguel Aranda
Browse files

More libcore benchmarks

Test: atest {TestName}
Change-Id: I1ff5876dc68d316ec95412513a4b7ceeff82d638
parent 7107a239
Loading
Loading
Loading
Loading
+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.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");
        }
    }
}
+107 −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;

@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;
        }
    }
}
+134 −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;

@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;
        }
    }
}
+98 −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;

@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;
        }
    }
}
+173 −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 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]);
            }
        }
    }
}
Loading