forked from gaolk/graph-database-benchmark
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathquery_runner.py
executable file
·93 lines (69 loc) · 2.73 KB
/
query_runner.py
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
############################################################
# Copyright (c) 2015-now, TigerGraph Inc.
# All rights reserved
# It is provided as it is for benchmark reproducible purpose.
# anyone can use it for benchmark purpose with the
# acknowledgement to TigerGraph.
# Author: Mingxi Wu [email protected]
############################################################
import requests
from neo4j.v1 import GraphDatabase, basic_auth
import config
class QueryRunner():
def __init__(self):
pass
def KN(self, root):
pass
def SSSP(self, root):
pass
def PG(self):
pass
def WCC(self):
pass
def LCC(self):
pass
class Neo4jQueryRunner(QueryRunner):
def __init__(self, url = config.NEO4J_BOLT):
QueryRunner.__init__(self)
self.driver = GraphDatabase.driver(url, auth=basic_auth("neo4j", "benchmark"))
self.session = self.driver.session()
def KN(self, root, depth):
try:
# a path of length depth, if a node is 1-hop away, and also 2-hop away, we count it in 2-hop set.
result = self.session.run("match (n1:MyNode)-[:MyEdge*" + str(depth) + "]->(n2:MyNode) where n1.id={root} return count(distinct n2)", {"root":root})
record = result.peek()
except: #timeout, we return -1, reset session
self.session.close()
self.session = self.driver.session()
return -1
else:
return record["count(distinct n2)"]
def PG(self, iteration):
result = self.session.run("MATCH (node:MyNode) WITH COLLECT(node) AS nodes CALL apoc.algo.pageRankWithConfig(nodes,{iterations:{iteration}}) YIELD node, score RETURN node, score LIMIT 1", {"iteration":iteration})
record = result.peek()
return record
def WCC(self):
result = self.session.run("CALL apoc.algo.wcc() yield stats return count(*)")
record = result.peek()
return record
#build index
def Index(self, att, typename):
result = self.session.run("CREATE INDEX ON :{typename}({att});", {"typename":typename, "att":att})
record = result.peek()
class TigerGraphQueryRunner(QueryRunner):
def __init__(self, url = config.TIGERGRAPH_HTTP):
QueryRunner.__init__(self)
self.session = requests.Session()
self.url = url
def KN(self, root, depth):
result = self.session.get(self.url + "/query/khop", params={'start_node': root, "depth":depth}).json()
return result["results"][0]["Start.size()"]
def PG(self, iteration):
result = self.session.get(self.url + "/query/pagerank", params={'iteration': iteration, "dampingFactor":0.8}).json()
print (result)
def WCC(self):
result = self.session.get(self.url + "/query/wcc").json()
print (result)
if __name__ == "__main__":
runner = TigerGraphQueryRunner()
runner.PG(100)