const_vs_enum.py 830 B

123456789101112131415161718192021222324252627282930
  1. """
  2. A benchmark for comparing equivalent validation of `const` and `enum`.
  3. """
  4. from pyperf import Runner
  5. from jsonschema import Draft202012Validator
  6. value = [37] * 100
  7. const_schema = {"const": list(value)}
  8. enum_schema = {"enum": [list(value)]}
  9. valid = list(value)
  10. invalid = [*valid, 73]
  11. const = Draft202012Validator(const_schema)
  12. enum = Draft202012Validator(enum_schema)
  13. assert const.is_valid(valid)
  14. assert enum.is_valid(valid)
  15. assert not const.is_valid(invalid)
  16. assert not enum.is_valid(invalid)
  17. if __name__ == "__main__":
  18. runner = Runner()
  19. runner.bench_func("const valid", lambda: const.is_valid(valid))
  20. runner.bench_func("const invalid", lambda: const.is_valid(invalid))
  21. runner.bench_func("enum valid", lambda: enum.is_valid(valid))
  22. runner.bench_func("enum invalid", lambda: enum.is_valid(invalid))