|
| 1 | +/* |
| 2 | + * DISCLAIMER |
| 3 | + * |
| 4 | + * Copyright 2016 ArangoDB GmbH, Cologne, Germany |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + * |
| 18 | + * Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | + */ |
| 20 | + |
| 21 | +package com.arangodb; |
| 22 | + |
| 23 | +import static org.hamcrest.Matchers.is; |
| 24 | +import static org.junit.Assert.assertThat; |
| 25 | + |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.concurrent.CompletableFuture; |
| 28 | +import java.util.concurrent.ExecutionException; |
| 29 | + |
| 30 | +import org.junit.Test; |
| 31 | + |
| 32 | +import com.arangodb.entity.BaseDocument; |
| 33 | +import com.arangodb.entity.DocumentCreateEntity; |
| 34 | +import com.arangodb.entity.DocumentDeleteEntity; |
| 35 | +import com.arangodb.entity.DocumentUpdateEntity; |
| 36 | + |
| 37 | +public class A { |
| 38 | + |
| 39 | + public static class TestTaskResult { |
| 40 | + |
| 41 | + private int successNum = 0; |
| 42 | + private int exceptNum = 0; |
| 43 | + private int failNum = 0; |
| 44 | + |
| 45 | + public void addSuccessNum(final int i) { |
| 46 | + successNum += i; |
| 47 | + } |
| 48 | + |
| 49 | + public void addExceptNum(final int i) { |
| 50 | + exceptNum += i; |
| 51 | + } |
| 52 | + |
| 53 | + public void addFailNum(final int i) { |
| 54 | + failNum += i; |
| 55 | + } |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | + private static final String ENTITY_COLLECTION = "test2017"; |
| 60 | + private ArangoDBAsync arangoDB; |
| 61 | + private ArangoDatabaseAsync database; |
| 62 | + private final int propertyNum = 10; |
| 63 | + private final int threadTestCnt = 50000; |
| 64 | + private final String namePrefix = "test_"; |
| 65 | + private final int nameIdOffset = 5; |
| 66 | + |
| 67 | + @Test |
| 68 | + public void a() { |
| 69 | + check("init", initArangoDB()); |
| 70 | + |
| 71 | + try { |
| 72 | + check("insert", testArangoInsert()); |
| 73 | + check("select", testArangoSelect()); |
| 74 | + check("update", testArangoUpdate()); |
| 75 | + check("select", testArangoSelect()); |
| 76 | + check("delete", testArangoDelete()); |
| 77 | + |
| 78 | + } finally { |
| 79 | + database.collection(ENTITY_COLLECTION).drop(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private void check(final String phase, final TestTaskResult result) { |
| 84 | + System.out.println(String.format("%S: success:%s, fail:%s, exceptions:%s", phase, result.successNum, |
| 85 | + result.failNum, result.exceptNum)); |
| 86 | + assertThat(result.exceptNum, is(0)); |
| 87 | + assertThat(result.failNum, is(0)); |
| 88 | + } |
| 89 | + |
| 90 | + private TestTaskResult initArangoDB() { |
| 91 | + final TestTaskResult result = new TestTaskResult(); |
| 92 | + try { |
| 93 | + arangoDB = new ArangoDBAsync.Builder().build(); |
| 94 | + database = arangoDB.db(); |
| 95 | + |
| 96 | + try { |
| 97 | + arangoDB.db().collection(ENTITY_COLLECTION).drop().get(); |
| 98 | + } catch (InterruptedException | ExecutionException e1) { |
| 99 | + } |
| 100 | + |
| 101 | + database.createCollection(ENTITY_COLLECTION).get(); |
| 102 | + result.addSuccessNum(1); |
| 103 | + } catch (final ArangoDBException | InterruptedException | ExecutionException e) { |
| 104 | + e.printStackTrace(); |
| 105 | + result.addExceptNum(1); |
| 106 | + result.addFailNum(1); |
| 107 | + } |
| 108 | + return result; |
| 109 | + } |
| 110 | + |
| 111 | + private TestTaskResult testArangoInsert() { |
| 112 | + final TestTaskResult result = new TestTaskResult(); |
| 113 | + final ArrayList<CompletableFuture<DocumentCreateEntity<BaseDocument>>> list = new ArrayList<>(threadTestCnt); |
| 114 | + final ArangoCollectionAsync entityCollection = database.collection(ENTITY_COLLECTION); |
| 115 | + |
| 116 | + try { |
| 117 | + for (int i = 0; i < threadTestCnt; i++) { |
| 118 | + final BaseDocument doc = new BaseDocument(); |
| 119 | + doc.setKey(namePrefix + String.valueOf(i + 1 + nameIdOffset)); |
| 120 | + for (int j = 0; j < propertyNum; j++) { |
| 121 | + final String nanoTime = String.valueOf(System.nanoTime()); |
| 122 | + final StringBuilder randomValue = new StringBuilder(); |
| 123 | + |
| 124 | + final String appendProp = "__" + nanoTime + "__" + String.valueOf(i + 1) + "__" |
| 125 | + + String.valueOf(j + 1); |
| 126 | + randomValue.append(appendProp); |
| 127 | + doc.addAttribute("property" + String.valueOf(j), randomValue.toString()); |
| 128 | + } |
| 129 | + |
| 130 | + final CompletableFuture<DocumentCreateEntity<BaseDocument>> f = entityCollection.insertDocument(doc); |
| 131 | + list.add(f); |
| 132 | + } |
| 133 | + } catch (final ArangoDBException e) { |
| 134 | + e.printStackTrace(); |
| 135 | + result.addExceptNum(1); |
| 136 | + } |
| 137 | + |
| 138 | + for (int i = 0; i < threadTestCnt; i++) { |
| 139 | + try { |
| 140 | + final DocumentCreateEntity<BaseDocument> createEntity = list.get(i).get(); |
| 141 | + if (createEntity.getKey() != null) { |
| 142 | + result.addSuccessNum(1); |
| 143 | + } else { |
| 144 | + result.addFailNum(1); |
| 145 | + } |
| 146 | + } catch (InterruptedException | ExecutionException e) { |
| 147 | + result.addFailNum(1); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + return result; |
| 152 | + } |
| 153 | + |
| 154 | + private TestTaskResult testArangoUpdate() { |
| 155 | + final TestTaskResult result = new TestTaskResult(); |
| 156 | + int updatePropNum = propertyNum; |
| 157 | + if (propertyNum >= 10) { |
| 158 | + updatePropNum = propertyNum / 3; |
| 159 | + } |
| 160 | + |
| 161 | + final ArrayList<CompletableFuture<DocumentUpdateEntity<BaseDocument>>> list = new ArrayList<>(threadTestCnt); |
| 162 | + final ArangoCollectionAsync entityCollection = database.collection(ENTITY_COLLECTION); |
| 163 | + try { |
| 164 | + |
| 165 | + for (int i = 0; i < threadTestCnt; i++) { |
| 166 | + final BaseDocument doc = new BaseDocument(); |
| 167 | + doc.setKey(namePrefix + String.valueOf(i + 1 + nameIdOffset)); |
| 168 | + |
| 169 | + for (int j = 0; j < updatePropNum; j++) { |
| 170 | + final String nanoTime = String.valueOf(System.nanoTime()); |
| 171 | + final StringBuilder randomValue = new StringBuilder(); |
| 172 | + randomValue.append("_Update"); |
| 173 | + |
| 174 | + final String appendProp = "__" + nanoTime + "__" + String.valueOf(i + 1) + "__" |
| 175 | + + String.valueOf(j + 1); |
| 176 | + randomValue.append(appendProp); |
| 177 | + doc.addAttribute("property" + String.valueOf(j), randomValue.toString()); |
| 178 | + } |
| 179 | + |
| 180 | + final CompletableFuture<DocumentUpdateEntity<BaseDocument>> f = entityCollection |
| 181 | + .updateDocument(doc.getKey(), doc); |
| 182 | + list.add(f); |
| 183 | + } |
| 184 | + } catch (final ArangoDBException e) { |
| 185 | + e.printStackTrace(); |
| 186 | + result.addExceptNum(1); |
| 187 | + } |
| 188 | + |
| 189 | + for (int i = 0; i < threadTestCnt; i++) { |
| 190 | + try { |
| 191 | + final DocumentUpdateEntity<BaseDocument> updateEntity = list.get(i).get(); |
| 192 | + if (updateEntity.getKey() != null) { |
| 193 | + result.addSuccessNum(1); |
| 194 | + } else { |
| 195 | + result.addFailNum(1); |
| 196 | + } |
| 197 | + } catch (InterruptedException | ExecutionException e) { |
| 198 | + result.addFailNum(1); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + return result; |
| 203 | + } |
| 204 | + |
| 205 | + private TestTaskResult testArangoSelect() { |
| 206 | + final TestTaskResult result = new TestTaskResult(); |
| 207 | + final ArrayList<CompletableFuture<BaseDocument>> list = new ArrayList<>(threadTestCnt); |
| 208 | + final ArangoCollectionAsync entityCollection = database.collection(ENTITY_COLLECTION); |
| 209 | + try { |
| 210 | + for (int i = 0; i < threadTestCnt; i++) { |
| 211 | + final CompletableFuture<BaseDocument> f = entityCollection |
| 212 | + .getDocument(namePrefix + String.valueOf(i + 1 + nameIdOffset), BaseDocument.class); |
| 213 | + list.add(f); |
| 214 | + } |
| 215 | + } catch (final ArangoDBException e) { |
| 216 | + e.printStackTrace(); |
| 217 | + result.addExceptNum(1); |
| 218 | + } |
| 219 | + |
| 220 | + for (int i = 0; i < threadTestCnt; i++) { |
| 221 | + try { |
| 222 | + final BaseDocument document = list.get(i).get(); |
| 223 | + if (document.getKey() != null) { |
| 224 | + result.addSuccessNum(1); |
| 225 | + } else { |
| 226 | + result.addFailNum(1); |
| 227 | + } |
| 228 | + } catch (InterruptedException | ExecutionException e) { |
| 229 | + result.addFailNum(1); |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + return result; |
| 234 | + } |
| 235 | + |
| 236 | + private TestTaskResult testArangoDelete() { |
| 237 | + final TestTaskResult result = new TestTaskResult(); |
| 238 | + final ArrayList<CompletableFuture<DocumentDeleteEntity<Void>>> list = new ArrayList<>(threadTestCnt); |
| 239 | + final ArangoCollectionAsync entityCollection = database.collection(ENTITY_COLLECTION); |
| 240 | + try { |
| 241 | + for (int i = 0; i < threadTestCnt; i++) { |
| 242 | + final CompletableFuture<DocumentDeleteEntity<Void>> f = entityCollection |
| 243 | + .deleteDocument(namePrefix + String.valueOf(i + 1 + nameIdOffset)); |
| 244 | + list.add(f); |
| 245 | + } |
| 246 | + } catch (final ArangoDBException e) { |
| 247 | + e.printStackTrace(); |
| 248 | + result.addExceptNum(1); |
| 249 | + } |
| 250 | + |
| 251 | + for (int i = 0; i < threadTestCnt; i++) { |
| 252 | + try { |
| 253 | + final DocumentDeleteEntity<Void> deleteEntity = list.get(i).get(); |
| 254 | + if (deleteEntity.getKey() != null) { |
| 255 | + result.addSuccessNum(1); |
| 256 | + } else { |
| 257 | + result.addFailNum(1); |
| 258 | + } |
| 259 | + } catch (InterruptedException | ExecutionException e) { |
| 260 | + result.addFailNum(1); |
| 261 | + } |
| 262 | + } |
| 263 | + |
| 264 | + return result; |
| 265 | + } |
| 266 | + |
| 267 | +} |
0 commit comments