-
Notifications
You must be signed in to change notification settings - Fork 0
/
performance-merge.py
72 lines (64 loc) · 2.22 KB
/
performance-merge.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
from neo4j import GraphDatabase, AsyncGraphDatabase
import neo4j
import random
from time import time, sleep
import asyncio
import os
URI = "neo4j://localhost:7687"
AUTH = ("neo4j", "verysecret")
NODE_N = 10000
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 merge():
case = "merge"
with GraphDatabase.driver(URI, auth=AUTH) as driver:
clear_db(driver)
rows = [random.random() for i in range(NODE_N)]
start_time = time()
for row in rows:
driver.execute_query("MERGE (n:Number {val: $val})", val=row, database_="neo4j")
execution_time = time() - start_time
print(f"** Case {case} **\n"
f"Run time {execution_time} seconds\n")
def create():
case = "create"
with GraphDatabase.driver(URI, auth=AUTH) as driver:
clear_db(driver)
rows = [random.random() for i in range(NODE_N)]
start_time = time()
for row in rows:
driver.execute_query("CREATE (n:Number {val: $val})", val=row, database_="neo4j")
execution_time = time() - start_time
print(f"** Case {case} **\n"
f"Run time {execution_time} seconds\n")
def createnovar():
case = "createnovar"
with GraphDatabase.driver(URI, auth=AUTH) as driver:
clear_db(driver)
rows = [random.random() for i in range(NODE_N)]
start_time = time()
for row in rows:
driver.execute_query("CREATE (:Number {val: $val})", val=row, database_="neo4j")
execution_time = time() - start_time
print(f"** Case {case} **\n"
f"Run time {execution_time} seconds\n")
if __name__ == "__main__":
flush_cache()
createnovar()
flush_cache()
create()