@@ -25,7 +25,7 @@ public class PerformanceTestCouchbase extends ApplicationTestCase<Application> {
25
25
26
26
private static final String TAG = "PerfTestCouchbase" ;
27
27
28
- private static final int BATCH_SIZE = 1000 ;
28
+ private static final int BATCH_SIZE = 10000 ;
29
29
private static final int RUNS = 8 ;
30
30
31
31
private static final String DB_NAME = "couchbase-test" ;
@@ -78,28 +78,33 @@ protected void runTests(int entityCount) throws Exception {
78
78
79
79
long start , time ;
80
80
81
- // In Couchbase there is no such thing as batching,
82
- // each document has to be created on its own.
83
- // Hence we can only measure one-by-one creation time.
84
-
85
81
// precreate property maps for documents
86
82
List <Map <String , Object >> maps = new ArrayList <>(entityCount );
87
83
for (int i = 0 ; i < entityCount ; i ++) {
88
84
maps .add (createDocumentMap (i ));
89
85
}
86
+ System .gc ();
87
+
88
+ runOneByOne (maps , entityCount / 10 );
89
+
90
+ System .gc ();
91
+ deleteAll ();
90
92
91
93
start = System .currentTimeMillis ();
92
94
List <Document > documents = new ArrayList <>(entityCount );
95
+ database .beginTransaction ();
93
96
for (int i = 0 ; i < entityCount ; i ++) {
94
97
// use our own ids (use .createDocument() for random UUIDs)
95
98
Document document = database .getDocument (String .valueOf (i ));
96
99
document .putProperties (maps .get (i ));
97
100
documents .add (document );
98
101
}
102
+ database .endTransaction (true );
99
103
time = System .currentTimeMillis () - start ;
100
- Log .d (TAG , "Created (one-by-one ) " + documents . size () + " entities in " + time + " ms" );
104
+ Log .d (TAG , "Created (batch ) " + BATCH_SIZE + " entities in " + time + " ms" );
101
105
102
106
start = System .currentTimeMillis ();
107
+ database .beginTransaction ();
103
108
for (int i = 0 ; i < entityCount ; i ++) {
104
109
Document document = documents .get (i );
105
110
Map <String , Object > updatedProperties = new HashMap <>();
@@ -108,8 +113,9 @@ protected void runTests(int entityCount) throws Exception {
108
113
updatedProperties .putAll (maps .get (i ));
109
114
document .putProperties (updatedProperties );
110
115
}
116
+ database .endTransaction (true );
111
117
time = System .currentTimeMillis () - start ;
112
- Log .d (TAG , "Updated (one-by-one ) " + documents . size () + " entities in " + time + " ms" );
118
+ Log .d (TAG , "Updated (batch ) " + BATCH_SIZE + " entities in " + time + " ms" );
113
119
114
120
start = System .currentTimeMillis ();
115
121
List <Document > reloaded = new ArrayList <>();
@@ -144,15 +150,45 @@ protected void runTests(int entityCount) throws Exception {
144
150
Log .d (TAG , "---------------End: " + entityCount );
145
151
}
146
152
153
+ private void runOneByOne (List <Map <String , Object >> maps , int count )
154
+ throws CouchbaseLiteException {
155
+ long start ;
156
+ long time ;
157
+ start = System .currentTimeMillis ();
158
+ List <Document > documents = new ArrayList <>(count );
159
+ for (int i = 0 ; i < count ; i ++) {
160
+ // use our own ids (use .createDocument() for random UUIDs)
161
+ Document document = database .getDocument (String .valueOf (i ));
162
+ document .putProperties (maps .get (i ));
163
+ documents .add (document );
164
+ }
165
+ time = System .currentTimeMillis () - start ;
166
+ Log .d (TAG , "Inserted (one-by-one) " + count + " entities in " + time + " ms" );
167
+
168
+ start = System .currentTimeMillis ();
169
+ for (int i = 0 ; i < count ; i ++) {
170
+ Document document = documents .get (i );
171
+ Map <String , Object > updatedProperties = new HashMap <>();
172
+ // copy existing properties to get _rev property
173
+ updatedProperties .putAll (document .getProperties ());
174
+ updatedProperties .putAll (maps .get (i ));
175
+ document .putProperties (updatedProperties );
176
+ }
177
+ time = System .currentTimeMillis () - start ;
178
+ Log .d (TAG , "Updated (one-by-one) " + count + " entities in " + time + " ms" );
179
+ }
180
+
147
181
protected void deleteAll () throws CouchbaseLiteException {
148
182
long start = System .currentTimeMillis ();
149
183
// query all documents, mark them as deleted
150
184
Query query = database .createAllDocumentsQuery ();
151
185
QueryEnumerator result = query .run ();
186
+ database .beginTransaction ();
152
187
while (result .hasNext ()) {
153
188
QueryRow row = result .next ();
154
189
row .getDocument ().delete ();
155
190
}
191
+ database .endTransaction (true );
156
192
long time = System .currentTimeMillis () - start ;
157
193
Log .d (TAG , "Deleted all entities in " + time + " ms" );
158
194
}
0 commit comments