From: Jason Kirtland Date: Mon, 8 Oct 2007 18:51:51 +0000 (+0000) Subject: - Expanded insert speed test to cover execute as well as executemany X-Git-Tag: rel_0_4_0~52 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=da45a65a484f69881ca205157687282a9eabbf6e;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - Expanded insert speed test to cover execute as well as executemany --- diff --git a/test/perf/insertspeed.py b/test/perf/insertspeed.py index 4985af68d5..cb6a9bf349 100644 --- a/test/perf/insertspeed.py +++ b/test/perf/insertspeed.py @@ -1,8 +1,8 @@ import testbase +import sys, time from sqlalchemy import * from sqlalchemy.orm import * from testlib import * -import time db = create_engine('sqlite://') metadata = MetaData(db) @@ -12,47 +12,97 @@ Person_table = Table('Person', metadata, Column('age', Integer)) -def sa_unprofiled_inserts(n): - print "Inserting %s records into SQLite(memory) with SQLAlchemy"%n +def sa_unprofiled_insertmany(n): i = Person_table.insert() i.execute([{'name':'John Doe','sex':1,'age':35} for j in xrange(n)]) - s = Person_table.select() -def sqlite_unprofiled_inserts(n): +def sqlite_unprofiled_insertmany(n): conn = db.connect().connection c = conn.cursor() persons = [('john doe', 1, 35) for i in xrange(n)] c.executemany("insert into Person(name, sex, age) values (?,?,?)", persons) -@profiling.profiled('test_many_inserts', always=True) -def test_many_inserts(n): - print "Inserting %s records into SQLite(memory) with SQLAlchemy"%n +@profiling.profiled('sa_profiled_insert_many', always=True) +def sa_profiled_insert_many(n): i = Person_table.insert() i.execute([{'name':'John Doe','sex':1,'age':35} for j in xrange(n)]) s = Person_table.select() r = s.execute() - print "Fetching all rows and columns" res = [[value for value in row] for row in r.fetchall()] - print "Number of records selected: %s\n"%(len(res)) -def all(): +def sqlite_unprofiled_insert(n): + conn = db.connect().connection + c = conn.cursor() + for j in xrange(n): + c.execute("insert into Person(name, sex, age) values (?,?,?)", + ('john doe', 1, 35)) + +def sa_unprofiled_insert(n): + # Another option is to build Person_table.insert() outside of the + # loop. But it doesn't make much of a difference, so might as well + # use the worst-case/naive version here. + for j in xrange(n): + Person_table.insert().execute({'name':'John Doe','sex':1,'age':35}) + +@profiling.profiled('sa_profiled_insert', always=True) +def sa_profiled_insert(n): + i = Person_table.insert() + for j in xrange(n): + i.execute({'name':'John Doe','sex':1,'age':35}) + s = Person_table.select() + r = s.execute() + res = [[value for value in row] for row in r.fetchall()] + +def run_timed(fn, label, *args, **kw): + metadata.drop_all() metadata.create_all() + + sys.stdout.write("%s (%s): " % (label, ', '.join([str(a) for a in args]))) + sys.stdout.flush() + + t = time.clock() + fn(*args, **kw) + t2 = time.clock() + + sys.stdout.write("%0.2f seconds\n" % (t2 - t)) + +def run_profiled(fn, label, *args, **kw): + metadata.drop_all() + metadata.create_all() + + print "%s (%s)" % (label, ', '.join([str(a) for a in args])) + fn(*args, **kw) + +def all(): try: - t = time.clock() - sqlite_unprofiled_inserts(100000) - t2 = time.clock() - print "sqlite unprofiled inserts took %d seconds" % (t2 - t) + print "Bulk INSERTS via executemany():\n" + + run_timed(sqlite_unprofiled_insertmany, + 'pysqlite bulk insert', + 50000) - Person_table.delete().execute() + run_timed(sa_unprofiled_insertmany, + 'SQLAlchemy bulk insert', + 50000) + + run_profiled(sa_profiled_insert_many, + 'SQLAlchemy bulk insert/select, profiled', + 1000) - t = time.clock() - sa_unprofiled_inserts(100000) - t2 = time.clock() - print "sqlalchemy unprofiled inserts took %d seconds" % (t2 - t) + print "\nIndividual INSERTS via execute():\n" - Person_table.delete().execute() + run_timed(sqlite_unprofiled_insert, + "pysqlite individual insert", + 50000) - test_many_inserts(50000) + run_timed(sa_unprofiled_insert, + "SQLAlchemy individual insert", + 50000) + + run_profiled(sa_profiled_insert, + 'SQLAlchemy individual insert/select, profiled', + 1000) + finally: metadata.drop_all()