Skip to content

Commit 45860c9

Browse files
Add indexed string test for Realm.
- Add dependency on Common module.
1 parent 7a0f4a6 commit 45860c9

File tree

3 files changed

+110
-8
lines changed

3 files changed

+110
-8
lines changed

PerformanceTests/Realm/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ android {
2626
}
2727

2828
dependencies {
29+
androidTestCompile project(':PerformanceTests:Common')
2930
androidTestCompile 'io.realm:realm-android:0.82.2'
3031
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package de.greenrobot.performance.realm;
2+
3+
import io.realm.RealmObject;
4+
import io.realm.annotations.Index;
5+
import io.realm.annotations.PrimaryKey;
6+
7+
/**
8+
* Simple entity with a string property that is indexed.
9+
*/
10+
public class IndexedStringEntity extends RealmObject {
11+
12+
@PrimaryKey
13+
private long id;
14+
15+
@Index
16+
private String indexedString;
17+
18+
// Be aware that the getters and setters will be overridden by the generated proxy class
19+
// used in the back by RealmObjects, so any custom logic you add to the getters & setters
20+
// will not actually be executed
21+
public long getId() {
22+
return id;
23+
}
24+
25+
public void setId(long id) {
26+
this.id = id;
27+
}
28+
29+
public String getIndexedString() {
30+
return indexedString;
31+
}
32+
33+
public void setIndexedString(String indexedString) {
34+
this.indexedString = indexedString;
35+
}
36+
}

PerformanceTests/Realm/src/androidTest/java/de/greenrobot/performance/realm/PerformanceTestRealm.java

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import android.app.Application;
44
import android.test.ApplicationTestCase;
55
import android.util.Log;
6+
import de.greenrobot.performance.StringGenerator;
67
import io.realm.Realm;
78
import io.realm.RealmConfiguration;
9+
import io.realm.RealmQuery;
810
import io.realm.RealmResults;
911
import java.io.File;
1012
import java.sql.SQLException;
@@ -19,6 +21,7 @@ public class PerformanceTestRealm extends ApplicationTestCase<Application> {
1921
private static final String TAG = "PerfTestRealm";
2022

2123
private static final int BATCH_SIZE = 10000;
24+
private static final int QUERY_COUNT = 1000;
2225
private static final int RUNS = 8;
2326

2427
private Realm realm;
@@ -63,28 +66,81 @@ protected void tearDown() throws Exception {
6366
super.tearDown();
6467
}
6568

66-
public void testPerformance() throws Exception {
69+
public void testIndexedStringEntityQuery() {
6770
//noinspection PointlessBooleanExpression
6871
if (!BuildConfig.RUN_PERFORMANCE_TESTS) {
6972
Log.d(TAG, "Performance tests are disabled.");
7073
return;
7174
}
72-
Log.d(TAG, "---------------Start");
75+
Log.d(TAG, "--------Indexed Queries: Start");
7376

7477
for (int i = 0; i < RUNS; i++) {
75-
runTests(BATCH_SIZE);
78+
Log.d(TAG, "----Run " + (i + 1) + " of " + RUNS);
79+
doIndexedStringEntityQuery();
7680
}
7781

78-
Log.d(TAG, "---------------End");
82+
Log.d(TAG, "--------Indexed Queries: End");
7983
}
8084

81-
protected void deleteAll() {
82-
long start = System.currentTimeMillis();
85+
public void doIndexedStringEntityQuery() {
86+
// create entities
87+
List<IndexedStringEntity> entities = new ArrayList<>(BATCH_SIZE);
88+
String[] fixedRandomStrings = StringGenerator.createFixedRandomStrings(BATCH_SIZE);
89+
for (int i = 0; i < BATCH_SIZE; i++) {
90+
IndexedStringEntity entity = new IndexedStringEntity();
91+
entity.setId((long) i);
92+
entity.setIndexedString(fixedRandomStrings[i]);
93+
entities.add(entity);
94+
}
95+
Log.d(TAG, "Built entities.");
96+
97+
// insert entities
8398
realm.beginTransaction();
84-
realm.allObjects(SimpleEntityNotNull.class).clear();
99+
realm.copyToRealm(entities);
85100
realm.commitTransaction();
101+
Log.d(TAG, "Inserted entities.");
102+
103+
// query for entities by indexed string at random
104+
int[] randomIndices = StringGenerator.getFixedRandomIndices(QUERY_COUNT, BATCH_SIZE - 1);
105+
106+
long start = System.currentTimeMillis();
107+
for (int i = 0; i < QUERY_COUNT; i++) {
108+
int nextIndex = randomIndices[i];
109+
RealmQuery<IndexedStringEntity> query = realm.where(IndexedStringEntity.class);
110+
query.equalTo("indexedString", fixedRandomStrings[nextIndex]);
111+
RealmResults<IndexedStringEntity> result = query.findAll();
112+
for (int j = 0, resultSize = result.size(); j < resultSize; j++) {
113+
// actually get each entity so its object is reconstructed, same with properties
114+
IndexedStringEntity entity = result.get(j);
115+
entity.getId();
116+
entity.getIndexedString();
117+
}
118+
}
86119
long time = System.currentTimeMillis() - start;
87-
Log.d(TAG, "Deleted all entities in " + time + " ms");
120+
Log.d(TAG,
121+
"Queried for " + QUERY_COUNT + " of " + BATCH_SIZE + " indexed entities in " + time
122+
+ " ms.");
123+
124+
// delete all entities
125+
realm.beginTransaction();
126+
realm.allObjects(IndexedStringEntity.class).clear();
127+
realm.commitTransaction();
128+
Log.d(TAG, "Deleted all entities.");
129+
}
130+
131+
public void testPerformance() throws Exception {
132+
//noinspection PointlessBooleanExpression
133+
if (!BuildConfig.RUN_PERFORMANCE_TESTS) {
134+
Log.d(TAG, "Performance tests are disabled.");
135+
return;
136+
}
137+
Log.d(TAG, "---------------Start");
138+
139+
for (int i = 0; i < RUNS; i++) {
140+
runTests(BATCH_SIZE);
141+
}
142+
143+
Log.d(TAG, "---------------End");
88144
}
89145

90146
protected void runTests(int entityCount) throws Exception {
@@ -168,4 +224,13 @@ protected void runOneByOne(List<SimpleEntityNotNull> list, int count) throws SQL
168224
time = System.currentTimeMillis() - start;
169225
Log.d(TAG, "Updated (one-by-one) " + count + " entities in " + time + " ms");
170226
}
227+
228+
protected void deleteAll() {
229+
long start = System.currentTimeMillis();
230+
realm.beginTransaction();
231+
realm.allObjects(SimpleEntityNotNull.class).clear();
232+
realm.commitTransaction();
233+
long time = System.currentTimeMillis() - start;
234+
Log.d(TAG, "Deleted all entities in " + time + " ms");
235+
}
171236
}

0 commit comments

Comments
 (0)