Skip to content

Commit 058b2ad

Browse files
committed
issue python-jsonschema#158: Add benchmarks based on V3 &V4 schemas.
1 parent a38eac9 commit 058b2ad

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

jsonschema/tests/test_benchmarks.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import time
2+
3+
from jsonschema.tests.compat import unittest
4+
from jsonschema.validators import (
5+
Draft3Validator, Draft4Validator, validate,
6+
)
7+
8+
9+
class TestBenchmarks(unittest.TestCase):
10+
11+
def run_for_sometime(self, sec):
12+
now = time.time()
13+
then = now + sec
14+
run = 0
15+
while now < then:
16+
yield run
17+
run += 1
18+
now = time.time()
19+
20+
run_interval_sec = 2.0
21+
22+
def print_results(self, runs, interval_sec):
23+
print("%i runs in %s sec\n\t%.2f ms/run, %.4f runs/sec" % (runs, interval_sec, 1000*interval_sec/(runs), interval_sec / runs))
24+
25+
def test_V3_meta_schema(self):
26+
runs = 0
27+
for run in self.run_for_sometime(self.run_interval_sec):
28+
runs = run
29+
v_class = Draft3Validator
30+
schema = v_class.META_SCHEMA
31+
v_class(schema).validate(schema)
32+
33+
self.print_results(runs, self.run_interval_sec)
34+
35+
36+
def test_V4_meta_schema(self):
37+
runs = 0
38+
for run in self.run_for_sometime(self.run_interval_sec):
39+
runs = run
40+
v_class = Draft4Validator
41+
schema = v_class.META_SCHEMA
42+
v_class(schema).validate(schema)
43+
44+
self.print_results(runs, self.run_interval_sec)
45+
46+
47+
def test_both_meta_schemas(self):
48+
v_classes = [Draft3Validator, Draft4Validator]
49+
runs = 0
50+
for run in self.run_for_sometime(self.run_interval_sec):
51+
runs = run
52+
for v_class in v_classes:
53+
schema = v_class.META_SCHEMA
54+
v_class(schema).validate(schema)
55+
56+
57+
self.print_results(runs, self.run_interval_sec)

0 commit comments

Comments
 (0)