Skip to content

Commit 737c63c

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 92ce1f9 commit 737c63c

File tree

1 file changed

+83
-35
lines changed

1 file changed

+83
-35
lines changed

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 {
@@ -77,18 +78,30 @@ public void map(Map<String, Object> document, Emitter emitter) {
7778
}
7879
}
7980

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

94107
// query for entities by indexed string at random
@@ -159,36 +172,61 @@ private void oneByOneCrudRun(int count) throws CouchbaseLiteException {
159172
deleteAll();
160173
}
161174

162-
private void batchCrudRun(int count) throws Exception {
175+
private void batchCrudRun(final int count) throws Exception {
163176
// precreate property maps for documents
164-
List<Map<String, Object>> maps = new ArrayList<>(count);
177+
final List<Map<String, Object>> maps = new ArrayList<>(count);
165178
for (int i = 0; i < count; i++) {
166179
maps.add(createDocumentMap(i));
167180
}
168181

169182
startClock();
170-
List<Document> documents = new ArrayList<>(count);
171-
database.beginTransaction();
172-
for (int i = 0; i < count; i++) {
173-
// use our own ids (use .createDocument() for random UUIDs)
174-
Document document = database.getDocument(String.valueOf(i));
175-
document.putProperties(maps.get(i));
176-
documents.add(document);
183+
final List<Document> documents = new ArrayList<>(count);
184+
boolean successful = database.runInTransaction(new TransactionalTask() {
185+
@Override
186+
public boolean run() {
187+
for (int i = 0; i < count; i++) {
188+
// use our own ids (use .createDocument() for random UUIDs)
189+
Document document = database.getDocument(String.valueOf(i));
190+
try {
191+
// TODO ut: exception with 409 (HTTP 409 conflict)
192+
document.putProperties(maps.get(i));
193+
} catch (CouchbaseLiteException e) {
194+
log(e.toString());
195+
return false;
196+
}
197+
documents.add(document);
198+
}
199+
return true;
200+
}
201+
});
202+
if (!successful) {
203+
throw new RuntimeException("Exception while running transaction");
177204
}
178-
database.endTransaction(true);
179205
stopClock(LogMessage.BATCH_CREATE);
180206

181207
startClock();
182-
database.beginTransaction();
183-
for (int i = 0; i < count; i++) {
184-
Document document = documents.get(i);
185-
Map<String, Object> updatedProperties = new HashMap<>();
186-
// copy existing properties to get _rev property
187-
updatedProperties.putAll(document.getProperties());
188-
updatedProperties.putAll(maps.get(i));
189-
document.putProperties(updatedProperties);
208+
successful = database.runInTransaction(new TransactionalTask() {
209+
@Override
210+
public boolean run() {
211+
for (int i = 0; i < count; i++) {
212+
Document document = documents.get(i);
213+
Map<String, Object> updatedProperties = new HashMap<>();
214+
// copy existing properties to get _rev property
215+
updatedProperties.putAll(document.getProperties());
216+
updatedProperties.putAll(maps.get(i));
217+
try {
218+
document.putProperties(updatedProperties);
219+
} catch (CouchbaseLiteException e) {
220+
log(e.toString());
221+
return false;
222+
}
223+
}
224+
return true;
225+
}
226+
});
227+
if (!successful) {
228+
throw new RuntimeException("Exception while running transaction");
190229
}
191-
database.endTransaction(true);
192230
stopClock(LogMessage.BATCH_UPDATE);
193231

194232
// clear the document cache to force loading properties from the database
@@ -227,13 +265,23 @@ private void batchCrudRun(int count) throws Exception {
227265
private void deleteAll() throws CouchbaseLiteException {
228266
// query all documents, mark them as deleted
229267
Query query = database.createAllDocumentsQuery();
230-
QueryEnumerator result = query.run();
231-
database.beginTransaction();
232-
while (result.hasNext()) {
233-
QueryRow row = result.next();
234-
row.getDocument().purge();
268+
final QueryEnumerator result = query.run();
269+
boolean successful = database.runInTransaction(new TransactionalTask() {
270+
@Override
271+
public boolean run() {
272+
QueryRow row = result.next();
273+
try {
274+
row.getDocument().purge();
275+
} catch (CouchbaseLiteException e) {
276+
log(e.toString());
277+
return false;
278+
}
279+
return true;
280+
}
281+
});
282+
if (!successful) {
283+
throw new RuntimeException("Exception while running transaction");
235284
}
236-
database.endTransaction(true);
237285
}
238286

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

0 commit comments

Comments
 (0)