public class SqliteTest extends ActivityInstrumentationTestCase2<HelloAndroidActivity> {
private static final String TAG = "SqliteTest";
private DBHelper dbHelper;
public SqliteTest() {
super(HelloAndroidActivity.class);
}
public SqliteTest(Class<HelloAndroidActivity> activityClass) {
super(HelloAndroidActivity.class);
}
@Before
public void setUp() throws Exception {
super.setUp();
this.getActivity().deleteDatabase("test.db");
dbHelper = DBHelper.getInsrance(getActivity());
}
@After
public void tearDown() throws Exception {
dbHelper.close();
}
private int[] sizes = new int[] { 1, 5, 10, 50, 100, 500, 700, 1000, 2500, 5000, 7500, 10000, 20000 };
@Test
public void testIndexPerformace(){
doQueryTask(dbHelper.getWritableDatabase(), " ", 50000, 1000);
testNoIndexPerformace();
testUniqueIndexPerformace();
testNornalIndexPerformace();
}
@Test
public void testNoIndexPerformace() {
SQLiteDatabase db = dbHelper.getWritableDatabase();
for (int size : sizes) {
doQueryTask(db, "没有索引", size, 1000);
}
}
@Test
public void testUniqueIndexPerformace() {
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.execSQL("create unique index i_users_id on users(_id)");
for (int size : sizes) {
doQueryTask(db, "唯一索引", size, 1000);
}
}
@Test
public void testNornalIndexPerformace() {
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.execSQL("drop index i_users_id");
db.execSQL("create index i_users_id on users(_id)");
for (int size : sizes) {
doQueryTask(db, "普通索引", size, 1000);
}
}
private void insertData(SQLiteDatabase db, int size) {
db.beginTransaction();
try {
db.execSQL("delete from users");
for (int i = 0; i < size; i++) {
db.execSQL("insert into users(name,gender,age,phoneNumber,address) values( 'name', 1, 1, 'tel', 'address')");
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
private void doQueryTask(SQLiteDatabase db, String tag, int size, int times) {
insertData(db, size);
String sql1 = "select * from users where _id = " + (1 + size >> 1);
String sql2 = "select count(_id) from users where _id > " + (size - 1);
long start = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
Cursor c = db.rawQuery(sql1, null);
c.moveToNext();
c.getInt(0);
c.close();
c = db.rawQuery(sql2, null);
c.moveToNext();
c.getInt(0);
c.close();
}
Log.i(TAG, tag + " size:" + size + " , time:" + (System.currentTimeMillis() - start));
}
}
I/SqliteTest(32503): 没有索引 size:1 , time:2345
I/SqliteTest(32503): 没有索引 size:5 , time:2395
I/SqliteTest(32503): 没有索引 size:10 , time:2432
I/SqliteTest(32503): 没有索引 size:50 , time:2395
I/SqliteTest(32503): 没有索引 size:100 , time:2363
I/SqliteTest(32503): 没有索引 size:500 , time:2390
I/SqliteTest(32503): 没有索引 size:700 , time:2349
I/SqliteTest(32503): 没有索引 size:1000 , time:2398
I/SqliteTest(32503): 没有索引 size:2500 , time:2348
I/SqliteTest(32503): 没有索引 size:5000 , time:2343
I/SqliteTest(32503): 没有索引 size:7500 , time:2415
I/SqliteTest(32503): 没有索引 size:10000 , time:2402
I/SqliteTest(32503): 没有索引 size:20000 , time:2420
I/SqliteTest(32503): 唯一索引 size:1 , time:2346
I/SqliteTest(32503): 唯一索引 size:5 , time:2456
I/SqliteTest(32503): 唯一索引 size:10 , time:2393
I/SqliteTest(32503): 唯一索引 size:50 , time:2488
I/SqliteTest(32503): 唯一索引 size:100 , time:2376
I/SqliteTest(32503): 唯一索引 size:500 , time:2445
I/SqliteTest(32503): 唯一索引 size:700 , time:2402
I/SqliteTest(32503): 唯一索引 size:1000 , time:2340
I/SqliteTest(32503): 唯一索引 size:2500 , time:2715
I/SqliteTest(32503): 唯一索引 size:5000 , time:2406
I/SqliteTest(32503): 唯一索引 size:7500 , time:2380
I/SqliteTest(32503): 唯一索引 size:10000 , time:2357
I/SqliteTest(32503): 唯一索引 size:20000 , time:2391
I/SqliteTest(32503): 普通索引 size:1 , time:2376
I/SqliteTest(32503): 普通索引 size:5 , time:2390
I/SqliteTest(32503): 普通索引 size:10 , time:2368
I/SqliteTest(32503): 普通索引 size:50 , time:2378
I/SqliteTest(32503): 普通索引 size:100 , time:2403
I/SqliteTest(32503): 普通索引 size:500 , time:2425
I/SqliteTest(32503): 普通索引 size:700 , time:2378
I/SqliteTest(32503): 普通索引 size:1000 , time:2394
I/SqliteTest(32503): 普通索引 size:2500 , time:2404
I/SqliteTest(32503): 普通索引 size:5000 , time:2412
I/SqliteTest(32503): 普通索引 size:7500 , time:2405
I/SqliteTest(32503): 普通索引 size:10000 , time:2406
I/SqliteTest(32503): 普通索引 size:20000 , time:2376