forked from ldbc/ldbc_snb_bi
-
Notifications
You must be signed in to change notification settings - Fork 3
/
benchmark.py
282 lines (228 loc) · 11.6 KB
/
benchmark.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
from datetime import datetime, date
from dateutil.relativedelta import relativedelta
import time
import sys
import os
import csv
import datetime
from dateutil.relativedelta import relativedelta
import os
import re
import neo4j
import time
import sys
# Usage: benchmark.py [--test|--pgtuning]
result_mapping = {
1: ["INT32", "BOOL", "INT32", "INT32", "FLOAT32", "INT32", "FLOAT32"],
2: ["STRING", "INT32", "INT32", "INT32"],
3: ["ID", "STRING", "DATETIME", "ID", "INT32"],
4: ["ID", "STRING", "STRING", "DATETIME", "INT32"],
5: ["ID", "INT32", "INT32", "INT32", "INT32"],
6: ["ID", "INT32"],
7: ["STRING", "INT32"],
8: ["ID", "INT32", "INT32"],
9: ["ID", "STRING", "STRING", "INT32", "INT32"],
10: ["ID", "STRING", "INT32"],
11: ["INT64"],
12: ["INT32", "INT32"],
13: ["ID", "INT32", "INT32", "FLOAT32"],
14: ["ID", "ID", "STRING", "INT32"],
15: ["FLOAT32"],
16: ["ID", "INT32", "INT32"],
17: ["ID", "INT32"],
18: ["ID", "ID", "INT32"],
19: ["ID", "ID", "FLOAT32"],
20: ["ID", "INT64"],
}
def convert_value_to_string(value, result_type, input):
if result_type == "ID[]" or result_type == "INT[]" or result_type == "INT32[]" or result_type == "INT64[]":
return "[" + ",".join([str(int(x)) for x in value]) + "]"
elif result_type == "ID" or result_type == "INT" or result_type == "INT32" or result_type == "INT64":
return str(int(value))
elif result_type == "FLOAT" or result_type == "FLOAT32" or result_type == "FLOAT64":
return str(float(value))
elif result_type == "STRING[]":
return "[" + ",".join([f'"{v}"' for v in value]) + "]"
elif result_type == "STRING":
return f'"{value}"'
elif result_type == "DATETIME":
if input:
return f"{datetime.datetime.strftime(value, '%Y-%m-%dT%H:%M:%S.%f')[:-3]}+00:00"
else:
return f"{datetime.datetime.strftime(value.to_native(), '%Y-%m-%dT%H:%M:%S.%f')[:-3]}+00:00"
elif result_type == "DATE":
if input:
return datetime.datetime.strftime(value, '%Y-%m-%d')
else:
return datetime.datetime.strftime(value.to_native(), '%Y-%m-%d')
elif result_type == "BOOL":
return str(bool(value))
else:
raise ValueError(f"Result type {result_type} not found")
def cast_parameter_to_driver_input(value, parameter_type):
if parameter_type == "ID[]" or parameter_type == "INT[]" or parameter_type == "INT32[]" or parameter_type == "INT64[]":
return [int(x) for x in value.split(";")]
elif parameter_type == "ID" or parameter_type == "INT" or parameter_type == "INT32" or parameter_type == "INT64":
return int(value)
elif parameter_type == "STRING[]":
return value.split(";")
elif parameter_type == "STRING":
return value
elif parameter_type == "DATETIME":
dt = datetime.datetime.strptime(value, '%Y-%m-%dT%H:%M:%S.%f+00:00')
return datetime.datetime(dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, dt.microsecond*1000, tzinfo=datetime.timezone.utc)
elif parameter_type == "DATE":
dt = datetime.datetime.strptime(value, '%Y-%m-%d')
return datetime.datetime(dt.year, dt.month, dt.day, tzinfo=datetime.timezone.utc)
else:
raise ValueError(f"Parameter type {parameter_type} not found")
def read_query_fun(tx, query_num, query_spec, query_parameters):
results = tx.run(query_spec, query_parameters)
mapping = result_mapping[query_num]
result_tuples = "[" + ";".join([
f'<{",".join([convert_value_to_string(result[i], value_type, False) for i, value_type in enumerate(mapping)])}>'
for result in results
]) + "]"
return result_tuples
def write_query_fun(tx, query_spec):
tx.run(query_spec, {})
def run_query(session, query_num, query_variant, query_spec, query_parameters, test):
if test:
print(f'Q{query_variant}: {query_parameters}')
start = time.time()
# some queries use temporary labels, hence the need for write_transaction
results = session.write_transaction(read_query_fun, query_num, query_spec, query_parameters)
end = time.time()
duration = end - start
if test:
print(f"-> {duration:.4f} seconds")
print(f"-> {results}")
return (results, duration)
def run_queries(query_variants, session, sf, test, pgtuning):
for query_variant in query_variants:
query_num = int(re.sub("[^0-9]", "", query_variant))
query_subvariant = re.sub("[^ab]", "", query_variant)
print(f"========================= Q {query_num:02d}{query_subvariant.rjust(1)} =========================")
query_file = open(f'queries/bi-{query_num}.cypher', 'r')
query_spec = query_file.read()
parameters_csv = csv.DictReader(open(f'../parameters/bi-{query_variant}.csv'), delimiter='|')
parameters = [{"name": t[0], "type": t[1]} for t in [f.split(":") for f in parameters_csv.fieldnames]]
i = 0
for query_parameters in parameters_csv:
i = i + 1
query_parameters_converted = {k.split(":")[0]: cast_parameter_to_driver_input(v, k.split(":")[1]) for k, v in query_parameters.items()}
query_parameters_split = {k.split(":")[0]: v for k, v in query_parameters.items()}
query_parameters_in_order = f'<{";".join([query_parameters_split[parameter["name"]] for parameter in parameters])}>'
(results, duration) = run_query(session, query_num, query_variant, query_spec, query_parameters_converted, test)
timings_file.write(f"Neo4j|{sf}|{query_variant}|{query_parameters_in_order}|{duration}\n")
timings_file.flush()
results_file.write(f"{query_num}|{query_variant}|{query_parameters_in_order}|{results}\n")
results_file.flush()
# - test run: 1 query
# - regular run: 10 queries
# - paramgen tuning: 50 queries
if (test) or (not pgtuning and i == 10) or (pgtuning and i == 100):
break
query_file.close()
def write_txn_fun(tx, query_spec, batch, csv_file):
result = tx.run(query_spec, batch=batch, csv_file=csv_file)
return result.value()
def run_update(session, query_spec, batch, csv_file):
start = time.time()
result = session.write_transaction(write_txn_fun, query_spec, batch, csv_file)
end = time.time()
duration = end - start
num_changes = result[0]
return num_changes
def run_batch_updates(session, data_dir, batch_start_date, insert_entities, delete_entities, insert_queries, delete_queries):
# format date to yyyy-mm-dd
batch_id = batch_start_date.strftime('%Y-%m-%d')
batch_dir = f"batch_id={batch_id}"
print(f"#################### {batch_dir} ####################")
print("## Inserts")
for entity in insert_entities:
batch_path = f"{data_dir}/inserts/dynamic/{entity}/{batch_dir}"
if not os.path.exists(batch_path):
continue
print(f"{entity}:")
for csv_file in [f for f in os.listdir(batch_path) if f.endswith('.csv') or f.endswith('.csv.gz')]:
print(f"- {entity}/{batch_dir}/{csv_file}")
num_changes = run_update(session, insert_queries[entity], batch_dir, csv_file)
if num_changes == 0:
print("!!! No changes occured")
else:
print(f"> {num_changes} changes")
print()
print("## Deletes")
for entity in delete_entities:
batch_path = f"{data_dir}/deletes/dynamic/{entity}/{batch_dir}"
if not os.path.exists(batch_path):
continue
print(f"{entity}:")
for csv_file in [f for f in os.listdir(batch_path) if f.endswith('.csv') or f.endswith('.csv.gz')]:
print(f"- {entity}/{batch_dir}/{csv_file}")
num_changes = run_update(session, delete_queries[entity], batch_dir, csv_file)
if num_changes == 0:
print("!!! No changes occured")
else:
print(f"> {num_changes} changes")
print()
query_variants = ["1", "2a", "2b", "3", "4", "5", "6", "7", "8a", "8b", "9", "10a", "10b", "11", "12", "13", "14a", "14b", "15a", "15b", "16a", "16b", "17", "18", "19a", "19b", "20a", "20b"]
driver = neo4j.GraphDatabase.driver("bolt://localhost:7687")
session = driver.session()
# env vars and arguments
sf = os.environ.get("SF")
test = False
pgtuning = False
if len(sys.argv) > 1:
if sys.argv[1] == "--test":
test = True
if sys.argv[1] == "--pgtuning":
pgtuning = True
data_dir = os.environ.get("NEO4J_CSV_DIR")
if data_dir is None:
print("${NEO4J_CSV_DIR} environment variable must be set")
exit(1)
print(f"- Input data directory, ${{NEO4J_CSV_DIR}}: {data_dir}")
# to ensure that all inserted edges have their endpoints at the time of their insertion, we insert nodes first and edges second
insert_nodes = ["Comment", "Forum", "Person", "Post"]
insert_edges = ["Comment_hasCreator_Person", "Comment_hasTag_Tag", "Comment_isLocatedIn_Country", "Comment_replyOf_Comment", "Comment_replyOf_Post", "Forum_containerOf_Post", "Forum_hasMember_Person", "Forum_hasModerator_Person", "Forum_hasTag_Tag", "Person_hasInterest_Tag", "Person_isLocatedIn_City", "Person_knows_Person", "Person_likes_Comment", "Person_likes_Post", "Person_studyAt_University", "Person_workAt_Company", "Post_hasCreator_Person", "Post_hasTag_Tag", "Post_isLocatedIn_Country"]
insert_entities = insert_nodes + insert_edges
# set the order of deletions to reflect the dependencies between node labels (:Comment)-[:REPLY_OF]->(:Post)<-[:CONTAINER_OF]-(:Forum)-[:HAS_MODERATOR]->(:Person)
delete_nodes = ["Comment", "Post", "Forum", "Person"]
delete_edges = ["Forum_hasMember_Person", "Person_knows_Person", "Person_likes_Comment", "Person_likes_Post"]
delete_entities = delete_nodes + delete_edges
insert_queries = {}
for entity in insert_entities:
with open(f"dml/ins-{entity}.cypher", "r") as insert_query_file:
insert_queries[entity] = insert_query_file.read()
delete_queries = {}
for entity in delete_entities:
with open(f"dml/del-{entity}.cypher", "r") as delete_query_file:
delete_queries[entity] = delete_query_file.read()
open(f"output/results.csv", "w").close()
open(f"output/timings.csv", "w").close()
results_file = open(f"output/results.csv", "a")
timings_file = open(f"output/timings.csv", "a")
timings_file.write(f"tool|sf|q|parameters|time\n")
network_start_date = datetime.date(2012, 11, 29)
network_end_date = datetime.date(2013, 1, 1)
batch_size = relativedelta(days=1)
batch_date = network_start_date
# run alternating write-read blocks
while batch_date < network_end_date and (not test or batch_date < datetime.date(2012, 12, 2)):
print()
print(f"----------------> Batch date: {batch_date} <---------------")
run_batch_updates(session, data_dir, batch_date, insert_entities, delete_entities, insert_queries, delete_queries)
if "19a" in query_variants or "19b" in query_variants:
print("Creating graph (precomputing weights) for Q19")
session.write_transaction(write_query_fun, open(f'queries/bi-19-drop-graph.cypher', 'r').read())
session.write_transaction(write_query_fun, open(f'queries/bi-19-create-graph.cypher', 'r').read())
if "20a" in query_variants or "20b" in query_variants:
print("Creating graph (precomputing weights) for Q20")
session.write_transaction(write_query_fun, open(f'queries/bi-20-drop-graph.cypher', 'r').read())
session.write_transaction(write_query_fun, open(f'queries/bi-20-create-graph.cypher', 'r').read())
run_queries(query_variants, session, sf, test, pgtuning)
batch_date = batch_date + batch_size
results_file.close()
timings_file.close()