useless_applicator_schemas.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. """
  2. A benchmark for validation of applicators containing lots of useless schemas.
  3. Signals a small possible optimization to remove all such schemas ahead of time.
  4. """
  5. from pyperf import Runner
  6. from jsonschema import Draft202012Validator as Validator
  7. NUM_USELESS = 100000
  8. subschema = {"const": 37}
  9. valid = 37
  10. invalid = 12
  11. baseline = Validator(subschema)
  12. # These should be indistinguishable from just `subschema`
  13. by_name = {
  14. "single subschema": {
  15. "anyOf": Validator({"anyOf": [subschema]}),
  16. "allOf": Validator({"allOf": [subschema]}),
  17. "oneOf": Validator({"oneOf": [subschema]}),
  18. },
  19. "redundant subschemas": {
  20. "anyOf": Validator({"anyOf": [subschema] * NUM_USELESS}),
  21. "allOf": Validator({"allOf": [subschema] * NUM_USELESS}),
  22. },
  23. "useless successful subschemas (beginning)": {
  24. "anyOf": Validator({"anyOf": [subschema, *[True] * NUM_USELESS]}),
  25. "allOf": Validator({"allOf": [subschema, *[True] * NUM_USELESS]}),
  26. },
  27. "useless successful subschemas (middle)": {
  28. "anyOf": Validator(
  29. {
  30. "anyOf": [
  31. *[True] * (NUM_USELESS // 2),
  32. subschema,
  33. *[True] * (NUM_USELESS // 2),
  34. ],
  35. },
  36. ),
  37. "allOf": Validator(
  38. {
  39. "allOf": [
  40. *[True] * (NUM_USELESS // 2),
  41. subschema,
  42. *[True] * (NUM_USELESS // 2),
  43. ],
  44. },
  45. ),
  46. },
  47. "useless successful subschemas (end)": {
  48. "anyOf": Validator({"anyOf": [*[True] * NUM_USELESS, subschema]}),
  49. "allOf": Validator({"allOf": [*[True] * NUM_USELESS, subschema]}),
  50. },
  51. "useless failing subschemas (beginning)": {
  52. "anyOf": Validator({"anyOf": [subschema, *[False] * NUM_USELESS]}),
  53. "oneOf": Validator({"oneOf": [subschema, *[False] * NUM_USELESS]}),
  54. },
  55. "useless failing subschemas (middle)": {
  56. "anyOf": Validator(
  57. {
  58. "anyOf": [
  59. *[False] * (NUM_USELESS // 2),
  60. subschema,
  61. *[False] * (NUM_USELESS // 2),
  62. ],
  63. },
  64. ),
  65. "oneOf": Validator(
  66. {
  67. "oneOf": [
  68. *[False] * (NUM_USELESS // 2),
  69. subschema,
  70. *[False] * (NUM_USELESS // 2),
  71. ],
  72. },
  73. ),
  74. },
  75. "useless failing subschemas (end)": {
  76. "anyOf": Validator({"anyOf": [*[False] * NUM_USELESS, subschema]}),
  77. "oneOf": Validator({"oneOf": [*[False] * NUM_USELESS, subschema]}),
  78. },
  79. }
  80. if __name__ == "__main__":
  81. runner = Runner()
  82. runner.bench_func("baseline valid", lambda: baseline.is_valid(valid))
  83. runner.bench_func("baseline invalid", lambda: baseline.is_valid(invalid))
  84. for group, applicators in by_name.items():
  85. for applicator, validator in applicators.items():
  86. runner.bench_func(
  87. f"{group}: {applicator} valid",
  88. lambda validator=validator: validator.is_valid(valid),
  89. )
  90. runner.bench_func(
  91. f"{group}: {applicator} invalid",
  92. lambda validator=validator: validator.is_valid(invalid),
  93. )