-
Notifications
You must be signed in to change notification settings - Fork 2
/
demo.py
84 lines (72 loc) · 2.4 KB
/
demo.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
""" Demo of parallel queries using ``gsqlite3``. """
from __future__ import print_function
import os
from functools import partial
from random import randint
from timeit import Timer
import gevent
import sqlite3
import gsqlite3
filename = os.path.join(os.path.dirname(__file__), 'demo.sqlite')
select_stmt = "SELECT * FROM example WHERE value >= ? AND value < ?"
num_rows_to_fetch = 100000
def populate(module, num_rows=1000000):
con = module.connect(filename)
con.execute("CREATE TABLE IF NOT EXISTS example (value)")
(count,) = con.execute("SELECT count(1) FROM example").fetchone()
if count < num_rows:
num_rows -= count
values = [(randint(1, 1000000),) for _ in range(num_rows)]
print("Populating '{}' ...".format(filename))
insert = "INSERT INTO example (value) VALUES (?)"
con.executemany(insert, values)
con.commit()
con.close()
def query(module, lower, upper):
con = module.connect(filename)
cur = con.cursor()
cur.execute(select_stmt, (lower, upper))
cur.fetchmany(num_rows_to_fetch)
con.close()
def query_using_greenlets(module):
g1 = gevent.spawn(query, module, 10, 100)
g2 = gevent.spawn(query, module, 100, 1000)
g3 = gevent.spawn(query, module, 1000, 5000)
gevent.joinall([g1, g2, g3])
def main():
modules = [gsqlite3, sqlite3]
# either module will work here
populate(modules[0])
for module in modules:
time_query_using_module(module)
os.unlink(filename)
return None
def time_query_using_module(module):
# This is largely copied verbatim from the 'timeit' module
repeat = 3
number = 10
verbose = True
precision = 3
stmt = partial(query_using_greenlets, module)
t = Timer(stmt)
try:
r = t.repeat(repeat, number)
except:
t.print_exc()
return 1
best = min(r)
if verbose:
print("raw times:", " ".join(["%.*g" % (precision, x) for x in r]))
print("%s: %d loops," % (module.__name__, number))
usec = best * 1e6 / number
if usec < 1000:
print("best of %d: %.*g usec per loop" % (repeat, precision, usec))
else:
msec = usec / 1000
if msec < 1000:
print("best of %d: %.*g msec per loop" % (repeat, precision, msec))
else:
sec = msec / 1000
print("best of %d: %.*g sec per loop" % (repeat, precision, sec))
if __name__ == '__main__':
main()