|
9 | 9 | import com.couchbase.lite.Query; |
10 | 10 | import com.couchbase.lite.QueryEnumerator; |
11 | 11 | import com.couchbase.lite.QueryRow; |
| 12 | +import com.couchbase.lite.TransactionalTask; |
12 | 13 | import com.couchbase.lite.View; |
13 | 14 | import com.couchbase.lite.android.AndroidContext; |
14 | 15 | import de.greenrobot.performance.BasePerfTestCase; |
|
21 | 22 | import java.util.Map; |
22 | 23 |
|
23 | 24 | /** |
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 |
25 | 26 | * https://github.com/couchbaselabs/ToDoLite-Android |
26 | 27 | */ |
27 | 28 | public class PerfTestCouchbase extends BasePerfTestCase { |
@@ -77,18 +78,30 @@ public void map(Map<String, Object> document, Emitter emitter) { |
77 | 78 | } |
78 | 79 | } |
79 | 80 |
|
80 | | - private void indexedStringEntityQueriesRun(View indexedStringView, int count) |
| 81 | + private void indexedStringEntityQueriesRun(View indexedStringView, final int count) |
81 | 82 | throws CouchbaseLiteException { |
82 | 83 | // 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"); |
90 | 104 | } |
91 | | - database.endTransaction(true); |
92 | 105 | log("Built and inserted entities."); |
93 | 106 |
|
94 | 107 | // query for entities by indexed string at random |
@@ -159,36 +172,61 @@ private void oneByOneCrudRun(int count) throws CouchbaseLiteException { |
159 | 172 | deleteAll(); |
160 | 173 | } |
161 | 174 |
|
162 | | - private void batchCrudRun(int count) throws Exception { |
| 175 | + private void batchCrudRun(final int count) throws Exception { |
163 | 176 | // precreate property maps for documents |
164 | | - List<Map<String, Object>> maps = new ArrayList<>(count); |
| 177 | + final List<Map<String, Object>> maps = new ArrayList<>(count); |
165 | 178 | for (int i = 0; i < count; i++) { |
166 | 179 | maps.add(createDocumentMap(i)); |
167 | 180 | } |
168 | 181 |
|
169 | 182 | 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"); |
177 | 204 | } |
178 | | - database.endTransaction(true); |
179 | 205 | stopClock(LogMessage.BATCH_CREATE); |
180 | 206 |
|
181 | 207 | 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"); |
190 | 229 | } |
191 | | - database.endTransaction(true); |
192 | 230 | stopClock(LogMessage.BATCH_UPDATE); |
193 | 231 |
|
194 | 232 | // clear the document cache to force loading properties from the database |
@@ -227,13 +265,23 @@ private void batchCrudRun(int count) throws Exception { |
227 | 265 | private void deleteAll() throws CouchbaseLiteException { |
228 | 266 | // query all documents, mark them as deleted |
229 | 267 | 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"); |
235 | 284 | } |
236 | | - database.endTransaction(true); |
237 | 285 | } |
238 | 286 |
|
239 | 287 | private Map<String, Object> createDocumentMap(int seed) throws CouchbaseLiteException { |
|
0 commit comments