-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbench.py
93 lines (87 loc) · 2.57 KB
/
bench.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
import sqlite3, time, psycopg2
def withSqliteConnection(dbFileName, f, isolationLevel, useWal = False, timeout = 60):
try:
conn = sqlite3.connect(dbFileName, timeout)
conn.isolation_level = isolationLevel
if useWal:
print('Using WAL...')
conn.execute("PRAGMA journal_mode=WAL")
else:
print('Using rollback journal...')
f(conn)
finally:
if conn:
conn.close()
def withPgsqlConnection(f):
try:
conn = psycopg2.connect(
database = "testDb", port = "5431", host = "/tmp"
)
f(conn)
conn.commit()
finally:
if conn:
conn.close()
def createTableSqlite(conn):
primariKeySpec = "integer primary key autoincrement"
conn.execute("""
PRAGMA foreign_keys = ON
""")
cur = conn.cursor()
cur.execute("""
create table departments (
department_id %s,
department_name text unique not null,
created text not null
)
""" % primariKeySpec)
createTableCommon(conn, primariKeySpec)
def createTablePgsql(conn):
primariKeySpec = "serial primary key"
cur = conn.cursor()
cur.execute("""
create table departments (
department_id %s,
department_name text unique not null,
created timestamp not null
)
""" % primariKeySpec)
createTableCommon(conn, primariKeySpec)
conn.commit()
def createTableCommon(conn, primariKeySpec):
cur = conn.cursor()
cur.execute("""
create table addresses (
address_id %s,
address text unique not null
)
""" % primariKeySpec)
cur.execute("""
create table users (
user_id %s,
address_id integer not null,
user_name text unique not null,
first_name text not null,
last_name text not null,
created text not null,
foreign key (address_id) references addresses(address_id)
)
""" % primariKeySpec)
cur.execute("""
create table user_department (
user_department_id %s,
user_id integer not null,
department_id integer not null,
unique ( user_id, department_id),
foreign key (user_id) references users(user_id),
foreign key (department_id) references departments(department_id)
)
""" % primariKeySpec)
cur.execute("""
create index department_created on departments ( created )
""")
def withStopwatch(title, f):
start = time.time()
print('%s started...' % title)
f()
print('%s %.3f secs' % (title, time.time() - start))