forked from horejsek/python-fastjsonschema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperformance.py
139 lines (115 loc) · 3.78 KB
/
performance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from textwrap import dedent
import timeit
# apt-get install jsonschema json-spec validictory
import fastjsonschema
import jsonschema
import validictory
from jsonspec.validators import load
NUMBER = 1000
JSON_SCHEMA = {
'$schema': 'http://json-schema.org/draft-04/schema#',
'type': 'array',
'items': [
{
'type': 'number',
'maximum': 10,
'exclusiveMaximum': True,
},
{
'type': 'string',
'enum': ['hello', 'world'],
},
{
'type': 'array',
'minItems': 1,
'maxItems': 3,
'items': [
{'type': 'number'},
{'type': 'string'},
{'type': 'boolean'},
],
},
{
'type': 'object',
'required': ['a', 'b'],
'minProperties': 3,
'properties': {
'a': {'type': ['null', 'string']},
'b': {'type': ['null', 'string']},
'c': {'type': ['null', 'string'], 'default': 'abc'}
},
'additionalProperties': {'type': 'string'},
},
{'not': {'type': ['null']}},
{'oneOf': [
{'type': 'number', 'multipleOf': 3},
{'type': 'number', 'multipleOf': 5},
]},
],
}
VALUES_OK = (
[9, 'hello', [1, 'a', True], {'a': 'a', 'b': 'b', 'd': 'd'}, 42, 3],
[9, 'world', [1, 'a', True], {'a': 'a', 'b': 'b', 'd': 'd'}, 42, 3],
[9, 'world', [1, 'a', True], {'a': 'a', 'b': 'b', 'c': 'xy'}, 42, 3],
[9, 'world', [1, 'a', True], {'a': 'a', 'b': 'b', 'c': 'xy'}, 'str', 5],
)
VALUES_BAD = (
[10, 'world', [1, 'a', True], {'a': 'a', 'b': 'b', 'c': 'xy'}, 'str', 5],
[9, 'xxx', [1, 'a', True], {'a': 'a', 'b': 'b', 'c': 'xy'}, 'str', 5],
[9, 'hello', [], {'a': 'a', 'b': 'b', 'c': 'xy'}, 'str', 5],
[9, 'hello', [1, 2, 3], {'a': 'a', 'b': 'b', 'c': 'xy'}, 'str', 5],
[9, 'hello', [1, 'a', True], {'a': 'a', 'x': 'x', 'y': 'y'}, 'str', 5],
[9, 'hello', [1, 'a', True], {}, 'str', 5],
[9, 'hello', [1, 'a', True], {'a': 'a', 'b': 'b', 'x': 'x'}, None, 5],
[9, 'hello', [1, 'a', True], {'a': 'a', 'b': 'b', 'x': 'x'}, 42, 15],
)
fastjsonschema_validate = fastjsonschema.compile(JSON_SCHEMA)
fast_compiled = lambda value, _: fastjsonschema_validate(value)
fast_not_compiled = lambda value, json_schema: fastjsonschema.compile(json_schema)(value)
with open('temp/performance.py', 'w') as f:
f.write(fastjsonschema.compile_to_code(JSON_SCHEMA))
from temp.performance import validate
fast_file = lambda value, _: validate(value)
jsonspec = load(JSON_SCHEMA)
def t(func, valid_values=True):
module = func.split('.')[0]
setup = """from __main__ import (
JSON_SCHEMA,
VALUES_OK,
VALUES_BAD,
validictory,
jsonschema,
jsonspec,
fast_compiled,
fast_file,
fast_not_compiled,
)
"""
if valid_values:
code = dedent("""
for value in VALUES_OK:
{}(value, JSON_SCHEMA)
""".format(func))
else:
code = dedent("""
try:
for value in VALUES_BAD:
{}(value, JSON_SCHEMA)
except:
pass
""".format(func))
res = timeit.timeit(code, setup, number=NUMBER)
print('{:<20} {:<10} ==> {}'.format(module, 'valid' if valid_values else 'invalid', res))
print('Number: {}'.format(NUMBER))
t('fast_compiled')
t('fast_compiled', valid_values=False)
t('fast_file')
t('fast_file', valid_values=False)
t('fast_not_compiled')
t('fast_not_compiled', valid_values=False)
t('jsonschema.validate')
t('jsonschema.validate', valid_values=False)
t('jsonspec.validate')
t('jsonspec.validate', valid_values=False)
t('validictory.validate')
t('validictory.validate', valid_values=False)