forked from gaolk/graph-database-benchmark
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathkhop.java
executable file
·145 lines (123 loc) · 5.62 KB
/
khop.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import java.util.Iterator;
import java.util.Map;
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.File;
import java.io.FileReader;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import com.arangodb.ArangoCollection;
import com.arangodb.ArangoDBException;
import com.arangodb.ArangoDatabase;
import com.arangodb.ArangoCursor;
import com.arangodb.ArangoDB;
import com.arangodb.ArangoDBException;
import com.arangodb.entity.BaseDocument;
import com.arangodb.entity.CollectionEntity;
import com.arangodb.util.MapBuilder;
import com.arangodb.model.AqlQueryOptions;
class ArangoTask implements Callable<long[]> {
private ArangoDB arangoDB = null;
private ArangoDatabase db = null;
private int depth = 0;
private String root = "";
public ArangoTask(String dbName, int depth, String root){
this.arangoDB = new ArangoDB.Builder().user("root").password("root").build();
this.db = arangoDB.db(dbName);
this.depth = depth;
this.root = root;
}
@Override
public long[] call() throws Exception {
String query = "FOR v IN "+depth+".."+depth+" OUTBOUND 'vertex/"+root+"' edge RETURN distinct v._id";
long startTime = System.nanoTime();
ArangoCursor<String> cursor = db.query(query, null, new AqlQueryOptions().count(true), String.class);
long endTime = System.nanoTime();
long diff = (endTime - startTime)/1000000;
arangoDB.shutdown();
return new long[]{cursor.count(), diff};
}
}
public class khop {
public static void main(String[] args) {
if (args.length == 0 || args.length < 3){
System.out.println("Provide graph name (graph500, twitter), depth (1,2,3,6) AND timeout in seconds");
System.exit(0);
}
String dbName = args[0];
int depth = Integer.parseInt(args[1]);
int timeout = Integer.parseInt(args[2]);
String seedFile = "graph500-22-seed";
if (dbName.equals("twitter")){
seedFile = "twitter_rv.net-seed";
}
try {
// reed seeds
File file = new File(seedFile);
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuffer stringBuffer = new StringBuffer();
String line = bufferedReader.readLine();
String[] roots = line.split(" ");
//file to write results
FileWriter writer = new FileWriter("khopResults_" + dbName + "_" + depth);
writer.write("k-hop query with depth = " + depth + "\n");
writer.write("start vertex,\tneighbor size,\tquery time (in ms)\n");
long totalSize = 0;
double totalTime = 0.0;
int errorQuery = 0;
int totalQuery = 0;
long[] result = new long[0];;
for(String root:roots) {
// for depth 3 qnd 6 only need to run 10 queries
if (depth > 2 && totalQuery == 10){
break;
}
totalQuery++;
ArangoTask arangoTask = new ArangoTask(dbName, depth, root);
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<long[]> future = executor.submit(arangoTask);
try {
System.out.println("Starting...seed=" + root);
result = future.get(timeout, TimeUnit.SECONDS);
System.out.println("Finished!");
} catch (TimeoutException e) {
future.cancel(true);
result = new long[]{-1, -1};
errorQuery++;
System.out.println("TIMEOUT: query terminated!");
} catch (Exception e){
System.out.println("Failed to terminate:" + e.getMessage());
}
executor.shutdownNow();
if (result[0] != -1){
totalSize += result[0];
totalTime += result[1];
}
writer.write(root + ",\t" + Long.toString(result[0]) + ",\t" + Long.toString(result[1]) + "\n");
writer.flush();
}
double avgSize = totalQuery == errorQuery ? -1.0 : (double)totalSize/(double)(totalQuery-errorQuery);
double avgTime = totalQuery == errorQuery ? -1.0 : totalTime/(double)(totalQuery-errorQuery);
System.out.println("===================SUMMARY=================================\n");
System.out.println("Total "+ depth + "-Neighborhood size: " + totalSize);
System.out.println("Total elapsed time, ms: " + totalTime);
System.out.println("Total number of queries: " + totalQuery);
System.out.println("Number of failed queries: " + errorQuery);
System.out.println("Average " + depth + "-Neighborhood size: " + avgSize);
System.out.println("Average query time, ms: " + avgTime);
writer.write("===================SUMMARY=================================\n");
writer.write("Total number of queries:\t" + totalQuery + "\n" + "Total elapsed time, ms:\t" + totalTime + "\n" + "Total Neighborhood size:\t" + totalSize + "\n" + "Total number of failed queries:\t" + errorQuery + "\n" + "Average Neighborhood size:\t" + avgSize + "\n" + "Average query time, ms:\t" + avgTime + "\n");
writer.flush();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Done!");
System.exit(0);
}
}