001: package org.garret.bench;
002:
003: import java.io.PrintStream;
004:
005: import android.database.sqlite.*;
006: import android.database.*;
007:
008: public class SqlLiteTest extends Test {
009: final static int nRecords = 10000;
010: final static int pagePoolSize = 2 * 1024 * 1024;
011:
012: String databaseName;
013: PrintStream out;
014:
015: SqlLiteTest(String databaseName, PrintStream out) {
016: this .databaseName = databaseName;
017: this .out = out;
018: }
019:
020: public String getName() {
021: return "SqLite";
022: }
023:
024: public void run() {
025: SQLiteDatabase db = SQLiteDatabase
026: .create(databaseName, 1, null);
027: db.execSQL("create table TestIndex (i integer(8), s text)");
028: db.execSQL("create index StrIndex on TestIndex (s)");
029: db.execSQL("create index IntIndex on TestIndex (i)");
030: SQLiteStatement stmt = db
031: .compileStatement("insert into TestIndex (i,s) values (?,?)");
032: long start = System.currentTimeMillis();
033: long key = 1999;
034: int i;
035: for (i = 0; i < nRecords; i++) {
036: key = (3141592621L * key + 2718281829L) % 1000000007L;
037: stmt.bindLong(1, key);
038: stmt.bindString(2, Long.toString(key));
039: stmt.execute();
040: }
041: stmt.close();
042: out.println("Elapsed time for inserting " + nRecords
043: + " records: " + (System.currentTimeMillis() - start)
044: + " milliseconds");
045:
046: start = System.currentTimeMillis();
047: key = 1999;
048: for (i = 0; i < nRecords; i++) {
049: key = (3141592621L * key + 2718281829L) % 1000000007L;
050: String s = Long.toString(key);
051: Cursor c1 = db.query("TestIndex",
052: new String[] { "i", "s" }, "i=?",
053: new String[] { s }, null, null, null);
054: Cursor c2 = db.query("TestIndex",
055: new String[] { "i", "s" }, "s=?",
056: new String[] { s }, null, null, null);
057: boolean found = c1.first();
058: assert (found);
059: assert (c1.getLong(0) == key);
060: assert (c1.getString(1).equals(s));
061: assert (!c1.next());
062: c1.close();
063:
064: found = c2.first();
065: assert (found);
066: assert (c2.getLong(0) == key);
067: assert (c2.getString(1).equals(s));
068: assert (!c2.next());
069: c2.close();
070: }
071: out.println("Elapsed time for performing " + nRecords * 2
072: + " index searches: "
073: + (System.currentTimeMillis() - start)
074: + " milliseconds");
075:
076: start = System.currentTimeMillis();
077: key = Long.MIN_VALUE;
078: i = 0;
079: Cursor c1 = db.query("TestIndex", new String[] { "i", "s" },
080: null, null, null, null, "i");
081: while (c1.next()) {
082: assert (c1.getLong(0) >= key);
083: key = c1.getLong(0);
084: i += 1;
085: }
086: c1.close();
087: assert (i == nRecords);
088: String s = "";
089: i = 0;
090: Cursor c2 = db.query("TestIndex", new String[] { "i", "s" },
091: null, null, null, null, "s");
092: while (c2.next()) {
093: assert (c2.getString(1).compareTo(s) >= 0);
094: s = c2.getString(1);
095: i += 1;
096: }
097: assert (i == nRecords);
098: out.println("Elapsed time for iterating through "
099: + (nRecords * 2) + " records: "
100: + (System.currentTimeMillis() - start)
101: + " milliseconds");
102:
103: start = System.currentTimeMillis();
104: key = 1999;
105: stmt = db.compileStatement("delete from TestIndex where i=?");
106: for (i = 0; i < nRecords; i++) {
107: key = (3141592621L * key + 2718281829L) % 1000000007L;
108: stmt.bindLong(1, key);
109: stmt.execute();
110: }
111: stmt.close();
112: out.println("Elapsed time for deleting " + nRecords
113: + " records: " + (System.currentTimeMillis() - start)
114: + " milliseconds");
115: db.close();
116: out.flush();
117: done();
118: }
119:
120: }
|