Skip to content

Commit 500726f

Browse files
committed
Add a (mostly placeholder) benchmark comparing enum and const.
We do no optimization here, though we could (if it turns out to be useful, which it's unclear that this benchmark shows). We'd also need to be careful that we don't mismark validation error details if we did swap keywords here.
1 parent 3646d91 commit 500726f

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
A benchmark for comparing equivalent validation of `const` and `enum`.
3+
"""
4+
5+
from pyperf import Runner
6+
7+
from jsonschema import Draft202012Validator
8+
9+
value = [37] * 100
10+
const_schema = {"const": list(value)}
11+
enum_schema = {"enum": [list(value)]}
12+
13+
valid = list(value)
14+
invalid = [*valid, 73]
15+
16+
const = Draft202012Validator(const_schema)
17+
enum = Draft202012Validator(enum_schema)
18+
19+
assert const.is_valid(valid)
20+
assert enum.is_valid(valid)
21+
assert not const.is_valid(invalid)
22+
assert not enum.is_valid(invalid)
23+
24+
25+
if __name__ == "__main__":
26+
runner = Runner()
27+
runner.bench_func("const valid", lambda: const.is_valid(valid))
28+
runner.bench_func("const invalid", lambda: const.is_valid(invalid))
29+
runner.bench_func("enum valid", lambda: enum.is_valid(valid))
30+
runner.bench_func("enum invalid", lambda: enum.is_valid(invalid))

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,4 @@ from-first = true
214214
"docs/*" = ["ANN", "D", "INP001"]
215215
"jsonschema/tests/*" = ["ANN", "D", "RUF012", "S", "PLR", "PYI024", "TRY"]
216216
"jsonschema/tests/test_format.py" = ["ERA001"]
217-
"jsonschema/benchmarks/*" = ["ANN", "D", "INP001"]
217+
"jsonschema/benchmarks/*" = ["ANN", "D", "INP001", "S101"]

0 commit comments

Comments
 (0)