contains.py 786 B

12345678910111213141516171819202122232425262728
  1. """
  2. A benchmark for validation of the `contains` keyword.
  3. """
  4. from pyperf import Runner
  5. from jsonschema import Draft202012Validator
  6. schema = {
  7. "type": "array",
  8. "contains": {"const": 37},
  9. }
  10. validator = Draft202012Validator(schema)
  11. size = 1000
  12. beginning = [37] + [0] * (size - 1)
  13. middle = [0] * (size // 2) + [37] + [0] * (size // 2)
  14. end = [0] * (size - 1) + [37]
  15. invalid = [0] * size
  16. if __name__ == "__main__":
  17. runner = Runner()
  18. runner.bench_func("baseline", lambda: validator.is_valid([]))
  19. runner.bench_func("beginning", lambda: validator.is_valid(beginning))
  20. runner.bench_func("middle", lambda: validator.is_valid(middle))
  21. runner.bench_func("end", lambda: validator.is_valid(end))
  22. runner.bench_func("invalid", lambda: validator.is_valid(invalid))