Repository navigation
Expand file tree
/
Copy pathbenchmark.py
More file actions
90 lines (66 loc) · 1.89 KB
/
Copy pathbenchmark.py
File metadata and controls
90 lines (66 loc) · 1.89 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
import json
import timeit
from typing import Any
from typing import Mapping
from typing import NamedTuple
from typing import Sequence
from typing import Union
# ruff: noqa: D100 D101 D103 T201
class CTSCase(NamedTuple):
query: str
data: Union[Sequence[Any], Mapping[str, Any]]
def valid_queries() -> Sequence[CTSCase]:
with open("tests/cts/cts.json") as fd:
data = json.load(fd)
return [
(CTSCase(t["selector"], t["document"]))
for t in data["tests"]
if not t.get("invalid_selector", False)
]
QUERIES = valid_queries()
COMPILE_AND_FIND_SETUP = "from jsonpath import findall"
COMPILE_AND_FIND_STMT = """\
for query, data in QUERIES:
findall(query, data)"""
JUST_COMPILE_SETUP = "from jsonpath import compile"
JUST_COMPILE_STMT = """\
for query, _ in QUERIES:
compile(query)"""
JUST_FIND_SETUP = """\
from jsonpath import compile
compiled_queries = [(compile(q), d) for q, d in QUERIES]
"""
JUST_FIND_STMT = """\
for path, data in compiled_queries:
path.findall(data)"""
def benchmark(number: int = 100, best_of: int = 3) -> None:
print(
f"repeating {len(QUERIES)} queries on small data {number} times, "
f"best of {best_of} rounds"
)
results = timeit.repeat(
COMPILE_AND_FIND_STMT,
setup=COMPILE_AND_FIND_SETUP,
globals={"QUERIES": QUERIES},
number=number,
repeat=best_of,
)
print("compile and find", results)
results = timeit.repeat(
JUST_COMPILE_STMT,
setup=JUST_COMPILE_SETUP,
globals={"QUERIES": QUERIES},
number=number,
repeat=best_of,
)
print("just compile", results)
results = timeit.repeat(
JUST_FIND_STMT,
setup=JUST_FIND_SETUP,
globals={"QUERIES": QUERIES},
number=number,
repeat=best_of,
)
print("just find", results)
if __name__ == "__main__":
benchmark()