-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark2.py
100 lines (71 loc) · 2.21 KB
/
benchmark2.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
# https://github.com/graphql-python/graphene/blob/master/examples/starwars_relay/data.py
import typing as t
import copy
from pprint import pprint
from timeit import timeit
import json
from auto_json.schema_analyse import AutoJson, SchemaMonitor
from auto_json.graphql_naive import generate as naive_generate
from auto_json.graphql_ast import generate as ast_generate
with open('data.json', 'rb') as fr:
data = json.load(fr)
class Building(AutoJson):
name: str
floors: t.List['Floor']
class Floor(AutoJson):
name: str
rooms: t.List['Room']
class Room(AutoJson):
name: str
dimension_gate: t.Optional[Building]
bookmark: t.Dict[str, str]
SchemaMonitor.resolve(strict=True)
naive_generate()
f1 = Building.from_dict
ast_generate()
f2 = Building.from_dict
ast_generate(use_cython=True)
f3 = Building.from_dict
building = Building.from_dict(data)
assert building.name == 'school'
assert building.floors[0].name == 'sf1'
rooms = building.floors[2].rooms
a304 = rooms[0]
assert a304.name == 'a304'
assert 'link' in a304.bookmark
h301 = a304.dimension_gate
assert h301.name == '301'
assert len(h301.floors) == 2
inc = building
def expand_vertically(b: Building):
b = Building.from_dict(b.to_dict())
last = b.floors[-1].rooms[-1]
while last.dimension_gate:
last = last.dimension_gate.floors[-1].rooms[-1]
last.dimension_gate = inc
return b
def expand_horizontally(b: Building):
b = Building.from_dict(b.to_dict())
b.floors.append(
Floor(
name="_", rooms=[Room(name="_", bookmark={}, dimension_gate=inc)]))
return b
def benchmark():
print('==================')
print('naive version costs: ',
timeit('fn(data)', number=100000, globals={
**ctx, 'fn': f1
}))
print('ast level generated function costs',
timeit('fn(data)', number=100000, globals={
**ctx, 'fn': f2
}))
print('ast level generated function with cython compilation costs',
timeit('fn(data)', number=100000, globals={
**ctx, 'fn': f3
}))
building_ = building
for each in range(10):
building_ = expand_vertically(building_)
ctx = {'data': building_.to_dict()}
benchmark()