-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
260 lines (223 loc) · 8.07 KB
/
app.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import os
if os.environ.get("ENABLE_MONKEY_PATCH_ALL", None):
import eventlet
eventlet.monkey_patch()
print("monkey patched!")
import inspect
import itertools
import urllib
import time
import cx_Oracle
import eventlet.db_pool
import pyodbc
import traceback
import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from flask import Flask
from flask_socketio import SocketIO
from toolz.curried import concat
app = Flask(__name__)
if not os.environ.get("ASYNC_MODE_THREADING"):
socketio = SocketIO(app)
else:
socketio = SocketIO(app, async_mode="threading")
if os.environ.get("ENABLE_DEBUG", None):
import pdb
pdb.set_trace()
class ConnectionPoolWrappedDefaultCons(eventlet.db_pool.ConnectionPool):
def __init__(self, db_module, *args, **kwargs):
super_pos_defaults = {
k: v.default
for k, v in inspect.signature(
super(ConnectionPoolWrappedDefaultCons, self,).__init__
).parameters.items()
if (
v.kind
in (
inspect.Parameter.POSITIONAL_ONLY,
inspect.Parameter.POSITIONAL_OR_KEYWORD,
)
and v.default is not inspect.Parameter.empty
)
}
min_size = kwargs.pop("min_size", super_pos_defaults.get("min_size", 0))
max_size = kwargs.pop("max_size", super_pos_defaults.get("max_size", 4))
max_idle = kwargs.pop("max_idle", super_pos_defaults.get("max_idle", 10))
max_age = kwargs.pop("max_age", super_pos_defaults.get("max_age", 30))
connect_timeout = kwargs.pop(
"connect_timeout", super_pos_defaults.get("connect_timeout", 5)
)
cleanup = kwargs.pop(
"cleanup",
super_pos_defaults.get("cleanup", eventlet.db_pool.cleanup_rollback),
)
super(ConnectionPoolWrappedDefaultCons, self).__init__(
db_module,
min_size,
max_size,
max_idle,
max_age,
connect_timeout,
cleanup,
*args,
**kwargs
)
class ConnectionPoolWithoutTime(ConnectionPoolWrappedDefaultCons):
def create(self):
return super(ConnectionPoolWithoutTime, self).create()[2]
creator_oracle = ConnectionPoolWithoutTime(
cx_Oracle,
os.environ["ORACLE_USER"],
os.environ["ORACLE_PASS"],
"{}/{}".format(os.environ["ORACLE_HOST"], os.environ["ORACLE_SID"],),
mode=cx_Oracle.SYSDBA,
)
cstr_oracle = "oracle+cx_oracle://{}:{}@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST={})(PORT={})))(CONNECT_DATA=(SERVICE_NAME={})(SERVER=DEDICATED)))".format(
urllib.parse.quote_plus(os.environ["ORACLE_USER"]),
urllib.parse.quote_plus(os.environ["ORACLE_PASS"]),
urllib.parse.quote_plus(os.environ["ORACLE_HOST"]),
urllib.parse.quote_plus(os.environ["ORACLE_PORT"]),
urllib.parse.quote_plus(os.environ["ORACLE_SERVICE_NAME"]),
)
db_engine_oracle = create_engine(cstr_oracle, **{"creator": creator_oracle.create})
conn_str_mssql = "DRIVER={{{}}};SERVER={};DATABASE={};UID={};PWD={}".format(
os.environ["MSSQL_DRIVER"],
os.environ["MSSQL_HOST"],
os.environ["MSSQL_CATALOG"],
os.environ["MSSQL_USER"],
os.environ["MSSQL_PASS"],
)
cstr = "mssql+pyodbc:///?odbc_connect={}".format(
urllib.parse.quote_plus(conn_str_mssql)
)
mssql_creator = ConnectionPoolWithoutTime(
pyodbc,
"DRIVER={{{}}};SERVER={};DATABASE={};UID={};PWD={{{}}}".format(
os.environ["MSSQL_DRIVER"],
os.environ["MSSQL_HOST"],
os.environ["MSSQL_CATALOG"],
os.environ["MSSQL_USER"],
os.environ["MSSQL_PASS"],
),
)
if not os.environ.get("MSSQL_USE_CUSTOM_EVENTLET_POOL_CREATOR"):
db_engine_mssql = create_engine(cstr)
else:
print("Using custom eventlet eventlet pool creator")
db_engine_mssql = create_engine(
cstr, **{"creator": mssql_creator.create}, max_overflow=-1
)
db_engine_postgresql = create_engine(
"postgresql+psycopg2://{}:{}@{}:{}/{}".format(
urllib.parse.quote_plus(os.environ["PSQL_USER"]),
urllib.parse.quote_plus(os.environ["PSQL_PASS"]),
urllib.parse.quote_plus(os.environ["PSQL_HOST"]),
urllib.parse.quote_plus(os.environ["PSQL_PORT"]),
urllib.parse.quote_plus(os.environ["PSQL_SCHEMA"]),
)
)
db_sessionmaker_oracle = sessionmaker(bind=db_engine_oracle)
db_sessionmaker_mssql = sessionmaker(bind=db_engine_mssql)
db_sessionmaker_postgresql = sessionmaker(bind=db_engine_postgresql)
@app.route("/api/test")
def api_test(*args, **kwargs):
return "api/test: done\n"
@app.route("/api/busy/cpu")
def api_busy(*args, **kwargs):
print("pre busy")
socketio.sleep(15)
print("post busy")
return "busy: done\n"
def _fetchall_with_sleep(result_proxy):
while True:
recs = result_proxy.fetchmany(size=1000)
if not recs:
break
yield recs
socketio.sleep(0)
def fetchall_with_sleep(result_proxy):
for x in concat(_fetchall_with_sleep(result_proxy)):
yield x
socketio.sleep(0)
@app.route("/api/busy/oracle")
def api_busy_oracle(*args, **kwargs):
sess_oracle = None
try:
print("pre busy oracle")
sess_oracle = db_sessionmaker_oracle()
recs = sess_oracle.execute(
"""
with x as (select rownum-1 r from dual connect by rownum <= 10)
select ones.r + 10*tens.r + 100*hundreds.r + 1000*thousands.r + 10000*tenthous.r + 100000*hunthous.r + 1000000*mil.r n
from x ones, x tens, x hundreds, x thousands, x tenthous, x hunthous, x mil
order by 1"""
)
# recs = sess_oracle.execute('select 1 as n from dual')
print(
", ".join(
str(x[0]) for x in itertools.islice(fetchall_with_sleep(recs), 10)
)
)
print("post busy oracle")
return "busy-oracle: done\n"
except sqlalchemy.exc.TimeoutError as ex:
print(ex)
return traceback.format_exc()
finally:
if sess_oracle:
sess_oracle.close()
@app.route("/api/busy/mssql")
def api_busy_mssql(*args, **kwargs):
sess_mssql = None
try:
print("pre busy mssql")
sess_mssql = db_sessionmaker_mssql()
recs = sess_mssql.execute(
"""
WITH x AS (SELECT n FROM (VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) v(n))
SELECT ones.n + 10*tens.n + 100*hundreds.n + 1000*thousands.n + 10000*tenthous.n + 100000*hunthous.n + 1000000*mil.n n
FROM x ones, x tens, x hundreds, x thousands, x tenthous, x hunthous, x mil
ORDER BY 1"""
)
# recs = sess_mssql.execute('select 1 as n')
print(
", ".join(
str(x[0]) for x in itertools.islice(fetchall_with_sleep(recs), 10)
)
)
print("post busy mssql")
return "busy-mssql: done\n"
except sqlalchemy.exc.TimeoutError as ex:
print(ex)
return traceback.format_exc()
finally:
if sess_mssql:
sess_mssql.close()
@app.route("/api/busy/postgresql")
def api_busy_postgresql(*args, **kwargs):
sess_psql = None
try:
print("pre busy postgresql")
sess_psql = db_sessionmaker_postgresql()
recs = sess_psql.execute("""SELECT * FROM generate_series(1,10000000)""")
#print(", ".join(str(x[0]) for x in recs.fetchall()[:10]))
print(", ".join(
str(x[0]) for x in itertools.islice(fetchall_with_sleep(recs), 10)
))
print("post busy postgresql")
return "busy-postgresql: done\n"
except sqlalchemy.exc.TimeoutError as ex:
print(ex)
#return traceback.format_exc()
return str(ex)
finally:
if sess_psql:
sess_psql.close()
if __name__ == "__main__":
run_kw = {"use_reloader": os.environ.get("FLASK_USE_RELOADER", False)}
if os.environ.get("FLASK_HOST"):
run_kw["host"] = os.environ.get("FLASK_HOST")
if os.environ.get("FLASK_PORT"):
run_kw["port"] = os.environ.get("FLASK_PORT")
socketio.run(app, **run_kw)