File: unused_registry.py

package info (click to toggle)
python-jsonschema 4.25.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,952 kB
  • sloc: python: 8,469; makefile: 210; sh: 12
file content (35 lines) | stat: -rw-r--r-- 940 bytes parent folder | download
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
"""
An unused schema registry should not cause slower validation.

"Unused" here means one where no reference resolution is occurring anyhow.

See https://github.com/python-jsonschema/jsonschema/issues/1088.
"""
from pyperf import Runner
from referencing import Registry
from referencing.jsonschema import DRAFT201909

from jsonschema import Draft201909Validator

registry = Registry().with_resource(
    "urn:example:foo",
    DRAFT201909.create_resource({}),
)

schema = {"$ref": "https://json-schema.org/draft/2019-09/schema"}
instance = {"maxLength": 4}

no_registry = Draft201909Validator(schema)
with_useless_registry = Draft201909Validator(schema, registry=registry)

if __name__ == "__main__":
    runner = Runner()

    runner.bench_func(
        "no registry",
        lambda: no_registry.is_valid(instance),
    )
    runner.bench_func(
        "useless registry",
        lambda: with_useless_registry.is_valid(instance),
    )