Skip to content

Commit fd51dc0

Browse files
committed
Added insert timing with/without transactions.
1 parent f72742c commit fd51dc0

File tree

5 files changed

+240
-2
lines changed

5 files changed

+240
-2
lines changed

dblocking.iml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<option name="LIBRARY_PROJECT" value="false" />
2020
<option name="RUN_PROCESS_RESOURCES_MAVEN_TASK" value="true" />
2121
<option name="GENERATE_UNSIGNED_APK" value="false" />
22+
<option name="CUSTOM_DEBUG_KEYSTORE_PATH" value="" />
2223
</configuration>
2324
</facet>
2425
</component>

gen/co/touchlab/R.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@ public static final class drawable {
1515
}
1616
public static final class id {
1717
public static final int manyHelpers=0x7f050001;
18+
public static final int noTrans=0x7f050004;
19+
public static final int numberOfInserts=0x7f050003;
1820
public static final int oneHelper=0x7f050000;
19-
public static final int results=0x7f050002;
21+
public static final int results=0x7f050007;
22+
public static final int testTransactionIsolation=0x7f050002;
23+
public static final int timeOut=0x7f050006;
24+
public static final int trans=0x7f050005;
2025
}
2126
public static final class layout {
2227
public static final int main=0x7f030000;

res/layout/main.xml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,40 @@
2020
android:layout_height="wrap_content"
2121
android:text="Many Helpers"/>
2222

23+
<Button
24+
android:id="@+id/testTransactionIsolation"
25+
android:layout_width="wrap_content"
26+
android:layout_height="wrap_content"
27+
android:text="Test Transaction Isolation"/>
28+
29+
<EditText
30+
android:layout_width="fill_parent"
31+
android:layout_height="wrap_content"
32+
android:id="@+id/numberOfInserts"/>
33+
<LinearLayout
34+
android:layout_width="fill_parent"
35+
android:layout_height="wrap_content"
36+
android:orientation="horizontal">
37+
<Button
38+
android:layout_width="wrap_content"
39+
android:layout_height="wrap_content"
40+
android:layout_weight="1"
41+
android:text="No Transaction"
42+
android:id="@+id/noTrans"/>
43+
<Button
44+
android:layout_width="wrap_content"
45+
android:layout_height="wrap_content"
46+
android:layout_weight="1"
47+
android:text="Transaction"
48+
android:id="@+id/trans"/>
49+
</LinearLayout>
50+
51+
<TextView
52+
android:layout_width="fill_parent"
53+
android:layout_height="wrap_content"
54+
android:id="@+id/timeOut"/>
55+
56+
2357
<TextView
2458
android:layout_width="fill_parent"
2559
android:layout_height="wrap_content"

src/co/touchlab/dblocking/DatabaseHelper.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ public class DatabaseHelper extends SQLiteOpenHelper
2727
};
2828

2929

30+
public static synchronized DatabaseHelper getInstance(Context context)
31+
{
32+
if(helper == null)
33+
{
34+
helper = new DatabaseHelper(context);
35+
}
36+
37+
return helper;
38+
}
39+
3040
public DatabaseHelper(Context context)
3141
{
3242
super(context, DATABASE_NAME, null, DATABASE_VERSION);
@@ -60,6 +70,13 @@ public void createSession(String desc)
6070

6171
writableDatabase.insertOrThrow("session", null, contentValues);
6272
}
73+
74+
public int countSessions()
75+
{
76+
Cursor cursor = getReadableDatabase().rawQuery("select count(*) from session", null);
77+
cursor.moveToFirst();
78+
return cursor.getInt(0);
79+
}
6380

6481
public void updateSession(Integer id, String desc)
6582
{

src/co/touchlab/dblocking/MyActivity.java

Lines changed: 182 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package co.touchlab.dblocking;
22

33
import android.app.Activity;
4+
import android.content.ContentValues;
5+
import android.database.sqlite.SQLiteDatabase;
6+
import android.os.AsyncTask;
47
import android.os.Bundle;
58
import android.os.Handler;
69
import android.util.Log;
710
import android.view.View;
11+
import android.widget.EditText;
812
import android.widget.TextView;
9-
import com.example.R;
13+
import co.touchlab.R;
1014

1115
import java.util.ArrayList;
1216
import java.util.List;
@@ -18,6 +22,7 @@ public class MyActivity extends Activity
1822
private AtomicInteger allCount = new AtomicInteger();
1923
private Handler uiHandler;
2024

25+
2126
@Override
2227
public void onCreate(Bundle savedInstanceState)
2328
{
@@ -63,6 +68,182 @@ public void onClick(View view)
6368
runAllThreads(allThreads);
6469
}
6570
});
71+
72+
findViewById(R.id.testTransactionIsolation).setOnClickListener(new View.OnClickListener()
73+
{
74+
public void onClick(View view)
75+
{
76+
DatabaseHelper helper = new DatabaseHelper(MyActivity.this);
77+
new SlowInsertThread(helper).start();
78+
new FastSelectThread(helper).start();
79+
}
80+
});
81+
82+
findViewById(R.id.noTrans).setOnClickListener(new View.OnClickListener()
83+
{
84+
public void onClick(View view)
85+
{
86+
runNoTrans();
87+
}
88+
});
89+
90+
findViewById(R.id.trans).setOnClickListener(new View.OnClickListener()
91+
{
92+
public void onClick(View view)
93+
{
94+
runTrans();
95+
}
96+
});
97+
}
98+
99+
@SuppressWarnings("unchecked")
100+
private void runTrans()
101+
{
102+
new AsyncTask(){
103+
104+
@Override
105+
protected Object doInBackground(Object... objects)
106+
{
107+
DatabaseHelper instance = DatabaseHelper.getInstance(MyActivity.this);
108+
109+
SQLiteDatabase writableDatabase = instance.getWritableDatabase();
110+
111+
long start = System.currentTimeMillis();
112+
writableDatabase.beginTransaction();
113+
insertTest(instance);
114+
writableDatabase.setTransactionSuccessful();
115+
writableDatabase.endTransaction();
116+
117+
return start;
118+
}
119+
120+
@Override
121+
protected void onPostExecute(Object o)
122+
{
123+
showTime((Long)o, DatabaseHelper.getInstance(MyActivity.this));
124+
}
125+
}.execute();
126+
127+
128+
}
129+
130+
@SuppressWarnings("unchecked")
131+
private void runNoTrans()
132+
{
133+
new AsyncTask(){
134+
135+
@Override
136+
protected Object doInBackground(Object... objects)
137+
{
138+
DatabaseHelper instance = DatabaseHelper.getInstance(MyActivity.this);
139+
long start = System.currentTimeMillis();
140+
insertTest(instance);
141+
142+
return start;
143+
}
144+
145+
@Override
146+
protected void onPostExecute(Object o)
147+
{
148+
showTime((Long)o, DatabaseHelper.getInstance(MyActivity.this));
149+
}
150+
}.execute();
151+
}
152+
153+
private void showTime(long start, DatabaseHelper instance)
154+
{
155+
long time = System.currentTimeMillis() - start;
156+
((TextView)findViewById(R.id.timeOut)).setText("Total rows: "+ instance.countSessions() +"/time: "+ Long.toString(time));
157+
}
158+
159+
private void insertTest(DatabaseHelper helper)
160+
{
161+
162+
int numberOfInserts = Integer.parseInt(((EditText) findViewById(R.id.numberOfInserts)).getText().toString());
163+
while(numberOfInserts > 0)
164+
{
165+
helper.createSession("Count: "+ numberOfInserts);
166+
numberOfInserts--;
167+
}
168+
169+
}
170+
171+
class SlowInsertThread extends Thread
172+
{
173+
private DatabaseHelper helper;
174+
private Handler handler;
175+
176+
SlowInsertThread(DatabaseHelper helper)
177+
{
178+
this.helper = helper;
179+
handler = new Handler();
180+
}
181+
182+
@Override
183+
public void run()
184+
{
185+
int count = 0;
186+
while(count < 10)
187+
{
188+
SQLiteDatabase db = helper.getWritableDatabase();
189+
db.beginTransaction();
190+
final ContentValues contentValues = new ContentValues();
191+
192+
contentValues.put("description", "asdlkfj");
193+
194+
Log.i(getClass().getName(), "insert");
195+
db.insertOrThrow("session", null, contentValues);
196+
Log.i(getClass().getName(), "start wait");
197+
try
198+
{
199+
Thread.sleep(5000);
200+
}
201+
catch (InterruptedException e)
202+
{
203+
e.printStackTrace();
204+
}
205+
Log.i(getClass().getName(), "end wait");
206+
db.endTransaction();
207+
208+
count++;
209+
}
210+
}
211+
}
212+
213+
class FastSelectThread extends Thread
214+
{
215+
private DatabaseHelper helper;
216+
private Handler handler;
217+
218+
FastSelectThread(DatabaseHelper helper)
219+
{
220+
this.helper = helper;
221+
handler = new Handler();
222+
}
223+
224+
@Override
225+
public void run()
226+
{
227+
int count = 0;
228+
while(count < 100)
229+
{
230+
helper.loadAllSessions();
231+
Log.i(getClass().getName(), "selected");
232+
233+
Log.i(getClass().getName(), "start wait");
234+
try
235+
{
236+
Thread.sleep(500);
237+
}
238+
catch (InterruptedException e)
239+
{
240+
e.printStackTrace();
241+
}
242+
Log.i(getClass().getName(), "end wait");
243+
244+
count++;
245+
}
246+
}
66247
}
67248

68249
private void runAllThreads(final List<DbInsertThread> allThreads)

0 commit comments

Comments
 (0)