-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.py
63 lines (48 loc) · 1.71 KB
/
benchmark.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
import gevent
from gevent import monkey
monkey.patch_all()
import os
import timeit
import sqlalchemy
import sqlalchemy.event
import sqlalchemy.pool
from sqlalchemy import Table, Column, Integer, String, MetaData
try:
from pysqlite2 import dbapi2
except ImportError:
from sqlite3 import dbapi2
print(dbapi2.sqlite_version_info)
if os.path.exists('tt.db'):
os.remove('tt.db')
def set_sqlite_pragma(dbapi_connection, connection_record):
dbapi_connection.isolation_level = None
cursor = dbapi_connection.cursor()
cursor.execute('PRAGMA journal_mode = WAL')
print cursor.fetchone()
cursor.execute('PRAGMA synchronous = NORMAL')
dbapi_connection.commit()
metadata = MetaData()
records = Table(
'records', metadata,
Column('id', Integer, primary_key=True),
Column('code', String)
)
engine = sqlalchemy.create_engine('sqlite:///tt.db', module=dbapi2, poolclass=sqlalchemy.pool.StaticPool)
sqlalchemy.event.listen(engine, 'connect', set_sqlite_pragma)
metadata.create_all(engine)
def block_test(times=100, run_foo=False):
def ins(n):
with engine.connect() as conn:
conn.execute(records.insert().values(code=n))
def foo():
for j in range(5):
print(j)
gevent.sleep(0.1)
greens = [gevent.spawn(ins, i) for i in range(times)]
if run_foo:
greens.append(gevent.spawn(foo))
gevent.joinall(greens)
print(timeit.Timer(stmt='block_test(times=1000)', setup='from __main__ import block_test').timeit(number=1))
print(engine.execute(records.count()).fetchone())
print(timeit.Timer(stmt='block_test(times=1000, run_foo=True)', setup='from __main__ import block_test').timeit(number=1))
print(engine.execute(records.count()).fetchone())