gecko/mobile/android/base/tests/testBrowserProviderPerf.java.in

262 lines
9.4 KiB
Java

#filter substitution
package @ANDROID_PACKAGE_NAME@.tests;
import @ANDROID_PACKAGE_NAME@.*;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.SystemClock;
import android.util.Log;
import java.lang.reflect.Method;
import java.util.UUID;
import java.util.Random;
/*
* This test is meant to exercise the performance of Fennec's
* history and bookmarks content provider.
*/
public class testBrowserProviderPerf extends ContentProviderTest {
private final int NUMBER_OF_BASIC_HISTORY_URLS = 10000;
private final int NUMBER_OF_BASIC_BOOKMARK_URLS = 500;
private final int NUMBER_OF_COMBINED_URLS = 500;
private final int NUMBER_OF_KNOWN_URLS = 200;
private final int BATCH_SIZE = 500;
// Include spaces in prefix to test performance querying with
// multiple constraint words
private final String KNOWN_PREFIX = "my mozilla test ";
private Method mFilterMethod;
private Random mGenerator;
private final String MOBILE_FOLDER_GUID = "mobile";
private long mMobileFolderId;
private Uri mBookmarksUri;
private Uri mHistoryUri;
private String mBookmarksIdCol;
private String mBookmarksTitleCol;
private String mBookmarksUrlCol;
private String mBookmarksParentCol;
private String mBookmarksTypeCol;
private String mBookmarksPositionCol;
private String mBookmarksTagsCol;
private String mBookmarksDescriptionCol;
private String mBookmarksKeywordCol;
private String mBookmarksGuidCol;
private int mBookmarksTypeBookmark;
private String mHistoryTitleCol;
private String mHistoryUrlCol;
private String mHistoryVisitsCol;
private String mHistoryFaviconCol;
private String mHistoryThumbnailCol;
private String mHistoryLastVisitedCol;
@Override
protected int getTestType() {
return TEST_TALOS;
}
private void loadFilterMethod() throws Exception {
Class browserDBClass = mClassLoader.loadClass("org.mozilla.gecko.db.BrowserDB");
mFilterMethod =
browserDBClass.getDeclaredMethod("filter", ContentResolver.class,
CharSequence.class, int.class);
}
private void initializeBrowserProvider() throws Exception {
Class browserDBClass = mClassLoader.loadClass("org.mozilla.gecko.db.BrowserDB");
Method initializeMethod =
browserDBClass.getDeclaredMethod("initialize", String.class);
initializeMethod.invoke(null, "default");
}
private void loadContractInfo() throws Exception {
mBookmarksUri = getContentUri("Bookmarks");
mHistoryUri = getContentUri("History");
mBookmarksIdCol = getStringColumn("Bookmarks", "_ID");
mBookmarksTitleCol = getStringColumn("Bookmarks", "TITLE");
mBookmarksUrlCol = getStringColumn("Bookmarks", "URL");
mBookmarksParentCol = getStringColumn("Bookmarks", "PARENT");
mBookmarksTypeCol = getStringColumn("Bookmarks", "TYPE");
mBookmarksPositionCol = getStringColumn("Bookmarks", "POSITION");
mBookmarksTagsCol = getStringColumn("Bookmarks", "TAGS");
mBookmarksDescriptionCol = getStringColumn("Bookmarks", "DESCRIPTION");
mBookmarksKeywordCol= getStringColumn("Bookmarks", "KEYWORD");
mBookmarksGuidCol= getStringColumn("Bookmarks", "GUID");
mBookmarksTypeBookmark = getIntColumn("Bookmarks", "TYPE_BOOKMARK");
mHistoryTitleCol = getStringColumn("History", "TITLE");
mHistoryUrlCol = getStringColumn("History", "URL");
mHistoryVisitsCol = getStringColumn("History", "VISITS");
mHistoryLastVisitedCol = getStringColumn("History", "DATE_LAST_VISITED");
mHistoryFaviconCol = getStringColumn("History", "FAVICON");
}
private void loadMobileFolderId() throws Exception {
Cursor c = mProvider.query(mBookmarksUri, null,
mBookmarksGuidCol + " = ?",
new String[] { MOBILE_FOLDER_GUID },
null);
c.moveToFirst();
mMobileFolderId = c.getLong(c.getColumnIndex(mBookmarksIdCol));
c.close();
}
private ContentValues createBookmarkEntry(String title, String url, long parentId,
int type, int position, String tags, String description, String keyword) throws Exception {
ContentValues bookmark = new ContentValues();
bookmark.put(mBookmarksTitleCol, title);
bookmark.put(mBookmarksUrlCol, url);
bookmark.put(mBookmarksParentCol, parentId);
bookmark.put(mBookmarksTypeCol, type);
bookmark.put(mBookmarksPositionCol, position);
bookmark.put(mBookmarksTagsCol, tags);
bookmark.put(mBookmarksDescriptionCol, description);
bookmark.put(mBookmarksKeywordCol, keyword);
return bookmark;
}
private ContentValues createBookmarkEntryWithUrl(String url) throws Exception {
return createBookmarkEntry(url, url, mMobileFolderId,
mBookmarksTypeBookmark, 0, "tags", "description", "keyword");
}
private ContentValues createRandomBookmarkEntry() throws Exception {
return createRandomBookmarkEntry("");
}
private ContentValues createRandomBookmarkEntry(String knownPrefix) throws Exception {
String randomStr = createRandomUrl(knownPrefix);
return createBookmarkEntryWithUrl(randomStr);
}
private ContentValues createHistoryEntry(String title, String url, int visits,
long lastVisited, byte[] favicon) throws Exception {
ContentValues historyEntry = new ContentValues();
historyEntry.put(mHistoryTitleCol, title);
historyEntry.put(mHistoryUrlCol, url);
historyEntry.put(mHistoryVisitsCol, visits);
historyEntry.put(mHistoryLastVisitedCol, lastVisited);
historyEntry.put(mHistoryFaviconCol, favicon);
return historyEntry;
}
private ContentValues createHistoryEntryWithUrl(String url) throws Exception {
int visits = mGenerator.nextInt(500);
return createHistoryEntry(url, url, visits,
System.currentTimeMillis(), url.getBytes("UTF8"));
}
private ContentValues createRandomHistoryEntry() throws Exception {
return createRandomHistoryEntry("");
}
private ContentValues createRandomHistoryEntry(String knownPrefix) throws Exception {
String randomStr = createRandomUrl(knownPrefix);
return createHistoryEntryWithUrl(randomStr);
}
private String createRandomUrl(String knownPrefix) throws Exception {
return knownPrefix + UUID.randomUUID().toString();
}
private void addTonsOfUrls() throws Exception {
// Create some random bookmark entries
ContentValues[] bookmarkEntries = new ContentValues[BATCH_SIZE];
for (int i = 0; i < NUMBER_OF_BASIC_BOOKMARK_URLS / BATCH_SIZE; i++) {
bookmarkEntries = new ContentValues[BATCH_SIZE];
for (int j = 0; j < BATCH_SIZE; j++) {
bookmarkEntries[j] = createRandomBookmarkEntry();
}
mProvider.bulkInsert(mBookmarksUri, bookmarkEntries);
}
// Create some random history entries
ContentValues[] historyEntries = new ContentValues[BATCH_SIZE];
for (int i = 0; i < NUMBER_OF_BASIC_HISTORY_URLS / BATCH_SIZE; i++) {
historyEntries = new ContentValues[BATCH_SIZE];
for (int j = 0; j < BATCH_SIZE; j++) {
historyEntries[j] = createRandomHistoryEntry();
}
mProvider.bulkInsert(mHistoryUri, historyEntries);
}
// Create random bookmark/history entries with the same url
for (int i = 0; i < NUMBER_OF_COMBINED_URLS / BATCH_SIZE; i++) {
bookmarkEntries = new ContentValues[BATCH_SIZE];
historyEntries = new ContentValues[BATCH_SIZE];
for (int j = 0; j < BATCH_SIZE; j++) {
String url = createRandomUrl("");
bookmarkEntries[j] = createBookmarkEntryWithUrl(url);
historyEntries[j] = createHistoryEntryWithUrl(url);
}
mProvider.bulkInsert(mBookmarksUri, bookmarkEntries);
mProvider.bulkInsert(mHistoryUri, historyEntries);
}
// Create some history entries with a known prefix
historyEntries = new ContentValues[NUMBER_OF_KNOWN_URLS];
for (int i = 0; i < NUMBER_OF_KNOWN_URLS; i++) {
historyEntries[i] = createRandomHistoryEntry(KNOWN_PREFIX);
}
mProvider.bulkInsert(mHistoryUri, historyEntries);
}
public void setUp() throws Exception {
super.setUp("@ANDROID_PACKAGE_NAME@.db.BrowserProvider", "AUTHORITY");
mGenerator = new Random(19580427);
loadContractInfo();
loadFilterMethod();
}
public void testBrowserProviderPerf() throws Exception {
initializeBrowserProvider();
loadMobileFolderId();
addTonsOfUrls();
long start = SystemClock.uptimeMillis();
Cursor c = (Cursor) mFilterMethod.invoke(null, mResolver, KNOWN_PREFIX, 100);
c.getCount(); // ensure query is not lazy loaded
long end = SystemClock.uptimeMillis();
mAsserter.dumpLog("__start_report" + Long.toString(end - start) + "__end_report");
mAsserter.dumpLog("__startTimestamp" + Long.toString(end - start) + "__endTimestamp");
c.close();
}
public void tearDown() throws Exception {
super.tearDown();
}
}