-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
performance.py
166 lines (130 loc) · 4.29 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import importlib.util
import timeit
import tempfile
from textwrap import dedent
# 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)
def fast_compiled(value, _):
fastjsonschema_validate(value)
def fast_not_compiled(value, json_schema):
fastjsonschema.compile(json_schema)(value)
validator_class = jsonschema.validators.validator_for(JSON_SCHEMA)
validator = validator_class(JSON_SCHEMA)
def jsonschema_compiled(value, _):
validator.validate(value)
with tempfile.NamedTemporaryFile('w', suffix='.py') as tmp_file:
tmp_file.write(fastjsonschema.compile_to_code(JSON_SCHEMA))
tmp_file.flush()
spec = importlib.util.spec_from_file_location("temp.performance", tmp_file.name)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
def fast_file(value, _):
module.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,
jsonschema_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} ==> {:10.7f}'.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('jsonschema_compiled')
t('jsonschema_compiled', valid_values=False)
t('jsonspec.validate')
t('jsonspec.validate', valid_values=False)
t('validictory.validate')
t('validictory.validate', valid_values=False)