-
Notifications
You must be signed in to change notification settings - Fork 73
/
speed.py
191 lines (146 loc) · 6.9 KB
/
speed.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# -*- coding: utf-8 -*-
# A script used to check performance of pypyodbc vs similar other modules, like pyodbc
# Let Python load it's ODBC connecting tool pypyodbc
from __future__ import print_function
import os, os.path, time, sys, gc
if len(sys.argv) < 2:
usage = '''
usage: [the python interpreter] speed.py [the module to be benchmarked]
for example:
python speed.py pypyodbc [connection_string]
python speed.py pyodbc [connection_string]
pypy speed.py pypyodbc [connection_string]
ipy speed.py pypyodbc [connection_string]
'''
print (usage)
exit()
localDir = os.path.dirname(os.path.realpath(sys.argv[0]))
if len(sys.argv) >= 3:
connection_string = sys.argv[2]
test_mdb = False
else:
test_mdb = True
if test_mdb is True:
from pypyodbc import win_create_mdb
temp_folder = localDir+'\\pypyodbc_mdb_test\\'
if not os.path.exists(temp_folder):
os.mkdir(temp_folder)
else:
connection_string_copy = connection_string
for x in range(4):
print ('file: '+str(x))
pypyodbc = __import__(sys.argv[1])
#pypyodbc.pooling = False
print ('Running with pypyodbc %s' %pypyodbc.version)
fix = str(x%10)
if test_mdb is True:
if os.path.exists(temp_folder + u'PerformanceTest'+fix+'.mdb'):
os.remove(temp_folder + u'PerformanceTest'+fix+'.mdb')
if os.path.exists(temp_folder + u'PerformanceTest'+fix+'c.mdb'):
os.remove(temp_folder + u'PerformanceTest'+fix+'c.mdb')
if os.path.exists(temp_folder + u'PerformanceTest'+fix+'copy.mdb'):
os.remove(temp_folder + u'PerformanceTest'+fix+'copy.mdb')
win_create_mdb( temp_folder + u'PerformanceTest'+fix+'.mdb' )
conn = pypyodbc.connect(u'Driver={Microsoft Access Driver (*.mdb)};DBQ='+temp_folder + u'PerformanceTest'+fix+'.mdb')
else:
conn = pypyodbc.connect(connection_string)
print (conn.getinfo(pypyodbc.SQL_DRIVER_NAME))
cur = conn.cursor()
if test_mdb:
cur.execute(u"""create table saleout (ID COUNTER PRIMARY KEY, customer_name varchar(255),
product_name varchar(255),
price float,
volume int,
sell_time datetime);""")
else:
cur.execute(u"""IF OBJECT_ID('dbo.saleout', 'U') IS NOT NULL DROP TABLE dbo.saleout;""")
cur.execute(u"""create table saleout (ID INT IDENTITY(1,1) PRIMARY KEY, customer_name varchar(255),
product_name varchar(255),
price float,
volume int,
sell_time datetime);""")
conn.commit()
t_begin = time.time()
for b in range(5000):
cur.executemany(u'''INSERT INTO saleout(customer_name,product_name,price,volume,sell_time)
VALUES(?,?,?,?,?)''', [(u'杨天真','Apple IPhone 5','5500.1',1,'2012-1-21'),
(u'郑现实','Huawei Ascend D2',None,1,'2012-1-21'),
(u'莫小闵','Huawei Ascend D2','5000.5',2,'2012-1-21'),
(u'顾小白','Huawei Ascend D2','5000.5',None,'2012-1-22')])
cur.commit()
if b%100 == 0:
print (b*4, end='\r')
#cur.execute('INSERT INTO saleout (customer_name,product_name,price,volume,sell_time) SELECT customer_name,product_name,price,volume,sell_time FROM saleout;')
cur.commit()
#cur.close()
conn.commit()
#conn.close()
write_time = time.time() - t_begin
print ('\tWrite time: '+str(write_time))
#pypyodbc.win_compact_mdb('D:\\pypyodbc_mdb_test\\PerformanceTest'+fix+'.mdb','D:\\pypyodbc_mdb_test\\PerformanceTest'+fix+'c.mdb')
if test_mdb is True:
conn = pypyodbc.connect(u'Driver={Microsoft Access Driver (*.mdb)};DBQ='+temp_folder + u'PerformanceTest'+fix+'.mdb',unicode_results=True)
#conn = pypyodbc.win_connect_mdb('D:\\pypyodbc_mdb_test\\PerformanceTest'+fix+'.mdb')
win_create_mdb( temp_folder + u'PerformanceTest'+fix+'copy.mdb' )
conn_copy = pypyodbc.connect(u'Driver={Microsoft Access Driver (*.mdb)};DBQ='+temp_folder + u'PerformanceTest'+fix+'copy.mdb')
else:
conn = pypyodbc.connect(connection_string)
conn_copy = pypyodbc.connect(connection_string_copy)
cur = conn.cursor()
cur_copy = conn_copy.cursor()
if test_mdb:
cur_copy.execute(u"""create table saleout_copy (ID COUNTER PRIMARY KEY, customer_name varchar(255),
product_name varchar(255),
price float,
volume int,
sell_time datetime);""")
else:
cur_copy.execute(u"""IF OBJECT_ID('dbo.saleout_copy', 'U') IS NOT NULL DROP TABLE dbo.saleout_copy;""")
cur_copy.execute(u"""create table saleout_copy (ID INT IDENTITY PRIMARY KEY, customer_name varchar(255),
product_name varchar(255),
price float,
volume int,
sell_time datetime);""")
t_begin = time.time()
cur.execute('select customer_name,product_name,price,volume,sell_time from saleout')
r = cur.fetchmany(4)
r_n = 0
while r:
#if r_n == 0:
# print (r['product_name'],r[:])
cur_copy.executemany('''INSERT INTO saleout_copy(customer_name,product_name,price,volume,sell_time)
VALUES(?,?,?,?,?)''', r)
cur_copy.commit()
if r_n % 400 == 0:
print (r_n, end='\r')
r = cur.fetchmany(4)
r_n +=4
#cur_copy.close()
cur_copy.commit()
#conn_copy.close()
#conn.close()
R_W_time = time.time() - t_begin
print ('\tR & W time: '+str(time.time() - t_begin))
t_begin = time.time()
if test_mdb is True:
conn = pypyodbc.connect(u'Driver={Microsoft Access Driver (*.mdb)};DBQ='+temp_folder + u'PerformanceTest'+fix+'copy.mdb',unicode_results=True)
#conn = pypyodbc.win_connect_mdb('D:\\pypyodbc_mdb_test\\PerformanceTest'+fix+'.mdb')
else:
conn = pypyodbc.connect(connection_string_copy)
cur = conn.cursor()
cur.execute('select customer_name,product_name,price,volume,sell_time from saleout_copy')
r = cur.fetchmany(4)
r_n = 0
while r:
#if r_n == 0:
# print (r['product_name'],r[:])
if r_n % 400 == 0:
print (r_n, end='\r')
r = cur.fetchmany(4)
r_n +=4
#cur_copy.close()
#conn.close()
print ('\tRead time: '+str(time.time() - t_begin))
print ("garbage collected:" + str(gc.collect()), end = ' ')
print ("garbage count:" + str(gc.garbage.__len__()), end = ' ')
print ("garbage:" + str(gc.garbage))