Home | History | Annotate | Download | only in database
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.database;
     18 
     19 import static org.junit.Assert.assertTrue;
     20 
     21 import android.content.Context;
     22 import android.database.sqlite.SQLiteCursor;
     23 import android.database.sqlite.SQLiteDatabase;
     24 import android.perftests.utils.BenchmarkState;
     25 import android.perftests.utils.PerfStatusReporter;
     26 import android.support.test.InstrumentationRegistry;
     27 import android.support.test.filters.LargeTest;
     28 import android.support.test.runner.AndroidJUnit4;
     29 
     30 import org.junit.AfterClass;
     31 import org.junit.BeforeClass;
     32 import org.junit.Rule;
     33 import org.junit.Test;
     34 import org.junit.runner.RunWith;
     35 
     36 @RunWith(AndroidJUnit4.class)
     37 @LargeTest
     38 public class CursorWindowPerfTest {
     39     @Rule
     40     public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
     41 
     42     private static Context getContext() {
     43         return InstrumentationRegistry.getTargetContext();
     44     }
     45 
     46     private static final String DB_NAME = CursorWindowPerfTest.class.toString();
     47 
     48     private static SQLiteDatabase sDatabase;
     49 
     50     @BeforeClass
     51     public static void setup() {
     52         getContext().deleteDatabase(DB_NAME);
     53         sDatabase = getContext().openOrCreateDatabase(DB_NAME, Context.MODE_PRIVATE, null);
     54 
     55         for (TableHelper helper : TableHelper.TABLE_HELPERS) {
     56             sDatabase.execSQL(helper.createSql());
     57             final String insert = helper.insertSql();
     58 
     59             // this test only needs 1 row
     60             sDatabase.execSQL(insert, helper.createItem(0));
     61         }
     62 
     63     }
     64 
     65     @AfterClass
     66     public static void teardown() {
     67         getContext().deleteDatabase(DB_NAME);
     68     }
     69 
     70     @Test
     71     public void loadInt() {
     72         loadRowFromCursorWindow(TableHelper.INT_1, false);
     73     }
     74 
     75     @Test
     76     public void loadInt_doubleRef() {
     77         loadRowFromCursorWindow(TableHelper.INT_1, true);
     78     }
     79 
     80     @Test
     81     public void load10Ints() {
     82         loadRowFromCursorWindow(TableHelper.INT_10, false);
     83     }
     84 
     85     @Test
     86     public void loadUser() {
     87         loadRowFromCursorWindow(TableHelper.USER, false);
     88     }
     89 
     90     private void loadRowFromCursorWindow(TableHelper helper, boolean doubleRef) {
     91         try (Cursor cursor = sDatabase.rawQuery(helper.readSql(), new String[0])) {
     92             TableHelper.CursorReader reader = helper.createReader(cursor);
     93 
     94             SQLiteCursor sqLiteCursor = (SQLiteCursor) cursor;
     95 
     96             sqLiteCursor.getCount(); // load one window
     97             CursorWindow window = sqLiteCursor.getWindow();
     98             assertTrue("must have enough rows", window.getNumRows() >= 1);
     99             int start = window.getStartPosition();
    100 
    101             BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
    102 
    103             if (!doubleRef) {
    104                 // normal access
    105                 while (state.keepRunning()) {
    106                     cursor.moveToPosition(start);
    107                     reader.read();
    108                 }
    109             } else {
    110                 // add an extra window acquire/release to measure overhead
    111                 while (state.keepRunning()) {
    112                     cursor.moveToPosition(start);
    113                     try {
    114                         window.acquireReference();
    115                         reader.read();
    116                     } finally {
    117                         window.releaseReference();
    118                     }
    119                 }
    120             }
    121         }
    122     }
    123 }
    124