-
Notifications
You must be signed in to change notification settings - Fork 9
/
benchmark.py
executable file
·160 lines (122 loc) · 4.38 KB
/
benchmark.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
#!/usr/bin/env python
import lollipop.types as lt
import lollipop.validators as lv
from collections import namedtuple
import timeit
import os
import hotshot, hotshot.stats
def profile(func, *args, **kwargs):
prof = hotshot.Profile("object.prof")
prof.runcall(func, *args, **kwargs)
prof.close()
stats = hotshot.stats.load("object.prof")
stats.strip_dirs()
stats.sort_stats('time', 'calls')
stats.print_stats(30)
os.remove('object.prof')
def benchmark_large_strings_object_dumping(iterations=1000):
n = 100
TYPE = lt.Object({
'field%02d' % idx: lt.String()
for idx in xrange(n)
})
Data = namedtuple('Data', ['field%02d' % idx for idx in xrange(n)])
data = Data(*['value%02d' % idx for idx in xrange(n)])
TYPE.dump(data) # warmup
time = timeit.timeit(lambda: TYPE.dump(data), number=iterations)
print 'Large strings object dumping: %s' % time
def benchmark_large_strings_object_loading(iterations=1000):
n = 100
TYPE = lt.Object({
'field%02d' % idx: lt.String()
for idx in xrange(n)
})
data = {'field%02d' % idx: 'value%02d' % idx for idx in xrange(n)}
TYPE.load(data) # warmup
time = timeit.timeit(lambda: TYPE.load(data), number=iterations)
print 'Large strings object loading: %s' % time
def benchmark_large_strings_object_with_validators_loading(iterations=1000):
n = 100
TYPE = lt.Object({
'field%02d' % idx: lt.String(validate=lv.Length(min=1))
for idx in xrange(n)
})
data = {'field%02d' % idx: 'value%02d' % idx for idx in xrange(n)}
TYPE.load(data) # warmup
time = timeit.timeit(lambda: TYPE.load(data), number=iterations)
print 'Large strings object with validators loading: %s' % time
def benchmark_large_integers_object_dumping(iterations=1000):
n = 100
TYPE = lt.Object({
'field%02d' % idx: lt.Integer()
for idx in xrange(n)
})
Data = namedtuple('Data', ['field%02d' % idx for idx in xrange(n)])
data = Data(*[idx for idx in xrange(n)])
TYPE.dump(data) # warmup
time = timeit.timeit(lambda: TYPE.dump(data), number=iterations)
print 'Large integers object dumping: %s' % time
def benchmark_large_integers_object_loading(iterations=1000):
n = 100
TYPE = lt.Object({
'field%02d' % idx: lt.Integer()
for idx in xrange(n)
})
data = {'field%02d' % idx: idx for idx in xrange(n)}
TYPE.load(data) # warmup
time = timeit.timeit(lambda: TYPE.load(data), number=iterations)
print 'Large integers object loading: %s' % time
def benchmark_complex_object_dumping(iterations=1000):
Foo = namedtuple('Foo', ['a', 'b', 'c', 'd'])
Bar = namedtuple('Bar', ['x', 'y', 'foo', 'foos'])
FOO = lt.Object({
'a': lt.Integer(),
'b': lt.FunctionField(lt.Integer(), lambda o: o.b + 10),
'c': lt.String(),
'd': lt.Integer(),
})
BAR = lt.Object({
'x': lt.String(),
'y': lt.Integer(),
'foo': FOO,
'foos': lt.List(FOO),
})
bar = Bar(
'bar', 123,
Foo(123, 456, 'foo', 789),
[Foo(123+i, 456+i, 'foo', 789+i) for i in xrange(10)]
)
BAR.dump(bar) # warmup
time = timeit.timeit(lambda: BAR.dump(bar), number=iterations)
print 'Complex object dumping: %s' % time
def benchmark_complex_object_loading(iterations=1000):
Foo = namedtuple('Foo', ['a', 'b', 'c', 'd'])
Bar = namedtuple('Bar', ['x', 'y', 'foo', 'foos'])
FOO = lt.Object({
'a': lt.Integer(),
'b': lt.FunctionField(lt.Integer(), lambda o: o.b + 10),
'c': lt.String(),
'd': lt.Integer(),
})
BAR = lt.Object({
'x': lt.String(),
'y': lt.Integer(),
'foo': FOO,
'foos': lt.List(FOO),
})
data = {
'x': 'bar', 'y': 123,
'foo': {'a': 123, 'b': 456, 'c': 'foo', 'd': 789},
'foos': [{'a': 123+i, 'b': 456+i, 'c': 'foo', 'd': 789+i}
for i in xrange(10)],
}
BAR.load(data) # warmup
time = timeit.timeit(lambda: BAR.load(data), number=iterations)
print 'Complex object loading: %s' % time
benchmark_large_strings_object_dumping()
benchmark_large_integers_object_dumping()
benchmark_complex_object_dumping()
benchmark_large_strings_object_loading()
benchmark_large_integers_object_loading()
benchmark_large_strings_object_with_validators_loading()
benchmark_complex_object_loading()