Clean up benchmark.py and move it to benchmarks/test_inserts.py
1. Removes the cascade=save tests. It's not an option I'd recommend using AND it primarily matters if you have any reference fields in your document, which is not the case in this script. 2. Uses PyMongo-v3.x-style write concern. 3. Removes an old docstring describing some random benchmark run from the past. 4. Removes unused parts of the code. I'll add more tests to the "benchmarks/" directory in future commits.
This commit is contained in:
		
							
								
								
									
										154
									
								
								benchmarks/test_inserts.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										154
									
								
								benchmarks/test_inserts.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,154 @@ | ||||
| import timeit | ||||
|  | ||||
|  | ||||
| def main(): | ||||
|     setup = """ | ||||
| from pymongo import MongoClient | ||||
| connection = MongoClient() | ||||
| connection.drop_database('timeit_test') | ||||
| """ | ||||
|  | ||||
|     stmt = """ | ||||
| from pymongo import MongoClient | ||||
| connection = MongoClient() | ||||
|  | ||||
| db = connection.timeit_test | ||||
| noddy = db.noddy | ||||
|  | ||||
| for i in xrange(10000): | ||||
|     example = {'fields': {}} | ||||
|     for j in range(20): | ||||
|         example['fields']["key"+str(j)] = "value "+str(j) | ||||
|  | ||||
|     noddy.insert_one(example) | ||||
|  | ||||
| myNoddys = noddy.find() | ||||
| [n for n in myNoddys]  # iterate | ||||
| """ | ||||
|  | ||||
|     print "-" * 100 | ||||
|     print """Creating 10000 dictionaries - PyMongo""" | ||||
|     t = timeit.Timer(stmt=stmt, setup=setup) | ||||
|     print '{}s'.format(t.timeit(1)) | ||||
|  | ||||
|     stmt = """ | ||||
| from pymongo import MongoClient, WriteConcern | ||||
| connection = MongoClient() | ||||
|  | ||||
| db = connection.timeit_test | ||||
| noddy = db.noddy.with_options(write_concern=WriteConcern(w=0)) | ||||
|  | ||||
| for i in xrange(10000): | ||||
|     example = {'fields': {}} | ||||
|     for j in range(20): | ||||
|         example['fields']["key"+str(j)] = "value "+str(j) | ||||
|  | ||||
|     noddy.insert_one(example) | ||||
|  | ||||
| myNoddys = noddy.find() | ||||
| [n for n in myNoddys]  # iterate | ||||
| """ | ||||
|  | ||||
|     print "-" * 100 | ||||
|     print """Creating 10000 dictionaries - PyMongo write_concern={"w": 0}""" | ||||
|     t = timeit.Timer(stmt=stmt, setup=setup) | ||||
|     print '{}s'.format(t.timeit(1)) | ||||
|  | ||||
|     setup = """ | ||||
| from pymongo import MongoClient | ||||
| connection = MongoClient() | ||||
| connection.drop_database('timeit_test') | ||||
| connection.close() | ||||
|  | ||||
| from mongoengine import Document, DictField, connect | ||||
| connect("timeit_test") | ||||
|  | ||||
| class Noddy(Document): | ||||
|     fields = DictField() | ||||
| """ | ||||
|  | ||||
|     stmt = """ | ||||
| for i in xrange(10000): | ||||
|     noddy = Noddy() | ||||
|     for j in range(20): | ||||
|         noddy.fields["key"+str(j)] = "value "+str(j) | ||||
|     noddy.save() | ||||
|  | ||||
| myNoddys = Noddy.objects() | ||||
| [n for n in myNoddys]  # iterate | ||||
| """ | ||||
|  | ||||
|     print "-" * 100 | ||||
|     print "Creating 10000 dictionaries - MongoEngine" | ||||
|     t = timeit.Timer(stmt=stmt, setup=setup) | ||||
|     print '{}s'.format(t.timeit(1)) | ||||
|  | ||||
|     stmt = """ | ||||
| for i in xrange(10000): | ||||
|     noddy = Noddy() | ||||
|     fields = {} | ||||
|     for j in range(20): | ||||
|         fields["key"+str(j)] = "value "+str(j) | ||||
|     noddy.fields = fields | ||||
|     noddy.save() | ||||
|  | ||||
| myNoddys = Noddy.objects() | ||||
| [n for n in myNoddys]  # iterate | ||||
| """ | ||||
|  | ||||
|     print "-" * 100 | ||||
|     print "Creating 10000 dictionaries without continual assign - MongoEngine" | ||||
|     t = timeit.Timer(stmt=stmt, setup=setup) | ||||
|     print '{}s'.format(t.timeit(1)) | ||||
|  | ||||
|     stmt = """ | ||||
| for i in xrange(10000): | ||||
|     noddy = Noddy() | ||||
|     for j in range(20): | ||||
|         noddy.fields["key"+str(j)] = "value "+str(j) | ||||
|     noddy.save(write_concern={"w": 0}) | ||||
|  | ||||
| myNoddys = Noddy.objects() | ||||
| [n for n in myNoddys] # iterate | ||||
| """ | ||||
|  | ||||
|     print "-" * 100 | ||||
|     print """Creating 10000 dictionaries - MongoEngine - write_concern={"w": 0}""" | ||||
|     t = timeit.Timer(stmt=stmt, setup=setup) | ||||
|     print '{}s'.format(t.timeit(1)) | ||||
|  | ||||
|     stmt = """ | ||||
| for i in xrange(10000): | ||||
|     noddy = Noddy() | ||||
|     for j in range(20): | ||||
|         noddy.fields["key"+str(j)] = "value "+str(j) | ||||
|     noddy.save(write_concern={"w": 0}, validate=False) | ||||
|  | ||||
| myNoddys = Noddy.objects() | ||||
| [n for n in myNoddys] # iterate | ||||
| """ | ||||
|  | ||||
|     print "-" * 100 | ||||
|     print """Creating 10000 dictionaries - MongoEngine, write_concern={"w": 0}, validate=False""" | ||||
|     t = timeit.Timer(stmt=stmt, setup=setup) | ||||
|     print '{}s'.format(t.timeit(1)) | ||||
|  | ||||
|     stmt = """ | ||||
| for i in xrange(10000): | ||||
|     noddy = Noddy() | ||||
|     for j in range(20): | ||||
|         noddy.fields["key"+str(j)] = "value "+str(j) | ||||
|     noddy.save(force_insert=True, write_concern={"w": 0}, validate=False) | ||||
|  | ||||
| myNoddys = Noddy.objects() | ||||
| [n for n in myNoddys] # iterate | ||||
| """ | ||||
|  | ||||
|     print "-" * 100 | ||||
|     print """Creating 10000 dictionaries - MongoEngine, force_insert=True, write_concern={"w": 0}, validate=False""" | ||||
|     t = timeit.Timer(stmt=stmt, setup=setup) | ||||
|     print '{}s'.format(t.timeit(1)) | ||||
|  | ||||
|  | ||||
| if __name__ == "__main__": | ||||
|     main() | ||||
		Reference in New Issue
	
	Block a user