|
| 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