-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperformance.py
195 lines (161 loc) · 6.58 KB
/
performance.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
from neo4j import GraphDatabase, AsyncGraphDatabase
import neo4j
import random
from time import time, sleep
import asyncio
import os
URI = "neo4j://localhost:7687"
AUTH = ("neo4j", "secret")
NODE_N = 5000
def flush_cache():
return
with GraphDatabase.driver(URI, auth=AUTH) as driver:
driver.execute_query("CALL db.clearQueryCaches()")
#return
os.system("docker restart neo4j5-nightly")
while True:
with GraphDatabase.driver(URI, auth=AUTH) as driver:
try:
driver.verify_connectivity()
return
except Exception:
sleep(2)
def clear_db(driver):
with driver.session() as session:
session.run("MATCH (a)-[r]-() DETACH DELETE a,r")
session.run("MATCH (n) DETACH DELETE n")
def execute_transaction(tx, number):
tx.run("CREATE (:Number {value: $value})",
value=number)
async def async_clear_db(driver):
async with driver.session() as session:
await session.run("MATCH (a)-[r]-() DETACH DELETE a,r")
await session.run("MATCH (n) DETACH DELETE n")
async def async_execute_transaction(tx, number):
await tx.run("CREATE (:Number {value: $value})",
value=number)
def exec_query_without_db():
case = "execute_query, no database selection"
with GraphDatabase.driver(URI, auth=AUTH) as driver:
driver.verify_connectivity()
clear_db(driver)
start_time = time()
for _ in range(NODE_N):
driver.execute_query("CREATE (:Number {value: $value})",
value=random.random())
execution_time = time() - start_time
print(f"** Case {case} **\n"
f"Run time {execution_time} seconds\n")
def exec_query_with_db():
case = "execute_query, with database selection"
with GraphDatabase.driver(URI, auth=AUTH) as driver:
driver.verify_connectivity()
clear_db(driver)
start_time = time()
for _ in range(NODE_N):
driver.execute_query("CREATE (:Number {value: $value})",
value=random.random(),
database_="neo4j")
execution_time = time() - start_time
print(f"** Case {case} **\n"
f"Run time {execution_time} seconds\n")
def transaction_func_without_db():
case = "transaction functions, without database selection"
with GraphDatabase.driver(URI, auth=AUTH) as driver:
driver.verify_connectivity()
clear_db(driver)
start_time = time()
for _ in range(NODE_N):
with driver.session() as session:
session.execute_write(execute_transaction, random.random())
execution_time = time() - start_time
print(f"** Case {case} **\n"
f"Run time {execution_time} seconds\n")
def transaction_func_with_db():
case = "transaction functions, with database selection"
with GraphDatabase.driver(URI, auth=AUTH) as driver:
driver.verify_connectivity()
clear_db(driver)
start_time = time()
for _ in range(NODE_N):
with driver.session(database="neo4j") as session:
session.execute_write(execute_transaction, random.random())
execution_time = time() - start_time
print(f"** Case {case} **\n"
f"Run time {execution_time} seconds\n")
async def async_exec_query_with_db():
case = "async execute_query, with database selection"
async with AsyncGraphDatabase.driver(URI, auth=AUTH) as driver:
await driver.verify_connectivity()
await async_clear_db(driver)
start_time = time()
for _ in range(NODE_N):
await driver.execute_query("CREATE (:Number {value: $value})",
value=random.random(),
database_="neo4j")
execution_time = time() - start_time
print(f"** Case {case} **\n"
f"Run time {execution_time} seconds\n")
async def async_exec_query_without_db():
case = "async execute_query, without database selection"
async with AsyncGraphDatabase.driver(URI, auth=AUTH) as driver:
await driver.verify_connectivity()
await async_clear_db(driver)
start_time = time()
for _ in range(NODE_N):
await driver.execute_query("CREATE (:Number {value: $value})",
value=random.random())
execution_time = time() - start_time
print(f"** Case {case} **\n"
f"Run time {execution_time} seconds\n")
def execute_query_batch():
case = "execute_query with batching"
with GraphDatabase.driver(URI, auth=AUTH) as driver:
driver.verify_connectivity()
clear_db(driver)
numbers = [{"value": random.random()} for i in range(NODE_N)]
start_time = time()
driver.execute_query("WITH $numbers as batch "
"UNWIND batch as node "
"CREATE (n:Number) "
"SET n += node",
numbers=numbers,
database_="neo4j")
execution_time = time() - start_time
print(f"** Case {case} **\n"
f"Run time {execution_time} seconds\n")
async def async_execute_query_batch():
case = "async execute_query with split batching"
async with AsyncGraphDatabase.driver(URI, auth=AUTH) as driver:
await driver.verify_connectivity()
await async_clear_db(driver)
split = 10
numbers = [[{"value": random.random()} for i in range(NODE_N//split)] for x in range(split)]
start_time = time()
for number_list in numbers:
await driver.execute_query("WITH $numbers as batch "
"UNWIND batch as node "
"CREATE (n:Number) "
"SET n += node",
numbers=number_list,
database_="neo4j")
execution_time = time() - start_time
print(f"** Case {case} **\n"
f"Run time {execution_time} seconds\n")
if __name__ == "__main__":
flush_cache()
exec_query_without_db()
flush_cache()
exec_query_with_db()
flush_cache()
transaction_func_without_db()
flush_cache()
transaction_func_with_db()
flush_cache()
asyncio.run(async_exec_query_without_db())
flush_cache()
asyncio.run(async_exec_query_with_db())
flush_cache()
execute_query_batch()
flush_cache()
asyncio.run(async_execute_query_batch())