useless_keywords.py 867 B

1234567891011121314151617181920212223242526272829303132
  1. """
  2. A benchmark for validation of schemas containing lots of useless keywords.
  3. Checks we filter them out once, ahead of time.
  4. """
  5. from pyperf import Runner
  6. from jsonschema import Draft202012Validator
  7. NUM_USELESS = 100000
  8. schema = dict(
  9. [
  10. ("not", {"const": 42}),
  11. *((str(i), i) for i in range(NUM_USELESS)),
  12. ("type", "integer"),
  13. *((str(i), i) for i in range(NUM_USELESS, NUM_USELESS)),
  14. ("minimum", 37),
  15. ],
  16. )
  17. validator = Draft202012Validator(schema)
  18. valid = 3737
  19. invalid = 12
  20. if __name__ == "__main__":
  21. runner = Runner()
  22. runner.bench_func("beginning of schema", lambda: validator.is_valid(42))
  23. runner.bench_func("middle of schema", lambda: validator.is_valid("foo"))
  24. runner.bench_func("end of schema", lambda: validator.is_valid(12))
  25. runner.bench_func("valid", lambda: validator.is_valid(3737))