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 android.app.Activity; 20 import android.content.ContentValues; 21 import android.content.Context; 22 import android.database.sqlite.SQLiteDatabase; 23 import android.os.Bundle; 24 import android.support.test.InstrumentationRegistry; 25 import android.support.test.filters.LargeTest; 26 import android.support.test.runner.AndroidJUnit4; 27 import android.util.ArrayMap; 28 import android.util.Log; 29 30 import com.android.internal.util.Preconditions; 31 32 import org.junit.After; 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 37 import java.io.File; 38 import java.io.IOException; 39 import java.nio.file.Files; 40 import java.util.List; 41 import java.util.Map; 42 43 import static org.junit.Assert.assertEquals; 44 45 /** 46 * Performance tests for measuring amount of data written during typical DB operations 47 * 48 * <p>To run: bit CorePerfTests:android.database.SQLiteDatabaseIoPerfTest 49 */ 50 @RunWith(AndroidJUnit4.class) 51 @LargeTest 52 public class SQLiteDatabaseIoPerfTest { 53 private static final String TAG = "SQLiteDatabaseIoPerfTest"; 54 private static final String DB_NAME = "db_io_perftest"; 55 private static final int DEFAULT_DATASET_SIZE = 500; 56 57 private Long mWriteBytes; 58 59 private SQLiteDatabase mDatabase; 60 private Context mContext; 61 62 @Before 63 public void setUp() { 64 mContext = InstrumentationRegistry.getTargetContext(); 65 mContext.deleteDatabase(DB_NAME); 66 mDatabase = mContext.openOrCreateDatabase(DB_NAME, Context.MODE_PRIVATE, null); 67 mDatabase.execSQL("CREATE TABLE T1 " 68 + "(_ID INTEGER PRIMARY KEY, COL_A INTEGER, COL_B VARCHAR(100), COL_C REAL)"); 69 } 70 71 @After 72 public void tearDown() { 73 mDatabase.close(); 74 mContext.deleteDatabase(DB_NAME); 75 } 76 77 @Test 78 public void testDatabaseModifications() { 79 startMeasuringWrites(); 80 ContentValues cv = new ContentValues(); 81 String[] whereArg = new String[1]; 82 for (int i = 0; i < DEFAULT_DATASET_SIZE; i++) { 83 cv.put("_ID", i); 84 cv.put("COL_A", i); 85 cv.put("COL_B", "NewValue"); 86 cv.put("COL_C", 1.0); 87 assertEquals(i, mDatabase.insert("T1", null, cv)); 88 } 89 cv = new ContentValues(); 90 for (int i = 0; i < DEFAULT_DATASET_SIZE; i++) { 91 cv.put("COL_B", "UpdatedValue"); 92 cv.put("COL_C", 1.1); 93 whereArg[0] = String.valueOf(i); 94 assertEquals(1, mDatabase.update("T1", cv, "_ID=?", whereArg)); 95 } 96 for (int i = 0; i < DEFAULT_DATASET_SIZE; i++) { 97 whereArg[0] = String.valueOf(i); 98 assertEquals(1, mDatabase.delete("T1", "_ID=?", whereArg)); 99 } 100 // Make sure all changes are written to disk 101 mDatabase.close(); 102 long bytes = endMeasuringWrites(); 103 sendResults("testDatabaseModifications" , bytes); 104 } 105 106 @Test 107 public void testInsertsWithTransactions() { 108 startMeasuringWrites(); 109 final int txSize = 10; 110 ContentValues cv = new ContentValues(); 111 for (int i = 0; i < DEFAULT_DATASET_SIZE * 5; i++) { 112 if (i % txSize == 0) { 113 mDatabase.beginTransaction(); 114 } 115 if (i % txSize == txSize-1) { 116 mDatabase.setTransactionSuccessful(); 117 mDatabase.endTransaction(); 118 119 } 120 cv.put("_ID", i); 121 cv.put("COL_A", i); 122 cv.put("COL_B", "NewValue"); 123 cv.put("COL_C", 1.0); 124 assertEquals(i, mDatabase.insert("T1", null, cv)); 125 } 126 // Make sure all changes are written to disk 127 mDatabase.close(); 128 long bytes = endMeasuringWrites(); 129 sendResults("testInsertsWithTransactions" , bytes); 130 } 131 132 private void startMeasuringWrites() { 133 Preconditions.checkState(mWriteBytes == null, "Measurement already started"); 134 mWriteBytes = getIoStats().get("write_bytes"); 135 } 136 137 private long endMeasuringWrites() { 138 Preconditions.checkState(mWriteBytes != null, "Measurement wasn't started"); 139 Long newWriteBytes = getIoStats().get("write_bytes"); 140 return newWriteBytes - mWriteBytes; 141 } 142 143 private void sendResults(String testName, long writeBytes) { 144 Log.i(TAG, testName + " write_bytes: " + writeBytes); 145 Bundle status = new Bundle(); 146 status.putLong("write_bytes", writeBytes); 147 InstrumentationRegistry.getInstrumentation().sendStatus(Activity.RESULT_OK, status); 148 } 149 150 private static Map<String, Long> getIoStats() { 151 String ioStat = "/proc/self/io"; 152 Map<String, Long> results = new ArrayMap<>(); 153 try { 154 List<String> lines = Files.readAllLines(new File(ioStat).toPath()); 155 for (String line : lines) { 156 line = line.trim(); 157 String[] split = line.split(":"); 158 if (split.length == 2) { 159 try { 160 String key = split[0].trim(); 161 Long value = Long.valueOf(split[1].trim()); 162 results.put(key, value); 163 } catch (NumberFormatException e) { 164 Log.e(TAG, "Cannot parse number from " + line); 165 } 166 } else if (line.isEmpty()) { 167 Log.e(TAG, "Cannot parse line " + line); 168 } 169 } 170 } catch (IOException e) { 171 Log.e(TAG, "Can't read: " + ioStat, e); 172 } 173 return results; 174 } 175 176 } 177