|
18 | 18 |
|
19 | 19 | import java.util.List; |
20 | 20 |
|
21 | | -import com.arangodb.ArangoConfigure; |
| 21 | +import org.junit.Assert; |
| 22 | +import org.junit.Before; |
| 23 | +import org.junit.Test; |
| 24 | + |
22 | 25 | import com.arangodb.ArangoDriver; |
23 | 26 | import com.arangodb.ArangoException; |
24 | 27 | import com.arangodb.Station; |
| 28 | +import com.arangodb.example.document.BaseExample; |
25 | 29 | import com.arangodb.util.TestUtils; |
26 | 30 |
|
27 | 31 | /** |
| 32 | + * Import a list of objects |
| 33 | + * |
| 34 | + * 1. Import the data with importDocuments() |
| 35 | + * |
| 36 | + * 2. Import the data with createDocument() |
| 37 | + * |
28 | 38 | * @author tamtam180 - kirscheless at gmail.com |
| 39 | + * @author a-brandt |
29 | 40 | * |
30 | 41 | */ |
31 | | -public class BenchmarkImport { |
32 | | - |
33 | | - final static String COLLECTION_NAME = "bench-test"; |
| 42 | +public class BenchmarkImport extends BaseExample { |
34 | 43 |
|
35 | | - public static void main(String[] args) throws Exception { |
| 44 | + private static final String DATABASE_NAME = "BenchmarkImport"; |
36 | 45 |
|
37 | | - final int max = 10; |
38 | | - ArangoConfigure configure = new ArangoConfigure(); |
39 | | - configure.init(); |
40 | | - |
41 | | - try { |
| 46 | + private static final String COLLECTION_NAME = "BenchmarkImportCollection"; |
42 | 47 |
|
43 | | - ArangoDriver driver = new ArangoDriver(configure); |
44 | | - List<Station> stations = TestUtils.readStations(); |
| 48 | + public ArangoDriver arangoDriver; |
45 | 49 |
|
46 | | - // truncate collection |
47 | | - try { |
48 | | - driver.truncateCollection(COLLECTION_NAME); |
49 | | - } catch (ArangoException e) { |
50 | | - } |
| 50 | + @Before |
| 51 | + public void _before() { |
| 52 | + removeTestDatabase(DATABASE_NAME); |
51 | 53 |
|
52 | | - // Bench import |
53 | | - BenchLogic logic1 = new BenchImport(driver); |
54 | | - BenchLogic logic2 = new BenchDocument(driver); |
55 | | - |
56 | | - // Bench create document |
57 | | - long time1 = 0, time2 = 0; |
58 | | - for (int i = 0; i < max; i++) { |
59 | | - time1 += logic1.bench(stations); |
60 | | - time2 += logic2.bench(stations); |
61 | | - } |
| 54 | + arangoDriver = getArangoDriver(getConfiguration()); |
| 55 | + createDatabase(arangoDriver, DATABASE_NAME); |
| 56 | + } |
62 | 57 |
|
63 | | - System.out.println("import:" + time1); |
64 | | - System.out.println("document:" + time2); |
| 58 | + @Test |
| 59 | + public void BenchmarkImportTest() throws Exception { |
65 | 60 |
|
66 | | - } finally { |
67 | | - configure.shutdown(); |
68 | | - } |
| 61 | + final int max = 10; |
69 | 62 |
|
70 | | - } |
| 63 | + // |
| 64 | + printHeadline("read example data"); |
| 65 | + // |
71 | 66 |
|
72 | | - static private abstract class BenchLogic { |
73 | | - protected ArangoDriver driver; |
| 67 | + List<Station> stations = TestUtils.readStations(); |
74 | 68 |
|
75 | | - public BenchLogic(ArangoDriver driver) { |
76 | | - this.driver = driver; |
| 69 | + // truncate collection |
| 70 | + try { |
| 71 | + arangoDriver.truncateCollection(COLLECTION_NAME); |
| 72 | + } catch (ArangoException e) { |
77 | 73 | } |
78 | 74 |
|
79 | | - abstract protected void execute(List<?> values) throws Exception; |
| 75 | + // create importer |
| 76 | + AbstractBenchmarkImporter logic1 = new ImportDocumentBenchmarkImporter(arangoDriver, COLLECTION_NAME); |
| 77 | + AbstractBenchmarkImporter logic2 = new SingleDocumentBenchmarkImporter(arangoDriver, COLLECTION_NAME); |
80 | 78 |
|
81 | | - public long bench(List<?> values) throws Exception { |
82 | | - long t = System.currentTimeMillis(); |
83 | | - execute(values); |
84 | | - return System.currentTimeMillis() - t; |
85 | | - } |
86 | | - } |
| 79 | + // |
| 80 | + printHeadline("import data"); |
| 81 | + // |
87 | 82 |
|
88 | | - static class BenchImport extends BenchLogic { |
89 | | - public BenchImport(ArangoDriver driver) { |
90 | | - super(driver); |
| 83 | + // Bench import and create document |
| 84 | + long time1 = 0, time2 = 0; |
| 85 | + for (int i = 0; i < max; i++) { |
| 86 | + time1 += logic1.bench(stations); |
| 87 | + time2 += logic2.bench(stations); |
91 | 88 | } |
92 | 89 |
|
93 | | - @Override |
94 | | - protected void execute(List<?> values) throws Exception { |
95 | | - driver.importDocuments(COLLECTION_NAME, true, values); |
96 | | - } |
97 | | - } |
98 | | - |
99 | | - static class BenchDocument extends BenchLogic { |
100 | | - public BenchDocument(ArangoDriver driver) { |
101 | | - super(driver); |
102 | | - } |
| 90 | + // |
| 91 | + printHeadline("results"); |
| 92 | + // |
| 93 | + System.out.println("importDocuments(): " + time1 + " ms"); |
| 94 | + System.out.println("createDocument(): " + time2 + " ms"); |
103 | 95 |
|
104 | | - @Override |
105 | | - protected void execute(List<?> values) throws Exception { |
106 | | - for (Object value : values) { |
107 | | - driver.createDocument("bench-test", value, true, false); |
108 | | - } |
109 | | - } |
| 96 | + Assert.assertTrue(time1 < time2); |
110 | 97 | } |
111 | 98 |
|
112 | 99 | } |
0 commit comments