forked from feiskyer/python-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheventlet_db_profile.py
More file actions
executable file
·55 lines (49 loc) · 1.35 KB
/
eventlet_db_profile.py
File metadata and controls
executable file
·55 lines (49 loc) · 1.35 KB
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
#!/usr/bin/env python
import eventlet
import time
import random
import MySQLdb
import eventlet.db_pool as db_pool
from DBUtils.PooledDB import PooledDB
conn_kwargs={'host':'10.241.226.41', 'user':'root', 'passwd':'feisky','db':'news', 'charset': 'utf8'}
sql="""select * from news limit 1"""
pooled=db_pool.ConnectionPool(MySQLdb, **conn_kwargs)
pooldb=PooledDB(MySQLdb, **conn_kwargs)
def query(conn):
cur=conn.cursor()
#cur.execute(sql % (random.randint(1,1000)))
cur.execute(sql)
data=cur.fetchall()
cur.close()
return data
def test_mysqldb(times):
now = time.time()
for i in range(times):
conn=MySQLdb.connect(**conn_kwargs)
r=query(conn)
print r
conn.close()
print 'MySQLdb: ', time.time()-now
def test_eventlet(times):
now = time.time()
for i in range(times):
conn=pooled.get()
try:
r=query(conn)
print r
finally:
pooled.put(conn)
print 'Eventlet: ', time.time()-now
def test_dbutils(times):
now = time.time()
for i in range(times):
conn=pooldb.connection()
try:
r=query(conn)
print r
finally:
conn.close()
print 'DBUtils: ', time.time()-now
#test_mysqldb(10000) # 50s
#test_eventlet(10000) # 31s
test_dbutils(10000) # 30s