aboutsummaryrefslogtreecommitdiff
path: root/.venv/lib/python3.12/site-packages/jsonschema/benchmarks/useless_keywords.py
blob: 50f435989dd14d34d804508067e91e5fae4a275c (about) (plain)
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
"""
A benchmark for validation of schemas containing lots of useless keywords.

Checks we filter them out once, ahead of time.
"""

from pyperf import Runner

from jsonschema import Draft202012Validator

NUM_USELESS = 100000
schema = dict(
    [
        ("not", {"const": 42}),
        *((str(i), i) for i in range(NUM_USELESS)),
        ("type", "integer"),
        *((str(i), i) for i in range(NUM_USELESS, NUM_USELESS)),
        ("minimum", 37),
    ],
)
validator = Draft202012Validator(schema)

valid = 3737
invalid = 12


if __name__ == "__main__":
    runner = Runner()
    runner.bench_func("beginning of schema", lambda: validator.is_valid(42))
    runner.bench_func("middle of schema", lambda: validator.is_valid("foo"))
    runner.bench_func("end of schema", lambda: validator.is_valid(12))
    runner.bench_func("valid", lambda: validator.is_valid(3737))