forked from unum-cloud/NetworkXum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP3Bench.py
335 lines (297 loc) · 9.49 KB
/
P3Bench.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import os
from time import time
from pystats2md.stats_file import StatsFile
from pystats2md.micro_bench import MicroBench
from networkxum.BaseAPI import BaseAPI
from P0Config import P0Config
from P3TasksSampler import P3TasksSampler
from networkxum.helpers import *
class P3Bench(object):
"""
Benchmarks groups of operations in following order:
2. Edge lookups and simple queries.
3. A few complex analytical queries.
4. Modifications: removing and restoring same objects.
5. Clearing all the data (if needed).
"""
def __init__(self, max_seconds_per_query=60):
self.conf = P0Config.shared()
self.max_seconds_per_query = max_seconds_per_query
self.tasks = P3TasksSampler()
def run(self, repeat_existing=False):
self.repeat_existing = repeat_existing
for dataset in self.conf.datasets:
if not dataset['enabled']:
continue
dataset_path = self.conf.normalize_path(dataset['path'])
self.tasks.sample_file(dataset_path)
for db in self.conf.databases:
self.gdb = self.conf.make_db(database=db, dataset=dataset)
self.database = db
self.dataset = dataset
self.bench_buffered_graph()
self.conf.default_stats_file.dump_to_file()
def bench_buffered_graph(self, remove_all_afterwards=False):
if self.gdb is None:
return
is_in_ram = bool(type(self.gdb).__in_memory__)
if (self.gdb.number_of_edges() == 0) and (not is_in_ram):
return
print('- Benchmarking: {} @ {}'.format(
self.dataset['name'],
self.database['name']
))
if is_in_ram:
self.bench_task(
name='Sequential Writes: Import CSV',
func=self.import_bulk
)
# Streaming edges.
# self.bench_task(
# name='Sequential Reads: Streaming Edges',
# func=self.stream_es
# )
# self.bench_task(
# name='Sequential Reads: Streaming Nodes',
# func=self.stream_ns
# )
# Queries returning collections.
self.bench_task(
name='Random Reads: Find Specific Edge',
func=self.find_e
)
self.bench_task(
name='Random Reads: Find Ingoing Edges',
func=self.find_es_to
)
self.bench_task(
name='Random Reads: Find Connected Edges',
func=self.find_es_related
)
self.bench_task(
name='Random Reads: Find Friends',
func=self.find_vs_related
)
# Queries returning stats.
self.bench_task(
name='Random Reads: Count Friends',
func=self.count_v_related
)
self.bench_task(
name='Random Reads: Count Followers',
func=self.count_v_followers
)
# Reversable write operations.
self.bench_task(
name='Random Writes: Remove Edge',
func=self.remove_e
)
self.bench_task(
name='Random Writes: Upsert Edge',
func=self.upsert_e
)
self.bench_task(
name='Random Writes: Remove Edges Batch',
func=self.remove_es
)
self.bench_task(
name='Random Writes: Upsert Edges Batch',
func=self.upsert_es
)
if remove_all_afterwards:
self.bench_task(
name='Random Writes: Remove Vertex',
func=self.remove_v
)
self.bench_task(
name='Sequential Writes: Remove All',
func=self.remove_bulk
)
def bench_task(self, name, func):
dataset_name = self.dataset['name']
db_name = self.database['name']
print(f'--- {db_name}: {name} @ {dataset_name}')
counter = MicroBench(
benchmark_name=name,
func=func,
database=db_name,
dataset=dataset_name,
source=self.conf.default_stats_file,
device_name=self.conf.device_name,
limit_iterations=1,
limit_seconds=None,
limit_operations=None,
)
if not self.repeat_existing:
if self.conf.default_stats_file.contains(counter):
print('--- Skipping!')
return
print('---- Running!')
counter.run()
if counter.count_operations == 0:
print('---- Didn\'t measure!')
return
print('---- Importing new stats!')
# ---
# Operations
# ---
def stream_es(self) -> int:
cnt = 0
for e in self.gdb.edges:
cnt += 1
return cnt
def stream_ns(self) -> int:
cnt = 0
for n in self.gdb.nodes:
cnt += 1
return cnt
def find_e(self) -> int:
cnt = 0
cnt_found = 0
t0 = time()
for e in self.tasks.edges_to_query:
match = self.gdb.has_edge(e.first, e.second)
cnt += 1
cnt_found += 0 if (match is None) else 1
dt = time() - t0
if dt > self.max_seconds_per_query:
break
print(f'---- {cnt} ops: {cnt_found} undirected matches')
return cnt
def find_es_related(self) -> int:
cnt = 0
cnt_found = 0
t0 = time()
for v in self.tasks.nodes_to_query:
es = self.gdb.has_edge(v, v)
cnt += 1
cnt_found += len(es)
dt = time() - t0
if dt > self.max_seconds_per_query:
break
print(f'---- {cnt} ops: {cnt_found} edges found')
return cnt
def find_es_from(self) -> int:
cnt = 0
cnt_found = 0
t0 = time()
for v in self.tasks.nodes_to_query:
es = self.gdb.has_edge(v, None)
cnt += 1
cnt_found += len(es)
dt = time() - t0
if dt > self.max_seconds_per_query:
break
print(f'---- {cnt} ops: {cnt_found} edges found')
return cnt
def find_es_to(self) -> int:
cnt = 0
cnt_found = 0
t0 = time()
for v in self.tasks.nodes_to_query:
es = self.gdb.has_edge(None, v)
cnt += 1
cnt_found += len(es)
dt = time() - t0
if dt > self.max_seconds_per_query:
break
print(f'---- {cnt} ops: {cnt_found} edges found')
return cnt
def find_vs_related(self) -> int:
cnt = 0
cnt_found = 0
t0 = time()
for v in self.tasks.nodes_to_query:
vs = self.gdb.neighbors(v)
cnt += 1
cnt_found += len(vs)
dt = time() - t0
if dt > self.max_seconds_per_query:
break
print(f'---- {cnt} ops: {cnt_found} related nodes')
return cnt
def count_v_related(self) -> int:
cnt = 0
t0 = time()
for v in self.tasks.nodes_to_query:
self.gdb.number_of_edges(v, v)
cnt += 1
dt = time() - t0
if dt > self.max_seconds_per_query:
break
return cnt
def count_v_followers(self) -> int:
cnt = 0
t0 = time()
for v in self.tasks.nodes_to_query:
self.gdb.number_of_edges(None, v)
cnt += 1
dt = time() - t0
if dt > self.max_seconds_per_query:
break
return cnt
def count_v_following(self) -> int:
cnt = 0
t0 = time()
for v in self.tasks.nodes_to_query:
self.gdb.number_of_edges(v, None)
cnt += 1
dt = time() - t0
if dt > self.max_seconds_per_query:
break
return cnt
def find_vs_related_related(self) -> int:
cnt = 0
cnt_found = 0
t0 = time()
for v in self.tasks.nodes_to_analyze:
vs = self.gdb.neighbors_of_neighbors(v)
cnt += 1
cnt_found += len(vs)
dt = time() - t0
if dt > self.max_seconds_per_query:
break
print(f'---- {cnt} ops: {cnt_found} related to related nodes')
return cnt
def remove_e(self) -> int:
cnt = 0
for e in self.tasks.edges_to_change_by_one:
self.gdb.remove(e)
cnt += 1
return cnt
def upsert_e(self) -> int:
cnt = 0
for e in self.tasks.edges_to_change_by_one:
self.gdb.add(e)
cnt += 1
return cnt
def remove_es(self) -> int:
cnt = 0
for es in self.tasks.edges_to_change_batched:
self.gdb.remove(es)
cnt += len(es)
return cnt
def upsert_es(self) -> int:
cnt = 0
for es in self.tasks.edges_to_change_batched:
self.gdb.add(es)
cnt += len(es)
return cnt
def remove_v(self) -> int:
cnt = 0
for v in self.tasks.nodes_to_change_by_one:
self.gdb.remove_node(v)
cnt += 1
return cnt
def remove_bulk(self) -> int:
c = self.gdb.number_of_edges()
self.gdb.clear()
return c - self.gdb.number_of_edges()
def import_bulk(self) -> int:
return import_graph(self.gdb, self.dataset_path)
if __name__ == "__main__":
c = P0Config(device_name='MacbookPro')
try:
P3Bench().run()
finally:
c.default_stats_file.dump_to_file()