1 | from psycopg2 import connect as _pg
|
---|
2 | from sqlite3 import connect as _lite
|
---|
3 | from MySQLdb import connect as _mysql
|
---|
4 |
|
---|
5 | def mysql():
|
---|
6 | con = _mysql()
|
---|
7 | con.select_db('shai')
|
---|
8 | return con
|
---|
9 |
|
---|
10 | def sqlite():
|
---|
11 | return _lite('sqlite.db')
|
---|
12 |
|
---|
13 | def pg():
|
---|
14 | return _pg("")
|
---|
15 |
|
---|
16 | def prepare(connection):
|
---|
17 | cursor = connection.cursor()
|
---|
18 | cursor.execute("""create table test(
|
---|
19 | testid int primary key,
|
---|
20 | test2 char(10) not null,
|
---|
21 | test3 varchar(50) not null
|
---|
22 | )""")
|
---|
23 | def fill_up(connection):
|
---|
24 | cursor = connection.cursor()
|
---|
25 | cursor.execute("delete from test where 1=1")
|
---|
26 | s = '0123456789'
|
---|
27 | t = 'abc'
|
---|
28 | for i in range(100000):
|
---|
29 | stmt = "insert into test values(%d, '%s', '%s')" % \
|
---|
30 | (i, s, t)
|
---|
31 | cursor.execute(stmt)
|
---|
32 | s = s[-1:]+s[:-1] # rotate
|
---|
33 | t = (t+ 'x'+ str(i))[-49:]
|
---|
34 | connection.commit()
|
---|
35 |
|
---|
36 | def warm_up(connection):
|
---|
37 | cursor = connection.cursor()
|
---|
38 | for i in range(1, 100000, 1017):
|
---|
39 | cursor.execute("select * from test where testid=%d" % (i,))
|
---|
40 | cursor.fetchall()
|
---|
41 | connection.rollback()
|
---|
42 | for i in range(1, 100000, 1017):
|
---|
43 | cursor.execute("select * from test where testid=%d" % (i,))
|
---|
44 | cursor.fetchall()
|
---|
45 | connection.commit()
|
---|
46 |
|
---|
47 | def bench(connection, end_tran):
|
---|
48 | cursor = connection.cursor()
|
---|
49 | for i in range(7, 99990, 69):
|
---|
50 | cursor.execute("select * from test where testid=%d" % (i,))
|
---|
51 | cursor.fetchall()
|
---|
52 | cursor.execute("select * from test where testid=%d" % (i+1,))
|
---|
53 | cursor.fetchall()
|
---|
54 | cursor.execute("select * from test where testid=%d" % (i+6,))
|
---|
55 | cursor.fetchall()
|
---|
56 | end_tran()
|
---|
57 |
|
---|
58 | def leave_open(connection):
|
---|
59 | def noop(): pass
|
---|
60 | bench(connection, noop)
|
---|
61 |
|
---|
62 | def commit(connection):
|
---|
63 | bench(connection, connection.commit)
|
---|
64 |
|
---|
65 | def rollback(connection):
|
---|
66 | bench(connection, connection.rollback)
|
---|
67 |
|
---|
68 | if __name__=='__main__':
|
---|
69 | fill_up(mysql())
|
---|
70 | fill_up(sqlite())
|
---|
71 | fill_up(pg())
|
---|
72 | print "styles:"
|
---|
73 | print "pg", _pg.__module__.paramstyle
|
---|
74 | print "mysql", _mysql.__module__.paramstyle
|
---|
75 | print "sqlite", _lite.__module__.paramstyle
|
---|
76 |
|
---|
77 |
|
---|
78 |
|
---|