Skip to content

Commit 713896f

Browse files
Use transactions for Couchbase, add batch test.
- Turns out there are transactions for Couchbase (though nowhere mentioned in the docs, found out by accident), so add batch tests and measure again!
1 parent 1fb18d9 commit 713896f

File tree

1 file changed

+43
-7
lines changed

1 file changed

+43
-7
lines changed

PerformanceTests/Couchbase/src/androidTest/java/de/greenrobot/performance/couchbase/PerformanceTestCouchbase.java

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class PerformanceTestCouchbase extends ApplicationTestCase<Application> {
2525

2626
private static final String TAG = "PerfTestCouchbase";
2727

28-
private static final int BATCH_SIZE = 1000;
28+
private static final int BATCH_SIZE = 10000;
2929
private static final int RUNS = 8;
3030

3131
private static final String DB_NAME = "couchbase-test";
@@ -78,28 +78,33 @@ protected void runTests(int entityCount) throws Exception {
7878

7979
long start, time;
8080

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-
8581
// precreate property maps for documents
8682
List<Map<String, Object>> maps = new ArrayList<>(entityCount);
8783
for (int i = 0; i < entityCount; i++) {
8884
maps.add(createDocumentMap(i));
8985
}
86+
System.gc();
87+
88+
runOneByOne(maps, entityCount / 10);
89+
90+
System.gc();
91+
deleteAll();
9092

9193
start = System.currentTimeMillis();
9294
List<Document> documents = new ArrayList<>(entityCount);
95+
database.beginTransaction();
9396
for (int i = 0; i < entityCount; i++) {
9497
// use our own ids (use .createDocument() for random UUIDs)
9598
Document document = database.getDocument(String.valueOf(i));
9699
document.putProperties(maps.get(i));
97100
documents.add(document);
98101
}
102+
database.endTransaction(true);
99103
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");
101105

102106
start = System.currentTimeMillis();
107+
database.beginTransaction();
103108
for (int i = 0; i < entityCount; i++) {
104109
Document document = documents.get(i);
105110
Map<String, Object> updatedProperties = new HashMap<>();
@@ -108,8 +113,9 @@ protected void runTests(int entityCount) throws Exception {
108113
updatedProperties.putAll(maps.get(i));
109114
document.putProperties(updatedProperties);
110115
}
116+
database.endTransaction(true);
111117
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");
113119

114120
start = System.currentTimeMillis();
115121
List<Document> reloaded = new ArrayList<>();
@@ -144,15 +150,45 @@ protected void runTests(int entityCount) throws Exception {
144150
Log.d(TAG, "---------------End: " + entityCount);
145151
}
146152

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+
147181
protected void deleteAll() throws CouchbaseLiteException {
148182
long start = System.currentTimeMillis();
149183
// query all documents, mark them as deleted
150184
Query query = database.createAllDocumentsQuery();
151185
QueryEnumerator result = query.run();
186+
database.beginTransaction();
152187
while (result.hasNext()) {
153188
QueryRow row = result.next();
154189
row.getDocument().delete();
155190
}
191+
database.endTransaction(true);
156192
long time = System.currentTimeMillis() - start;
157193
Log.d(TAG, "Deleted all entities in " + time + " ms");
158194
}

0 commit comments

Comments
 (0)