forked from tddschn/easygraph-bench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile_template.jinja.py
154 lines (119 loc) · 4.13 KB
/
profile_template.jinja.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
#!/usr/bin/env python3
{{ profile_preparation_code }}
from benchmark import benchmark_autorange
from utils_db import profile_script_insert_results
from utils_other import remove_system_resource_limits
import sqlite3
import sys
import argparse
def insert_results():
try:
profile_script_insert_results(__file__, filename, avg_times, args.iteration)
except sqlite3.OperationalError as e:
print(f"Failed to insert results into database: \n{e}")
print(f'Please run `./create_bench_results_db.py` to resolve this issue.')
def get_args():
'''Get command-line arguments'''
parser = argparse.ArgumentParser(
description='Benchmark {{ tool }}', formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument('dataset', help='path to the dataset file in tab-separated edgelist format', metavar='PATH', type=str)
parser.add_argument(
'-n',
'--iteration',
help='iteration count when benchmarking, auto-determined if unspecified',
metavar='INT',
type=int,
)
parser.add_argument(
'--print-graph-info',
help='get the # of nodes and edges and print',
action='store_true',
)
parser.add_argument(
'--print-graph-info-only',
help='get the # of nodes and edges and print, then exit',
action='store_true',
)
return parser.parse_args()
args = get_args()
if args.print_graph_info_only:
args.print_graph_info = True
remove_system_resource_limits()
filename = args.dataset
n = args.iteration
# filename = sys.argv[1]
# n = int(sys.argv[2])
avg_times: dict[str, float] = {}
print('''\033[91m=================================\033[0m''')
print(f"""Profiling \033[96m{{ tool }}\033[0m on dataset \033[34m{filename}\033[0m""")
print('''\033[91m=================================\033[0m''')
{{ profile_code }}
{% for method, code in graph_benchmark_code.items() %}
# ===========================
print(f"""Profiling \033[92m{{ method }}\033[0m on dataset \033[34m{filename}\033[0m""")
print("=================")
print()
# easygraph constraint doesn't have c bindings
# if method starts with 'constraint', then use python version of graph
{% if tool == 'easygraph' and method.startswith('constraint') %}
g = g_py
{% endif %}
{% if tool in ('networkx', 'networkit', 'easygraph') %}
{% if method == 'k-core' %}
# remove self loop from graph g before doing k-core
{% if tool in ('networkx', 'easygraph') %}
# if tool is easygraph
{% if tool == 'easygraph' %}
# give eg access to a python version of Graph first, so that removing self loops is possible
# eval code.removesuffix('.cpp()')
{# g_python = eval({{ loading_code_str }}.removesuffix('.cpp()')) #}
g_python = g_py
g = g_python
g.remove_edges({{ tool }}.selfloop_edges(g))
g = g.cpp()
{% endif %}
{% if tool == 'networkx' %}
g.remove_edges_from({{ tool }}.selfloop_edges(g))
{% endif %}
{% endif %}
{% if tool == 'networkit' %}
g.removeSelfLoops()
{% endif %}
{% endif %}
# {{ code }} contains quotes
avg_times |= {'{{ method }}': benchmark_autorange({{ code }}, globals=globals(), n=n) }
# if tool starts with 'constraint' and
# easygraph constraint doesn't have c bindings
# if method starts with 'constraint', then convert g back
{% if tool == 'easygraph' and method.startswith('constraint') %}
g = g_cpp
{% endif %}
{% if method in ('loading', 'loading_undirected') %}
# loading* only, make g in the globals() so the methods after loading methods can access it.
{% if tool == 'easygraph' %}
g_py = eval({{ code }})
g = g_py.cpp()
g_cpp = g
{% else %}
g = eval({{ code }})
{% endif %}
if args.print_graph_info:
{% if print_graph_info %}
{{ print_graph_info }}
{% else %}
print('{{ tool }} does not support printing graph info.')
{% endif %}
if args.print_graph_info_only:
sys.exit(0)
{# {% set loading_code_str = {{ code }} %} #}
{% if tool in ('networkx', 'easygraph') %}
# networkx & easygraph only, after loading*
from utils import get_first_node
nodeid = 'first_node'
first_node = get_first_node(g)
{% endif %}
{% endif %}
{% endif %}
{% endfor %}
insert_results()