unused_registry.py 940 B

1234567891011121314151617181920212223242526272829303132333435
  1. """
  2. An unused schema registry should not cause slower validation.
  3. "Unused" here means one where no reference resolution is occurring anyhow.
  4. See https://github.com/python-jsonschema/jsonschema/issues/1088.
  5. """
  6. from pyperf import Runner
  7. from referencing import Registry
  8. from referencing.jsonschema import DRAFT201909
  9. from jsonschema import Draft201909Validator
  10. registry = Registry().with_resource(
  11. "urn:example:foo",
  12. DRAFT201909.create_resource({}),
  13. )
  14. schema = {"$ref": "https://json-schema.org/draft/2019-09/schema"}
  15. instance = {"maxLength": 4}
  16. no_registry = Draft201909Validator(schema)
  17. with_useless_registry = Draft201909Validator(schema, registry=registry)
  18. if __name__ == "__main__":
  19. runner = Runner()
  20. runner.bench_func(
  21. "no registry",
  22. lambda: no_registry.is_valid(instance),
  23. )
  24. runner.bench_func(
  25. "useless registry",
  26. lambda: with_useless_registry.is_valid(instance),
  27. )