3
3
import android .app .Application ;
4
4
import android .test .ApplicationTestCase ;
5
5
import android .util .Log ;
6
+ import de .greenrobot .performance .StringGenerator ;
6
7
import io .realm .Realm ;
7
8
import io .realm .RealmConfiguration ;
9
+ import io .realm .RealmQuery ;
8
10
import io .realm .RealmResults ;
9
11
import java .io .File ;
10
12
import java .sql .SQLException ;
@@ -19,6 +21,7 @@ public class PerformanceTestRealm extends ApplicationTestCase<Application> {
19
21
private static final String TAG = "PerfTestRealm" ;
20
22
21
23
private static final int BATCH_SIZE = 10000 ;
24
+ private static final int QUERY_COUNT = 1000 ;
22
25
private static final int RUNS = 8 ;
23
26
24
27
private Realm realm ;
@@ -63,28 +66,81 @@ protected void tearDown() throws Exception {
63
66
super .tearDown ();
64
67
}
65
68
66
- public void testPerformance () throws Exception {
69
+ public void testIndexedStringEntityQuery () {
67
70
//noinspection PointlessBooleanExpression
68
71
if (!BuildConfig .RUN_PERFORMANCE_TESTS ) {
69
72
Log .d (TAG , "Performance tests are disabled." );
70
73
return ;
71
74
}
72
- Log .d (TAG , "--------------- Start" );
75
+ Log .d (TAG , "--------Indexed Queries: Start" );
73
76
74
77
for (int i = 0 ; i < RUNS ; i ++) {
75
- runTests (BATCH_SIZE );
78
+ Log .d (TAG , "----Run " + (i + 1 ) + " of " + RUNS );
79
+ doIndexedStringEntityQuery ();
76
80
}
77
81
78
- Log .d (TAG , "--------------- End" );
82
+ Log .d (TAG , "--------Indexed Queries: End" );
79
83
}
80
84
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
83
98
realm .beginTransaction ();
84
- realm .allObjects ( SimpleEntityNotNull . class ). clear ( );
99
+ realm .copyToRealm ( entities );
85
100
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
+ }
86
119
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" );
88
144
}
89
145
90
146
protected void runTests (int entityCount ) throws Exception {
@@ -168,4 +224,13 @@ protected void runOneByOne(List<SimpleEntityNotNull> list, int count) throws SQL
168
224
time = System .currentTimeMillis () - start ;
169
225
Log .d (TAG , "Updated (one-by-one) " + count + " entities in " + time + " ms" );
170
226
}
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
+ }
171
236
}
0 commit comments