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

Commit 7d34c946 authored by Jeff Sharkey's avatar Jeff Sharkey
Browse files

Benchmarks to measure CursorWindow overheads.

We have some ideas on how to improve the efficiency of CursorWindow
transport for smaller data sets, but we want to get some benchmarking
in place to better characterize our upcoming changes.

These tests move a small, medium, and large cursor across process
boundaries, exercising the typical ContentProvider.query() flow.

Bug: 169251528
Test: ./frameworks/base/libs/hwui/tests/scripts/prep_generic.sh little && atest CorePerfTests:android.database.CrossProcessCursorPerfTest
Change-Id: Ia9e6a3195324db93a02d224e8b5108d397e3bb41
parent f747948b
Loading
Loading
Loading
Loading
+11 −1
Original line number Diff line number Diff line
@@ -20,7 +20,17 @@
            <action android:name="com.android.perftests.core.PERFTEST" />
          </intent-filter>
        </activity>
        <service android:name="android.os.SomeService" android:exported="false" android:process=":some_service" />

        <service
            android:name="android.os.SomeService"
            android:exported="false"
            android:process=":some_service" />

        <provider
            android:name="android.os.SomeProvider"
            android:authorities="android.os.SomeProvider"
            android:exported="false"
            android:process=":some_provider" />

        <service
            android:name="android.view.autofill.MyAutofillService"
+89 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 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.database;

import static org.junit.Assert.assertEquals;

import android.content.ContentProviderClient;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.perftests.utils.BenchmarkState;
import android.perftests.utils.PerfStatusReporter;

import androidx.test.InstrumentationRegistry;
import androidx.test.filters.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 CrossProcessCursorPerfTest {
    @Rule
    public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();

    /**
     * Measure transporting a small {@link Cursor}, roughly 1KB in size.
     */
    @Test
    public void timeSmall() throws Exception {
        time(1);
    }

    /**
     * Measure transporting a small {@link Cursor}, roughly 54KB in size.
     */
    @Test
    public void timeMedium() throws Exception {
        time(100);
    }

    /**
     * Measure transporting a small {@link Cursor}, roughly 5.4MB in size.
     */
    @Test
    public void timeLarge() throws Exception {
        time(10_000);
    }

    private static final Uri TEST_URI = Uri.parse("content://android.os.SomeProvider/");

    private void time(int count) throws Exception {
        try (ContentProviderClient client = InstrumentationRegistry.getTargetContext()
                .getContentResolver().acquireContentProviderClient(TEST_URI)) {
            // Configure remote side once with data size to return
            final ContentValues values = new ContentValues();
            values.put(Intent.EXTRA_INDEX, count);
            client.update(TEST_URI, values, null);

            // Repeatedly query that data until we reach convergence
            final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
            while (state.keepRunning()) {
                try (Cursor c = client.query(TEST_URI, null, null, null)) {
                    // Actually walk the returned values to ensure we pull all
                    // data from the remote side
                    while (c.moveToNext()) {
                        assertEquals(c.getPosition(), c.getInt(0));
                    }
                }
            }
        }
    }
}
+73 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 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.os;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;

import java.util.Arrays;

public class SomeProvider extends ContentProvider {
    private Cursor mCursor;

    @Override
    public boolean onCreate() {
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
            String sortOrder) {
        return mCursor;
    }

    @Override
    public String getType(Uri uri) {
        throw new UnsupportedOperationException();
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        throw new UnsupportedOperationException();
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        throw new UnsupportedOperationException();
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        final char[] valueRaw = new char[512];
        Arrays.fill(valueRaw, '!');
        final String value = new String(valueRaw);

        final int count = values.getAsInteger(Intent.EXTRA_INDEX);
        final MatrixCursor cursor = new MatrixCursor(new String[] { "_id", "value" });
        for (int i = 0; i < count; i++) {
            MatrixCursor.RowBuilder row = cursor.newRow();
            row.add(0, i);
            row.add(1, value);
        }
        mCursor = cursor;
        return 1;
    }
}