Skip to content

Commit

Permalink
Update couchbase perf test.
Browse files Browse the repository at this point in the history
- Public transaction API has changed without notice?! New runInTransaction API fails now with 409 conflict exception.
- Rename to PerfTestActiveAndroid.
- Add note about ceased development.
  • Loading branch information
greenrobot-team committed Mar 14, 2016
1 parent a789524 commit fcc85f0
Showing 1 changed file with 85 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.couchbase.lite.Query;
import com.couchbase.lite.QueryEnumerator;
import com.couchbase.lite.QueryRow;
import com.couchbase.lite.TransactionalTask;
import com.couchbase.lite.View;
import com.couchbase.lite.android.AndroidContext;
import de.greenrobot.performance.BasePerfTestCase;
Expand All @@ -21,10 +22,10 @@
import java.util.Map;

/**
* http://developer.couchbase.com/documentation/mobile/1.1.0/develop/training/build-first-android-app/index.html
* http://developer.couchbase.com/documentation/mobile/1.2/develop/training/build-first-android-app/index.html
* https://github.com/couchbaselabs/ToDoLite-Android
*/
public class PerformanceTestCouchbase extends BasePerfTestCase {
public class PerfTestCouchbase extends BasePerfTestCase {

private static final String DB_NAME = "couchbase-test";
private static final String DOC_TYPE = "simpleentity";
Expand All @@ -33,7 +34,7 @@ public class PerformanceTestCouchbase extends BasePerfTestCase {

@Override
protected String getLogTag() {
return "PerfTestCouchbase";
return getClass().getSimpleName();
}

@Override
Expand Down Expand Up @@ -77,18 +78,30 @@ public void map(Map<String, Object> document, Emitter emitter) {
}
}

private void indexedStringEntityQueriesRun(View indexedStringView, int count)
private void indexedStringEntityQueriesRun(View indexedStringView, final int count)
throws CouchbaseLiteException {
// create entities
String[] fixedRandomStrings = StringGenerator.createFixedRandomStrings(count);
database.beginTransaction();
for (int i = 0; i < count; i++) {
Document entity = database.getDocument(String.valueOf(i));
Map<String, Object> properties = new HashMap<>();
properties.put("indexedString", fixedRandomStrings[i]);
entity.putProperties(properties);
final String[] fixedRandomStrings = StringGenerator.createFixedRandomStrings(count);
boolean successful = database.runInTransaction(new TransactionalTask() {
@Override
public boolean run() {
for (int i = 0; i < count; i++) {
Document entity = database.getDocument(String.valueOf(i));
Map<String, Object> properties = new HashMap<>();
properties.put("indexedString", fixedRandomStrings[i]);
try {
entity.putProperties(properties);
} catch (CouchbaseLiteException e) {
log(e.toString());
return false;
}
}
return true;
}
});
if (!successful) {
throw new RuntimeException("Exception while running transaction");
}
database.endTransaction(true);
log("Built and inserted entities.");

// query for entities by indexed string at random
Expand Down Expand Up @@ -156,36 +169,61 @@ private void oneByOneCrudRun(int count) throws CouchbaseLiteException {
deleteAll();
}

private void batchCrudRun(int count) throws Exception {
private void batchCrudRun(final int count) throws Exception {
// precreate property maps for documents
List<Map<String, Object>> maps = new ArrayList<>(count);
final List<Map<String, Object>> maps = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
maps.add(createDocumentMap(i));
}

startClock();
List<Document> documents = new ArrayList<>(count);
database.beginTransaction();
for (int i = 0; i < count; i++) {
// use our own ids (use .createDocument() for random UUIDs)
Document document = database.getDocument(String.valueOf(i));
document.putProperties(maps.get(i));
documents.add(document);
final List<Document> documents = new ArrayList<>(count);
boolean successful = database.runInTransaction(new TransactionalTask() {
@Override
public boolean run() {
for (int i = 0; i < count; i++) {
// use our own ids (use .createDocument() for random UUIDs)
Document document = database.getDocument(String.valueOf(i));
try {
// TODO ut: exception with 409 (HTTP 409 conflict)
document.putProperties(maps.get(i));
} catch (CouchbaseLiteException e) {
log(e.toString());
return false;
}
documents.add(document);
}
return true;
}
});
if (!successful) {
throw new RuntimeException("Exception while running transaction");
}
database.endTransaction(true);
stopClock(LogMessage.BATCH_CREATE);

startClock();
database.beginTransaction();
for (int i = 0; i < count; i++) {
Document document = documents.get(i);
Map<String, Object> updatedProperties = new HashMap<>();
// copy existing properties to get _rev property
updatedProperties.putAll(document.getProperties());
updatedProperties.putAll(maps.get(i));
document.putProperties(updatedProperties);
successful = database.runInTransaction(new TransactionalTask() {
@Override
public boolean run() {
for (int i = 0; i < count; i++) {
Document document = documents.get(i);
Map<String, Object> updatedProperties = new HashMap<>();
// copy existing properties to get _rev property
updatedProperties.putAll(document.getProperties());
updatedProperties.putAll(maps.get(i));
try {
document.putProperties(updatedProperties);
} catch (CouchbaseLiteException e) {
log(e.toString());
return false;
}
}
return true;
}
});
if (!successful) {
throw new RuntimeException("Exception while running transaction");
}
database.endTransaction(true);
stopClock(LogMessage.BATCH_UPDATE);

startClock();
Expand Down Expand Up @@ -221,13 +259,23 @@ private void batchCrudRun(int count) throws Exception {
private void deleteAll() throws CouchbaseLiteException {
// query all documents, mark them as deleted
Query query = database.createAllDocumentsQuery();
QueryEnumerator result = query.run();
database.beginTransaction();
while (result.hasNext()) {
QueryRow row = result.next();
row.getDocument().purge();
final QueryEnumerator result = query.run();
boolean successful = database.runInTransaction(new TransactionalTask() {
@Override
public boolean run() {
QueryRow row = result.next();
try {
row.getDocument().purge();
} catch (CouchbaseLiteException e) {
log(e.toString());
return false;
}
return true;
}
});
if (!successful) {
throw new RuntimeException("Exception while running transaction");
}
database.endTransaction(true);
}

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

0 comments on commit fcc85f0

Please sign in to comment.