|
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 { |
@@ -72,18 +73,30 @@ public void map(Map<String, Object> document, Emitter emitter) { |
72 | 73 | } |
73 | 74 | } |
74 | 75 |
|
75 | | - private void indexedStringEntityQueriesRun(View indexedStringView, int count) |
| 76 | + private void indexedStringEntityQueriesRun(View indexedStringView, final int count) |
76 | 77 | throws CouchbaseLiteException { |
77 | 78 | // 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"); |
85 | 99 | } |
86 | | - database.endTransaction(true); |
87 | 100 | log("Built and inserted entities."); |
88 | 101 |
|
89 | 102 | // query for entities by indexed string at random |
@@ -154,36 +167,61 @@ private void oneByOneCrudRun(int count) throws CouchbaseLiteException { |
154 | 167 | deleteAll(); |
155 | 168 | } |
156 | 169 |
|
157 | | - private void batchCrudRun(int count) throws Exception { |
| 170 | + private void batchCrudRun(final int count) throws Exception { |
158 | 171 | // precreate property maps for documents |
159 | | - List<Map<String, Object>> maps = new ArrayList<>(count); |
| 172 | + final List<Map<String, Object>> maps = new ArrayList<>(count); |
160 | 173 | for (int i = 0; i < count; i++) { |
161 | 174 | maps.add(createDocumentMap(i)); |
162 | 175 | } |
163 | 176 |
|
164 | 177 | 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"); |
172 | 199 | } |
173 | | - database.endTransaction(true); |
174 | 200 | stopClock(LogMessage.BATCH_CREATE); |
175 | 201 |
|
176 | 202 | 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"); |
185 | 224 | } |
186 | | - database.endTransaction(true); |
187 | 225 | stopClock(LogMessage.BATCH_UPDATE); |
188 | 226 |
|
189 | 227 | // clear the document cache to force loading properties from the database |
@@ -222,13 +260,23 @@ private void batchCrudRun(int count) throws Exception { |
222 | 260 | private void deleteAll() throws CouchbaseLiteException { |
223 | 261 | // query all documents, mark them as deleted |
224 | 262 | 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"); |
230 | 279 | } |
231 | | - database.endTransaction(true); |
232 | 280 | } |
233 | 281 |
|
234 | 282 | private Map<String, Object> createDocumentMap(int seed) throws CouchbaseLiteException { |
|
0 commit comments