forked from sqlalchemy/sqlalchemy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.py
More file actions
209 lines (182 loc) · 6.63 KB
/
Copy pathcommand.py
File metadata and controls
209 lines (182 loc) · 6.63 KB
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
from collections import defaultdict
from datetime import datetime
import subprocess
import sqlalchemy as sa
from .base import Case
if True:
from . import cache_key # noqa: F401
from . import collections_ # noqa: F401
from . import misc # noqa: F401
from . import result # noqa: F401
from . import row # noqa: F401
def tabulate(
impl_names: list[str],
result_by_method: dict[str, dict[str, float]],
):
if not result_by_method:
return
dim = max(len(n) for n in impl_names)
dim = min(dim, 20)
width = max(20, *(len(m) + 1 for m in result_by_method))
string_cell = "{:<%s}" % dim
header = "{:<%s}|" % width + f" {string_cell} |" * len(impl_names)
num_format = "{:<%s.9f}" % dim
csv_row = "{:<%s}|" % width + " {} |" * len(impl_names)
print(header.format("", *impl_names))
for meth in result_by_method:
data = result_by_method[meth]
strings = [
(
num_format.format(data[name])[:dim]
if name in data
else string_cell.format("—")
)
for name in impl_names
]
print(csv_row.format(meth, *strings))
def find_git_sha():
try:
git_res = subprocess.run(
["git", "rev-parse", "--short", "HEAD"], stdout=subprocess.PIPE
)
return git_res.stdout.decode("utf-8").strip()
except Exception:
return None
def main():
import argparse
cases = Case._CASES
parser = argparse.ArgumentParser(
description="Compare implementation between them"
)
parser.add_argument(
"case",
help="Case to run",
nargs="+",
choices=["all"] + sorted(c.__name__ for c in cases),
)
parser.add_argument("--filter", help="filter the test for this regexp")
parser.add_argument(
"--factor", help="scale number passed to timeit", type=float, default=1
)
parser.add_argument("--csv", help="save to csv", action="store_true")
save_group = parser.add_argument_group("Save result for later compare")
save_group.add_argument(
"--save-db",
help="Name of the sqlite db file to use",
const="perf.db",
nargs="?",
)
save_group.add_argument(
"--save-name",
help="A name given to the current save. "
"Can be used later to compare against this run.",
)
compare_group = parser.add_argument_group("Compare against stored data")
compare_group.add_argument(
"--compare-db",
help="Name of the sqlite db file to read for the compare data",
const="perf.db",
nargs="?",
)
compare_group.add_argument(
"--compare-filter",
help="Filter the compare data using this string. Can include "
"git-short-sha, save-name previously used or date. By default the "
"latest values are used",
)
args = parser.parse_args()
to_run: list[type[Case]]
if "all" in args.case:
to_run = cases
else:
to_run = [c for c in cases if c.__name__ in args.case]
if args.save_db:
save_engine = sa.create_engine(
f"sqlite:///{args.save_db}", poolclass=sa.NullPool
)
PerfTable.metadata.create_all(save_engine)
sha = find_git_sha()
if args.compare_db:
compare_engine = sa.create_engine(
f"sqlite:///{args.compare_db}", poolclass=sa.NullPool
)
stmt = (
sa.select(PerfTable)
.where(PerfTable.c.factor == args.factor)
.order_by(PerfTable.c.created.desc())
)
if args.compare_filter:
cf = args.compare_filter
stmt = stmt.where(
sa.or_(
PerfTable.c.created.cast(sa.Text).icontains(cf),
PerfTable.c.git_short_sha.icontains(cf),
PerfTable.c.save_name.icontains(cf),
),
)
for case in to_run:
print("Running case", case.__name__)
if args.compare_db:
with compare_engine.connect() as conn:
case_stmt = stmt.where(PerfTable.c.case == case.__name__)
compare_by_meth = defaultdict(dict)
for prow in conn.execute(case_stmt):
if prow.impl in compare_by_meth[prow.method]:
continue
compare_by_meth[prow.method][prow.impl] = prow.value
else:
compare_by_meth = {}
result_by_impl, impl_names = case.run_case(args.factor, args.filter)
result_by_method = defaultdict(dict)
all_impls = dict.fromkeys(result_by_impl)
for impl in result_by_impl:
for meth in result_by_impl[impl]:
meth_dict = result_by_method[meth]
meth_dict[impl] = result_by_impl[impl][meth]
if meth in compare_by_meth and impl in compare_by_meth[meth]:
cmp_impl = f"compare {impl}"
over = f"{impl} / compare"
all_impls[cmp_impl] = None
all_impls[over] = None
meth_dict[cmp_impl] = compare_by_meth[meth][impl]
meth_dict[over] = meth_dict[impl] / meth_dict[cmp_impl]
tabulate(list(all_impls), result_by_method)
if args.csv:
import csv
file_name = f"{case.__name__}.csv"
with open(file_name, "w", newline="") as f:
w = csv.DictWriter(f, ["", *result_by_impl])
w.writeheader()
for n in result_by_method:
w.writerow({"": n, **result_by_method[n]})
print("Wrote file", file_name)
if args.save_db:
data = [
{
"case": case.__name__,
"impl": impl,
"method": meth,
"value": result_by_impl[impl][meth],
"factor": args.factor,
"save_name": args.save_name,
"git_short_sha": sha,
"created": Now,
}
for impl in impl_names
for meth in result_by_impl[impl]
]
with save_engine.begin() as conn:
conn.execute(PerfTable.insert(), data)
PerfTable = sa.Table(
"perf_table",
sa.MetaData(),
sa.Column("case", sa.Text, nullable=False),
sa.Column("impl", sa.Text, nullable=False),
sa.Column("method", sa.Text, nullable=False),
sa.Column("value", sa.Float),
sa.Column("factor", sa.Float),
sa.Column("save_name", sa.Text),
sa.Column("git_short_sha", sa.Text),
sa.Column("created", sa.DateTime, nullable=False),
)
Now = datetime.now()