Skip to content
Newer
Older
100644 183 lines (147 sloc) 5.41 KB
6d9bfff @rozza Started work on performance
rozza authored Dec 16, 2011
1 #!/usr/bin/env python
2
3 import timeit
4
5
6 def cprofile_main():
7 from pymongo import Connection
8 connection = Connection()
9 connection.drop_database('timeit_test')
10 connection.disconnect()
11
12 from mongoengine import Document, DictField, connect
13 connect("timeit_test")
14
15 class Noddy(Document):
16 fields = DictField()
17
18 for i in xrange(1):
19 noddy = Noddy()
20 for j in range(20):
21 noddy.fields["key" + str(j)] = "value " + str(j)
22 noddy.save()
23
24
25 def main():
26 """
27 0.4 Performance Figures ...
28
29 ----------------------------------------------------------------------------------------------------
30 Creating 10000 dictionaries - Pymongo
31 1.1141769886
32 ----------------------------------------------------------------------------------------------------
33 Creating 10000 dictionaries - MongoEngine
34 2.37724113464
35 ----------------------------------------------------------------------------------------------------
36 Creating 10000 dictionaries - MongoEngine, safe=False, validate=False
37 1.92479610443
38
39 0.5.X
40 ----------------------------------------------------------------------------------------------------
41 Creating 10000 dictionaries - Pymongo
42 1.10552310944
43 ----------------------------------------------------------------------------------------------------
44 Creating 10000 dictionaries - MongoEngine
45 16.5169169903
46 ----------------------------------------------------------------------------------------------------
47 Creating 10000 dictionaries - MongoEngine, safe=False, validate=False
48 14.9446101189
49 ----------------------------------------------------------------------------------------------------
50 Creating 10000 dictionaries - MongoEngine, safe=False, validate=False, cascade=False
51 14.912801981
52 ----------------------------------------------------------------------------------------------------
53 Creating 10000 dictionaries - MongoEngine, force=True
54 14.9617750645
55
56 Performance
57 ----------------------------------------------------------------------------------------------------
58 Creating 10000 dictionaries - Pymongo
59 1.10072994232
60 ----------------------------------------------------------------------------------------------------
61 Creating 10000 dictionaries - MongoEngine
62 5.27341103554
63 ----------------------------------------------------------------------------------------------------
64 Creating 10000 dictionaries - MongoEngine, safe=False, validate=False
65 4.49365401268
66 ----------------------------------------------------------------------------------------------------
67 Creating 10000 dictionaries - MongoEngine, safe=False, validate=False, cascade=False
68 4.43459296227
69 ----------------------------------------------------------------------------------------------------
70 Creating 10000 dictionaries - MongoEngine, force=True
71 4.40114378929
72 """
73
74 setup = """
75 from pymongo import Connection
76 connection = Connection()
77 connection.drop_database('timeit_test')
78 """
79
80 stmt = """
81 from pymongo import Connection
82 connection = Connection()
83
84 db = connection.timeit_test
85 noddy = db.noddy
86
87 for i in xrange(10000):
88 example = {'fields': {}}
89 for j in range(20):
90 example['fields']["key"+str(j)] = "value "+str(j)
91
92 noddy.insert(example)
93
94 myNoddys = noddy.find()
95 [n for n in myNoddys] # iterate
96 """
97
98 print "-" * 100
99 print """Creating 10000 dictionaries - Pymongo"""
100 t = timeit.Timer(stmt=stmt, setup=setup)
101 print t.timeit(1)
102
103 setup = """
104 from pymongo import Connection
105 connection = Connection()
106 connection.drop_database('timeit_test')
107 connection.disconnect()
108
109 from mongoengine import Document, DictField, connect
110 connect("timeit_test")
111
112 class Noddy(Document):
113 fields = DictField()
114 """
115
116 stmt = """
117 for i in xrange(10000):
118 noddy = Noddy()
119 for j in range(20):
120 noddy.fields["key"+str(j)] = "value "+str(j)
121 noddy.save()
122
123 myNoddys = Noddy.objects()
124 [n for n in myNoddys] # iterate
125 """
126
127 print "-" * 100
128 print """Creating 10000 dictionaries - MongoEngine"""
129 t = timeit.Timer(stmt=stmt, setup=setup)
130 print t.timeit(1)
131
132 stmt = """
133 for i in xrange(10000):
134 noddy = Noddy()
135 for j in range(20):
136 noddy.fields["key"+str(j)] = "value "+str(j)
137 noddy.save(safe=False, validate=False)
138
139 myNoddys = Noddy.objects()
140 [n for n in myNoddys] # iterate
141 """
142
143 print "-" * 100
144 print """Creating 10000 dictionaries - MongoEngine, safe=False, validate=False"""
145 t = timeit.Timer(stmt=stmt, setup=setup)
146 print t.timeit(1)
147
148
149 stmt = """
150 for i in xrange(10000):
151 noddy = Noddy()
152 for j in range(20):
153 noddy.fields["key"+str(j)] = "value "+str(j)
154 noddy.save(safe=False, validate=False, cascade=False)
155
156 myNoddys = Noddy.objects()
157 [n for n in myNoddys] # iterate
158 """
159
160 print "-" * 100
161 print """Creating 10000 dictionaries - MongoEngine, safe=False, validate=False, cascade=False"""
162 t = timeit.Timer(stmt=stmt, setup=setup)
163 print t.timeit(1)
164
165 stmt = """
166 for i in xrange(10000):
167 noddy = Noddy()
168 for j in range(20):
169 noddy.fields["key"+str(j)] = "value "+str(j)
170 noddy.save(force_insert=True, safe=False, validate=False, cascade=False)
171
172 myNoddys = Noddy.objects()
173 [n for n in myNoddys] # iterate
174 """
175
176 print "-" * 100
177 print """Creating 10000 dictionaries - MongoEngine, force=True"""
178 t = timeit.Timer(stmt=stmt, setup=setup)
179 print t.timeit(1)
180
181 if __name__ == "__main__":
182 main()
Something went wrong with that request. Please try again.