Skip to content

Commit

Permalink
added crud benchmark tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lzoran committed Jun 10, 2018
1 parent 27250c8 commit de72a76
Show file tree
Hide file tree
Showing 7 changed files with 458 additions and 28 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
Expand Down
136 changes: 136 additions & 0 deletions src/main/java/com/graphdatabases/arangodb/ArangoDBBenchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,142 @@ public Iterator<Person> findFriendsOfFriendsOfMostConnectedNode() {
return cursor.iterator();
}

@Benchmark(iteration = 1, priority = 90)
public Person createNewNodeWithNodeId10000() {
Person person = new Person("10000");
arangoDB.db(DB_NAME).graph(GRAPH_NAME).vertexCollection(VERTEXT_COLLECTION_NAME).insertVertex(person);

return person;
}

@Benchmark(iteration = 1, priority = 80)
public boolean createNewRelationshipBetweenMostConnectedNodeAndNodeWithNodeId10000() {
boolean modified = false;

String query = "LET firstNodeId = (FOR p IN Persons FILTER p.nodeId == @firstNodeId RETURN p._id)[0] LET secondNodeId = (FOR p IN Persons FILTER p.nodeId == @secondNodeId RETURN p._id)[0] LET friends = [{ _from: firstNodeId, _to: secondNodeId }, { _from: secondNodeId, _to: firstNodeId }] FOR f IN friends INSERT f IN Friends LET inserted = NEW RETURN inserted";
Map<String, Object> vars = new HashMap<>();
vars.put("firstNodeId", "107");
vars.put("secondNodeId", "10000");
ArangoCursor<Friend> cursor = arangoDB.db(DB_NAME).query(query, vars, null, Friend.class);

modified = cursor.hasNext();

return modified;
}

@Benchmark(iteration = 10, priority = 70)
public Person findNodeWithNodeId10000() {
Person person = null;

String query = "FOR p IN Persons FILTER p.`nodeId` == @nodeId RETURN p";
Map<String, Object> vars = new HashMap<>();
vars.put("nodeId", "10000");
ArangoCursor<Person> cursor = arangoDB.db(DB_NAME).query(query, vars, null, Person.class);
if (cursor.hasNext()) {
person = cursor.next();
}

return person;
}

@Benchmark(iteration = 10, priority = 60)
public Person updateNodeWithNodeId10000() {
Person person = null;

String query = "FOR p IN Persons FILTER p.`nodeId` == @nodeId UPDATE p WITH {firstName: \"John\", lastName: \"Doe\"} IN Persons RETURN NEW";
Map<String, Object> vars = new HashMap<>();
vars.put("nodeId", "10000");
ArangoCursor<Person> cursor = arangoDB.db(DB_NAME).query(query, vars, null, Person.class);
if (cursor.hasNext()) {
person = cursor.next();
}

return person;
}

@Benchmark(iteration = 1, priority = 50)
public Person deleteNodeWithNodeId10000() {
Person person = null;

String query = "FOR p IN Persons FILTER p.`nodeId` == @nodeId REMOVE p IN Persons RETURN OLD";
Map<String, Object> vars = new HashMap<>();
vars.put("nodeId", "10000");
ArangoCursor<Person> cursor = arangoDB.db(DB_NAME).query(query, vars, null, Person.class);
if (cursor.hasNext()) {
person = cursor.next();
}

return person;
}

@Benchmark(iteration = 1, priority = 90)
public Person createNewNodeWithNodeId20000() {
Person person = new Person("20000");
arangoDB.db(DB_NAME).graph(GRAPH_NAME).vertexCollection(VERTEXT_COLLECTION_NAME).insertVertex(person);

return person;
}

@Benchmark(iteration = 1, priority = 80)
public boolean createNewRelationshipBetweenLeastConnectedNodeAndNodeWithNodeId20000() {
boolean modified = false;

String query = "LET firstNodeId = (FOR p IN Persons FILTER p.nodeId == @firstNodeId RETURN p._id)[0] LET secondNodeId = (FOR p IN Persons FILTER p.nodeId == @secondNodeId RETURN p._id)[0] LET friends = [{ _from: firstNodeId, _to: secondNodeId }, { _from: secondNodeId, _to: firstNodeId }] FOR f IN friends INSERT f IN Friends LET inserted = NEW RETURN inserted";
Map<String, Object> vars = new HashMap<>();
vars.put("firstNodeId", "891");
vars.put("secondNodeId", "20000");
ArangoCursor<Friend> cursor = arangoDB.db(DB_NAME).query(query, vars, null, Friend.class);

modified = cursor.hasNext();

return modified;
}

@Benchmark(iteration = 10, priority = 70)
public Person findNodeWithNodeId20000() {
Person person = null;

String query = "FOR p IN Persons FILTER p.`nodeId` == @nodeId RETURN p";
Map<String, Object> vars = new HashMap<>();
vars.put("nodeId", "20000");
ArangoCursor<Person> cursor = arangoDB.db(DB_NAME).query(query, vars, null, Person.class);
if (cursor.hasNext()) {
person = cursor.next();
}

return person;
}

@Benchmark(iteration = 10, priority = 60)
public Person updateNodeWithNodeId20000() {
Person person = null;

String query = "FOR p IN Persons FILTER p.`nodeId` == @nodeId UPDATE p WITH {firstName: \"John\", lastName: \"Doe\"} IN Persons RETURN NEW";
Map<String, Object> vars = new HashMap<>();
vars.put("nodeId", "20000");
ArangoCursor<Person> cursor = arangoDB.db(DB_NAME).query(query, vars, null, Person.class);
if (cursor.hasNext()) {
person = cursor.next();
}

return person;
}

@Benchmark(iteration = 1, priority = 50)
public Person deleteNodeWithNodeId20000() {
Person person = null;

String query = "FOR p IN Persons FILTER p.`nodeId` == @nodeId REMOVE p IN Persons RETURN OLD";
Map<String, Object> vars = new HashMap<>();
vars.put("nodeId", "20000");
ArangoCursor<Person> cursor = arangoDB.db(DB_NAME).query(query, vars, null, Person.class);
if (cursor.hasNext()) {
person = cursor.next();
}

return person;
}

@TearDown
public void tearDown() {
arangoDB.shutdown();
Expand Down
21 changes: 18 additions & 3 deletions src/main/java/com/graphdatabases/arangodb/model/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ public class Person {

@DocumentField(Type.ID)
private String id;

@DocumentField(Type.KEY)
private String key;

@DocumentField(Type.REV)
private String revision;

private String nodeId;
private String firstName;
private String lastName;

public Person() {}

Expand Down Expand Up @@ -53,4 +52,20 @@ public String getNodeId() {
public void setNodeId(String nodeId) {
this.nodeId = nodeId;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}
}
35 changes: 28 additions & 7 deletions src/main/java/com/graphdatabases/benchmark/BenchmarkTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.*;
import java.util.concurrent.TimeUnit;

public class BenchmarkTest {
Expand All @@ -26,20 +25,30 @@ public void run() {
try {
Object object = clazz.newInstance();

ArrayList<Method> setupMethods = new ArrayList();
ArrayList<Method> tearDownMethods = new ArrayList();
ArrayList<Method> benchmarkMethods = new ArrayList();
List<Method> setupMethods = new ArrayList<>();
List<Method> tearDownMethods = new ArrayList<>();
Map<Integer, List<Method>> benchmarkMethodsGroupedByPriority = new HashMap<>();

Method[] methods = clazz.getMethods();
for (Method method : methods) {
if (method.isAnnotationPresent(Setup.class)) {
setupMethods.add(method);
} else if (method.isAnnotationPresent(Benchmark.class)) {
benchmarkMethods.add(method);
Benchmark benchmark = method.getAnnotation(Benchmark.class);

if (benchmarkMethodsGroupedByPriority.containsKey(benchmark.priority())) {
benchmarkMethodsGroupedByPriority.get(benchmark.priority()).add(method);
} else {
List<Method> groupedMethods = new ArrayList<>();
groupedMethods.add(method);

benchmarkMethodsGroupedByPriority.put(benchmark.priority(), groupedMethods);
}
} else if (method.isAnnotationPresent(TearDown.class)) {
tearDownMethods.add(method);
}
}
List<Method> benchmarkMethods = sortBenchmarkMethodsByPriority(benchmarkMethodsGroupedByPriority);

// setup
validateSetupMethods(setupMethods);
Expand All @@ -52,7 +61,7 @@ public void run() {
for (Method method : benchmarkMethods) {
Benchmark benchmark = method.getAnnotation(Benchmark.class);

List<Long> times = new ArrayList();
List<Long> times = new ArrayList<>();
for (int i = 0; i < benchmark.iteration(); i++) {
Stopwatch stopwatch = Stopwatch.createStarted();
method.invoke(object);
Expand All @@ -79,6 +88,18 @@ public void run() {
System.out.println(String.format("%s: Benchmark finished.", clazz.getName()));
}

private List<Method> sortBenchmarkMethodsByPriority(Map<Integer, List<Method>> map) {
List<Method> sortedMethods = new ArrayList<>();

List<Integer> keys = new ArrayList<>(map.keySet());
Collections.sort(keys, Collections.reverseOrder());
for (Integer key : keys) {
sortedMethods.addAll(map.get(key));
}

return sortedMethods;
}

private void validateSetupMethods(List<Method> methods) {
if (methods.size() > 1) {
throw new BenchmarkException("Only one method can be marked with Setup annotation.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@
public @interface Benchmark {

int iteration() default 1;

int priority() default 100;
}
Loading

0 comments on commit de72a76

Please sign in to comment.