Skip to content

Commit 8889802

Browse files
Update couchbase perf test.
- Public transaction API has changed without notice?! New runInTransaction API fails now with 409 conflict exception. - Rename to PerfTestActiveAndroid. - Add note about ceased development.
1 parent 11ac1cf commit 8889802

1 file changed

Lines changed: 83 additions & 35 deletions

File tree

Couchbase/src/androidTest/java/de/greenrobot/performance/couchbase/PerfTestCouchbase.java

Lines changed: 83 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.couchbase.lite.Query;
1010
import com.couchbase.lite.QueryEnumerator;
1111
import com.couchbase.lite.QueryRow;
12+
import com.couchbase.lite.TransactionalTask;
1213
import com.couchbase.lite.View;
1314
import com.couchbase.lite.android.AndroidContext;
1415
import de.greenrobot.performance.BasePerfTestCase;
@@ -21,7 +22,7 @@
2122
import java.util.Map;
2223

2324
/**
24-
* http://developer.couchbase.com/documentation/mobile/1.1.0/develop/training/build-first-android-app/index.html
25+
* http://developer.couchbase.com/documentation/mobile/1.2/develop/training/build-first-android-app/index.html
2526
* https://github.com/couchbaselabs/ToDoLite-Android
2627
*/
2728
public class PerfTestCouchbase extends BasePerfTestCase {
@@ -72,18 +73,30 @@ public void map(Map<String, Object> document, Emitter emitter) {
7273
}
7374
}
7475

75-
private void indexedStringEntityQueriesRun(View indexedStringView, int count)
76+
private void indexedStringEntityQueriesRun(View indexedStringView, final int count)
7677
throws CouchbaseLiteException {
7778
// create entities
78-
String[] fixedRandomStrings = StringGenerator.createFixedRandomStrings(count);
79-
database.beginTransaction();
80-
for (int i = 0; i < count; i++) {
81-
Document entity = database.getDocument(String.valueOf(i));
82-
Map<String, Object> properties = new HashMap<>();
83-
properties.put("indexedString", fixedRandomStrings[i]);
84-
entity.putProperties(properties);
79+
final String[] fixedRandomStrings = StringGenerator.createFixedRandomStrings(count);
80+
boolean successful = database.runInTransaction(new TransactionalTask() {
81+
@Override
82+
public boolean run() {
83+
for (int i = 0; i < count; i++) {
84+
Document entity = database.getDocument(String.valueOf(i));
85+
Map<String, Object> properties = new HashMap<>();
86+
properties.put("indexedString", fixedRandomStrings[i]);
87+
try {
88+
entity.putProperties(properties);
89+
} catch (CouchbaseLiteException e) {
90+
log(e.toString());
91+
return false;
92+
}
93+
}
94+
return true;
95+
}
96+
});
97+
if (!successful) {
98+
throw new RuntimeException("Exception while running transaction");
8599
}
86-
database.endTransaction(true);
87100
log("Built and inserted entities.");
88101

89102
// query for entities by indexed string at random
@@ -154,36 +167,61 @@ private void oneByOneCrudRun(int count) throws CouchbaseLiteException {
154167
deleteAll();
155168
}
156169

157-
private void batchCrudRun(int count) throws Exception {
170+
private void batchCrudRun(final int count) throws Exception {
158171
// precreate property maps for documents
159-
List<Map<String, Object>> maps = new ArrayList<>(count);
172+
final List<Map<String, Object>> maps = new ArrayList<>(count);
160173
for (int i = 0; i < count; i++) {
161174
maps.add(createDocumentMap(i));
162175
}
163176

164177
startClock();
165-
List<Document> documents = new ArrayList<>(count);
166-
database.beginTransaction();
167-
for (int i = 0; i < count; i++) {
168-
// use our own ids (use .createDocument() for random UUIDs)
169-
Document document = database.getDocument(String.valueOf(i));
170-
document.putProperties(maps.get(i));
171-
documents.add(document);
178+
final List<Document> documents = new ArrayList<>(count);
179+
boolean successful = database.runInTransaction(new TransactionalTask() {
180+
@Override
181+
public boolean run() {
182+
for (int i = 0; i < count; i++) {
183+
// use our own ids (use .createDocument() for random UUIDs)
184+
Document document = database.getDocument(String.valueOf(i));
185+
try {
186+
// TODO ut: exception with 409 (HTTP 409 conflict)
187+
document.putProperties(maps.get(i));
188+
} catch (CouchbaseLiteException e) {
189+
log(e.toString());
190+
return false;
191+
}
192+
documents.add(document);
193+
}
194+
return true;
195+
}
196+
});
197+
if (!successful) {
198+
throw new RuntimeException("Exception while running transaction");
172199
}
173-
database.endTransaction(true);
174200
stopClock(LogMessage.BATCH_CREATE);
175201

176202
startClock();
177-
database.beginTransaction();
178-
for (int i = 0; i < count; i++) {
179-
Document document = documents.get(i);
180-
Map<String, Object> updatedProperties = new HashMap<>();
181-
// copy existing properties to get _rev property
182-
updatedProperties.putAll(document.getProperties());
183-
updatedProperties.putAll(maps.get(i));
184-
document.putProperties(updatedProperties);
203+
successful = database.runInTransaction(new TransactionalTask() {
204+
@Override
205+
public boolean run() {
206+
for (int i = 0; i < count; i++) {
207+
Document document = documents.get(i);
208+
Map<String, Object> updatedProperties = new HashMap<>();
209+
// copy existing properties to get _rev property
210+
updatedProperties.putAll(document.getProperties());
211+
updatedProperties.putAll(maps.get(i));
212+
try {
213+
document.putProperties(updatedProperties);
214+
} catch (CouchbaseLiteException e) {
215+
log(e.toString());
216+
return false;
217+
}
218+
}
219+
return true;
220+
}
221+
});
222+
if (!successful) {
223+
throw new RuntimeException("Exception while running transaction");
185224
}
186-
database.endTransaction(true);
187225
stopClock(LogMessage.BATCH_UPDATE);
188226

189227
// clear the document cache to force loading properties from the database
@@ -222,13 +260,23 @@ private void batchCrudRun(int count) throws Exception {
222260
private void deleteAll() throws CouchbaseLiteException {
223261
// query all documents, mark them as deleted
224262
Query query = database.createAllDocumentsQuery();
225-
QueryEnumerator result = query.run();
226-
database.beginTransaction();
227-
while (result.hasNext()) {
228-
QueryRow row = result.next();
229-
row.getDocument().purge();
263+
final QueryEnumerator result = query.run();
264+
boolean successful = database.runInTransaction(new TransactionalTask() {
265+
@Override
266+
public boolean run() {
267+
QueryRow row = result.next();
268+
try {
269+
row.getDocument().purge();
270+
} catch (CouchbaseLiteException e) {
271+
log(e.toString());
272+
return false;
273+
}
274+
return true;
275+
}
276+
});
277+
if (!successful) {
278+
throw new RuntimeException("Exception while running transaction");
230279
}
231-
database.endTransaction(true);
232280
}
233281

234282
private Map<String, Object> createDocumentMap(int seed) throws CouchbaseLiteException {

0 commit comments

Comments
 (0)