-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_connection_stability.py
More file actions
391 lines (329 loc) · 11.8 KB
/
test_connection_stability.py
File metadata and controls
391 lines (329 loc) · 11.8 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#!/usr/bin/env python3
"""
Test script to reproduce connection flakiness with iris-pgwire.
This simulates the patterns used by the sim application:
1. Multiple sequential queries
2. Transactions
3. Batch inserts
4. Concurrent connections (if pool > 1)
Run with:
python test_connection_stability.py
"""
import os
import sys
import time
# Try to use psycopg (PostgreSQL client) to connect through iris-pgwire
try:
import psycopg
from psycopg import sql
HAS_PSYCOPG = True
except ImportError:
HAS_PSYCOPG = False
print("psycopg not installed, trying psycopg2...")
try:
import psycopg2
HAS_PSYCOPG2 = True
except ImportError:
HAS_PSYCOPG2 = False
# Configuration
PGWIRE_HOST = os.environ.get("PGWIRE_HOST", "localhost")
PGWIRE_PORT = int(os.environ.get("PGWIRE_PORT", "5432"))
PGWIRE_USER = os.environ.get("PGWIRE_USER", "_SYSTEM")
PGWIRE_PASSWORD = os.environ.get("PGWIRE_PASSWORD", "SYS")
PGWIRE_DATABASE = os.environ.get("PGWIRE_DATABASE", "USER")
NUM_ITERATIONS = 50
CONCURRENT_CONNECTIONS = 3
def get_connection():
"""Get a database connection."""
if HAS_PSYCOPG:
return psycopg.connect(
host=PGWIRE_HOST,
port=PGWIRE_PORT,
user=PGWIRE_USER,
password=PGWIRE_PASSWORD,
dbname=PGWIRE_DATABASE,
autocommit=True,
)
elif HAS_PSYCOPG2:
conn = psycopg2.connect(
host=PGWIRE_HOST,
port=PGWIRE_PORT,
user=PGWIRE_USER,
password=PGWIRE_PASSWORD,
dbname=PGWIRE_DATABASE,
)
conn.autocommit = True
return conn
else:
raise RuntimeError("No PostgreSQL driver available (psycopg or psycopg2)")
def test_simple_queries(num_iterations: int = NUM_ITERATIONS) -> dict:
"""Test simple SELECT queries in sequence."""
print(f"\n=== Test 1: Simple Queries ({num_iterations} iterations) ===")
results = {"success": 0, "failure": 0, "errors": []}
conn = None
try:
conn = get_connection()
cursor = conn.cursor()
for i in range(num_iterations):
try:
cursor.execute("SELECT 1 AS test")
row = cursor.fetchone()
if row and row[0] == 1:
results["success"] += 1
else:
results["failure"] += 1
results["errors"].append(f"Iteration {i}: Unexpected result {row}")
except Exception as e:
results["failure"] += 1
results["errors"].append(f"Iteration {i}: {type(e).__name__}: {e}")
# Try to reconnect
try:
conn.close()
except:
pass
conn = get_connection()
cursor = conn.cursor()
except Exception as e:
results["errors"].append(f"Connection error: {type(e).__name__}: {e}")
finally:
if conn:
try:
conn.close()
except:
pass
print(f" Success: {results['success']}, Failure: {results['failure']}")
if results["errors"]:
for err in results["errors"][:5]:
print(f" Error: {err}")
return results
def test_transactions(num_iterations: int = 10) -> dict:
"""Test transaction patterns."""
print(f"\n=== Test 2: Transactions ({num_iterations} iterations) ===")
results = {"success": 0, "failure": 0, "errors": []}
conn = None
try:
conn = get_connection()
conn.autocommit = False
cursor = conn.cursor()
# Create test table
conn.autocommit = True
try:
cursor.execute("DROP TABLE IF EXISTS test_stability")
cursor.execute(
"""
CREATE TABLE test_stability (
id INT PRIMARY KEY,
value VARCHAR(100),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
"""
)
except Exception as e:
print(f" Warning: Could not create table: {e}")
conn.autocommit = False
for i in range(num_iterations):
try:
cursor.execute("BEGIN")
cursor.execute(
"INSERT INTO test_stability (id, value) VALUES (%s, %s)", (i, f"test_value_{i}")
)
cursor.execute("SELECT COUNT(*) FROM test_stability")
cursor.execute("COMMIT")
results["success"] += 1
except Exception as e:
results["failure"] += 1
results["errors"].append(f"Iteration {i}: {type(e).__name__}: {e}")
try:
cursor.execute("ROLLBACK")
except:
pass
# Try to reconnect
try:
conn.close()
except:
pass
conn = get_connection()
conn.autocommit = False
cursor = conn.cursor()
except Exception as e:
results["errors"].append(f"Connection error: {type(e).__name__}: {e}")
finally:
if conn:
try:
conn.autocommit = True
cursor = conn.cursor()
cursor.execute("DROP TABLE IF EXISTS test_stability")
conn.close()
except:
pass
print(f" Success: {results['success']}, Failure: {results['failure']}")
if results["errors"]:
for err in results["errors"][:5]:
print(f" Error: {err}")
return results
def test_batch_inserts(batch_size: int = 10, num_batches: int = 5) -> dict:
"""Test batch insert patterns (multi-row VALUES)."""
print(f"\n=== Test 3: Batch Inserts ({num_batches} batches of {batch_size}) ===")
results = {"success": 0, "failure": 0, "errors": []}
conn = None
try:
conn = get_connection()
cursor = conn.cursor()
# Create test table
try:
cursor.execute("DROP TABLE IF EXISTS test_batch")
cursor.execute(
"""
CREATE TABLE test_batch (
id INT,
value VARCHAR(100)
)
"""
)
except Exception as e:
print(f" Warning: Could not create table: {e}")
for batch_num in range(num_batches):
try:
# Build multi-row INSERT
values = []
params = []
for i in range(batch_size):
row_id = batch_num * batch_size + i
values.append("(%s, %s)")
params.extend([row_id, f"batch_{batch_num}_row_{i}"])
sql_stmt = f"INSERT INTO test_batch (id, value) VALUES {', '.join(values)}"
cursor.execute(sql_stmt, params)
results["success"] += 1
except Exception as e:
results["failure"] += 1
results["errors"].append(f"Batch {batch_num}: {type(e).__name__}: {e}")
# Try to reconnect
try:
conn.close()
except:
pass
conn = get_connection()
cursor = conn.cursor()
except Exception as e:
results["errors"].append(f"Connection error: {type(e).__name__}: {e}")
finally:
if conn:
try:
cursor = conn.cursor()
cursor.execute("DROP TABLE IF EXISTS test_batch")
conn.close()
except:
pass
print(f" Success: {results['success']}, Failure: {results['failure']}")
if results["errors"]:
for err in results["errors"][:5]:
print(f" Error: {err}")
return results
def test_rapid_reconnection(num_connections: int = 20) -> dict:
"""Test rapid connection/disconnection patterns."""
print(f"\n=== Test 4: Rapid Reconnection ({num_connections} connections) ===")
results = {"success": 0, "failure": 0, "errors": []}
for i in range(num_connections):
conn = None
try:
conn = get_connection()
cursor = conn.cursor()
cursor.execute("SELECT 1")
cursor.fetchone()
conn.close()
results["success"] += 1
except Exception as e:
results["failure"] += 1
results["errors"].append(f"Connection {i}: {type(e).__name__}: {e}")
finally:
if conn:
try:
conn.close()
except:
pass
print(f" Success: {results['success']}, Failure: {results['failure']}")
if results["errors"]:
for err in results["errors"][:5]:
print(f" Error: {err}")
return results
def test_long_running_connection(duration_seconds: int = 10) -> dict:
"""Test a long-running connection with periodic queries."""
print(f"\n=== Test 5: Long Running Connection ({duration_seconds}s) ===")
results = {"success": 0, "failure": 0, "errors": []}
conn = None
try:
conn = get_connection()
cursor = conn.cursor()
start_time = time.time()
query_count = 0
while time.time() - start_time < duration_seconds:
try:
cursor.execute("SELECT CURRENT_TIMESTAMP")
cursor.fetchone()
results["success"] += 1
query_count += 1
time.sleep(0.1) # 100ms between queries
except Exception as e:
results["failure"] += 1
results["errors"].append(f"Query {query_count}: {type(e).__name__}: {e}")
# Try to reconnect
try:
conn.close()
except:
pass
conn = get_connection()
cursor = conn.cursor()
except Exception as e:
results["errors"].append(f"Connection error: {type(e).__name__}: {e}")
finally:
if conn:
try:
conn.close()
except:
pass
print(f" Success: {results['success']}, Failure: {results['failure']}")
if results["errors"]:
for err in results["errors"][:5]:
print(f" Error: {err}")
return results
def main():
print("=" * 60)
print("iris-pgwire Connection Stability Test")
print("=" * 60)
print(f"Target: {PGWIRE_HOST}:{PGWIRE_PORT}")
print(f"User: {PGWIRE_USER}")
print(f"Database: {PGWIRE_DATABASE}")
if not HAS_PSYCOPG and not HAS_PSYCOPG2:
print("\nERROR: No PostgreSQL driver available!")
print("Install with: pip install psycopg[binary] or pip install psycopg2-binary")
sys.exit(1)
driver = "psycopg3" if HAS_PSYCOPG else "psycopg2"
print(f"Driver: {driver}")
all_results = {}
# Run tests
all_results["simple_queries"] = test_simple_queries()
all_results["transactions"] = test_transactions()
all_results["batch_inserts"] = test_batch_inserts()
all_results["rapid_reconnection"] = test_rapid_reconnection()
all_results["long_running"] = test_long_running_connection()
# Summary
print("\n" + "=" * 60)
print("SUMMARY")
print("=" * 60)
total_success = 0
total_failure = 0
for test_name, results in all_results.items():
total_success += results["success"]
total_failure += results["failure"]
status = "✅" if results["failure"] == 0 else "❌"
print(f"{status} {test_name}: {results['success']} success, {results['failure']} failure")
print("-" * 60)
print(f"Total: {total_success} success, {total_failure} failure")
if total_failure > 0:
print("\n⚠️ Connection instability detected!")
sys.exit(1)
else:
print("\n✅ All tests passed!")
sys.exit(0)
if __name__ == "__main__":
main()