Skip to content

Commit 9a87a34

Browse files
author
a-brandt
committed
changed example class to unit test class
1 parent acd7893 commit 9a87a34

File tree

5 files changed

+132
-66
lines changed

5 files changed

+132
-66
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.arangodb.bench;
2+
3+
import java.util.List;
4+
5+
import com.arangodb.ArangoDriver;
6+
7+
public abstract class AbstractBenchmarkImporter {
8+
9+
protected final ArangoDriver driver;
10+
11+
protected final String collectionName;
12+
13+
public AbstractBenchmarkImporter(ArangoDriver driver, String collectionName) {
14+
this.driver = driver;
15+
this.collectionName = collectionName;
16+
}
17+
18+
abstract protected void execute(List<?> values) throws Exception;
19+
20+
public long bench(List<?> values) throws Exception {
21+
long t = System.currentTimeMillis();
22+
execute(values);
23+
return System.currentTimeMillis() - t;
24+
}
25+
26+
}

src/test/java/com/arangodb/bench/BenchmarkImport.java

Lines changed: 50 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -18,95 +18,82 @@
1818

1919
import java.util.List;
2020

21-
import com.arangodb.ArangoConfigure;
21+
import org.junit.Assert;
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
2225
import com.arangodb.ArangoDriver;
2326
import com.arangodb.ArangoException;
2427
import com.arangodb.Station;
28+
import com.arangodb.example.document.BaseExample;
2529
import com.arangodb.util.TestUtils;
2630

2731
/**
32+
* Import a list of objects
33+
*
34+
* 1. Import the data with importDocuments()
35+
*
36+
* 2. Import the data with createDocument()
37+
*
2838
* @author tamtam180 - kirscheless at gmail.com
39+
* @author a-brandt
2940
*
3041
*/
31-
public class BenchmarkImport {
32-
33-
final static String COLLECTION_NAME = "bench-test";
42+
public class BenchmarkImport extends BaseExample {
3443

35-
public static void main(String[] args) throws Exception {
44+
private static final String DATABASE_NAME = "BenchmarkImport";
3645

37-
final int max = 10;
38-
ArangoConfigure configure = new ArangoConfigure();
39-
configure.init();
40-
41-
try {
46+
private static final String COLLECTION_NAME = "BenchmarkImportCollection";
4247

43-
ArangoDriver driver = new ArangoDriver(configure);
44-
List<Station> stations = TestUtils.readStations();
48+
public ArangoDriver arangoDriver;
4549

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);
5153

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+
}
6257

63-
System.out.println("import:" + time1);
64-
System.out.println("document:" + time2);
58+
@Test
59+
public void BenchmarkImportTest() throws Exception {
6560

66-
} finally {
67-
configure.shutdown();
68-
}
61+
final int max = 10;
6962

70-
}
63+
//
64+
printHeadline("read example data");
65+
//
7166

72-
static private abstract class BenchLogic {
73-
protected ArangoDriver driver;
67+
List<Station> stations = TestUtils.readStations();
7468

75-
public BenchLogic(ArangoDriver driver) {
76-
this.driver = driver;
69+
// truncate collection
70+
try {
71+
arangoDriver.truncateCollection(COLLECTION_NAME);
72+
} catch (ArangoException e) {
7773
}
7874

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);
8078

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+
//
8782

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);
9188
}
9289

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");
10395

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);
11097
}
11198

11299
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.arangodb.bench;
2+
3+
import java.util.List;
4+
5+
import com.arangodb.ArangoDriver;
6+
7+
/**
8+
* Import all values with importDocuments()
9+
*
10+
* @author a-brandt
11+
*
12+
*/
13+
public class ImportDocumentBenchmarkImporter extends AbstractBenchmarkImporter {
14+
15+
public ImportDocumentBenchmarkImporter(ArangoDriver driver, String collectionName) {
16+
super(driver, collectionName);
17+
}
18+
19+
@Override
20+
protected void execute(List<?> values) throws Exception {
21+
driver.importDocuments(collectionName, true, values);
22+
}
23+
24+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.arangodb.bench;
2+
3+
import java.util.List;
4+
5+
import com.arangodb.ArangoDriver;
6+
7+
/**
8+
* Import all values with createDocument()
9+
*
10+
* @author a-brandt
11+
*
12+
*/
13+
public class SingleDocumentBenchmarkImporter extends AbstractBenchmarkImporter {
14+
15+
public SingleDocumentBenchmarkImporter(ArangoDriver driver, String collectionName) {
16+
super(driver, collectionName);
17+
}
18+
19+
@Override
20+
protected void execute(List<?> values) throws Exception {
21+
for (Object value : values) {
22+
driver.createDocument(collectionName, value, true, false);
23+
}
24+
}
25+
26+
}

src/test/java/com/arangodb/example/ExamplesTestSuite.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.junit.runners.Suite;
2121
import org.junit.runners.Suite.SuiteClasses;
2222

23+
import com.arangodb.bench.BenchmarkImport;
2324
import com.arangodb.example.document.DocumentExamplesTestSuite;
2425
import com.arangodb.example.graph.GraphExamplesTestSuite;
2526

@@ -32,11 +33,13 @@
3233
@RunWith(Suite.class)
3334
@SuiteClasses({
3435

35-
TransactionExample.class,
36-
3736
DocumentExamplesTestSuite.class,
3837

39-
GraphExamplesTestSuite.class
38+
GraphExamplesTestSuite.class,
39+
40+
BenchmarkImport.class,
41+
42+
TransactionExample.class
4043

4144
})
4245

0 commit comments

Comments
 (0)