subcomponents.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. """
  2. A benchmark which tries to compare the possible slow subparts of validation.
  3. """
  4. from referencing import Registry
  5. from referencing.jsonschema import DRAFT202012
  6. from rpds import HashTrieMap, HashTrieSet
  7. from jsonschema import Draft202012Validator
  8. schema = {
  9. "type": "array",
  10. "minLength": 1,
  11. "maxLength": 1,
  12. "items": {"type": "integer"},
  13. }
  14. hmap = HashTrieMap()
  15. hset = HashTrieSet()
  16. registry = Registry()
  17. v = Draft202012Validator(schema)
  18. def registry_data_structures():
  19. return hmap.insert("foo", "bar"), hset.insert("foo")
  20. def registry_add():
  21. resource = DRAFT202012.create_resource(schema)
  22. return registry.with_resource(uri="urn:example", resource=resource)
  23. if __name__ == "__main__":
  24. from pyperf import Runner
  25. runner = Runner()
  26. runner.bench_func("HashMap/HashSet insertion", registry_data_structures)
  27. runner.bench_func("Registry insertion", registry_add)
  28. runner.bench_func("Success", lambda: v.is_valid([1]))
  29. runner.bench_func("Failure", lambda: v.is_valid(["foo"]))
  30. runner.bench_func("Metaschema validation", lambda: v.check_schema(schema))