-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathbench_mssql.py
More file actions
770 lines (673 loc) · 25.5 KB
/
bench_mssql.py
File metadata and controls
770 lines (673 loc) · 25.5 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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
import atexit
import pyodbc
import os
import sys
import threading
import time
import mssql_python
CONNECTION_STRING = "Driver={ODBC Driver 18 for SQL Server};" + os.environ.get(
"DB_CONNECTION_STRING"
)
def setup_database():
print("Setting up the database...")
conn = pyodbc.connect(CONNECTION_STRING)
cursor = conn.cursor()
try:
# Drop permanent tables and stored procedure if they exist
print("Dropping existing tables and stored procedure if they exist...")
cursor.execute(
"""
IF OBJECT_ID('perfbenchmark_child_table', 'U') IS NOT NULL DROP TABLE perfbenchmark_child_table;
IF OBJECT_ID('perfbenchmark_parent_table', 'U') IS NOT NULL DROP TABLE perfbenchmark_parent_table;
IF OBJECT_ID('perfbenchmark_table', 'U') IS NOT NULL DROP TABLE perfbenchmark_table;
IF OBJECT_ID('perfbenchmark_stored_procedure', 'P') IS NOT NULL DROP PROCEDURE perfbenchmark_stored_procedure;
"""
)
# Create permanent tables with new names
print("Creating tables...")
cursor.execute(
"""
CREATE TABLE perfbenchmark_table (
id INT,
name NVARCHAR(50),
age INT
)
"""
)
cursor.execute(
"""
CREATE TABLE perfbenchmark_parent_table (
id INT PRIMARY KEY,
name NVARCHAR(50)
)
"""
)
cursor.execute(
"""
CREATE TABLE perfbenchmark_child_table (
id INT PRIMARY KEY,
parent_id INT,
description NVARCHAR(100),
FOREIGN KEY (parent_id) REFERENCES perfbenchmark_parent_table(id)
)
"""
)
# Create stored procedure
print("Creating stored procedure...")
cursor.execute(
"""
CREATE PROCEDURE perfbenchmark_stored_procedure
AS
BEGIN
SELECT * FROM perfbenchmark_table;
END
"""
)
conn.commit()
print("Database setup completed.")
finally:
cursor.close()
conn.close()
# Call setup_database to ensure permanent tables and procedure are recreated
setup_database()
def cleanup_database():
print("Cleaning up the database...")
conn = pyodbc.connect(CONNECTION_STRING)
cursor = conn.cursor()
try:
# Drop tables and stored procedure after benchmarks
print("Dropping tables and stored procedure...")
cursor.execute(
"""
IF OBJECT_ID('perfbenchmark_child_table', 'U') IS NOT NULL DROP TABLE perfbenchmark_child_table;
IF OBJECT_ID('perfbenchmark_parent_table', 'U') IS NOT NULL DROP TABLE perfbenchmark_parent_table;
IF OBJECT_ID('perfbenchmark_table', 'U') IS NOT NULL DROP TABLE perfbenchmark_table;
IF OBJECT_ID('perfbenchmark_stored_procedure', 'P') IS NOT NULL DROP PROCEDURE perfbenchmark_stored_procedure;
"""
)
conn.commit()
print("Database cleanup completed.")
finally:
cursor.close()
conn.close()
# Register cleanup function to run at exit
atexit.register(cleanup_database)
# Define benchmark functions for pyodbc
def bench_select_pyodbc():
print("Running SELECT benchmark with pyodbc...")
conn = pyodbc.connect(CONNECTION_STRING)
cursor = conn.cursor()
# Clear plan and data cache
cursor.execute("CHECKPOINT;")
cursor.execute("DBCC DROPCLEANBUFFERS;")
cursor.execute("DBCC FREEPROCCACHE;")
cursor.execute("SELECT * FROM perfbenchmark_table")
cursor.fetchall()
cursor.close()
conn.close()
print("SELECT benchmark with pyodbc completed.")
def bench_insert_pyodbc():
print("Running INSERT benchmark with pyodbc...")
try:
conn = pyodbc.connect(CONNECTION_STRING)
cursor = conn.cursor()
cursor.execute("INSERT INTO perfbenchmark_table (id, name, age) VALUES (1, 'John Doe', 30)")
conn.commit()
cursor.close()
conn.close()
print("INSERT benchmark with pyodbc completed.")
except Exception as e:
print(f"Error during INSERT benchmark: {e}")
def bench_update_pyodbc():
print("Running UPDATE benchmark with pyodbc...")
try:
conn = pyodbc.connect(CONNECTION_STRING)
cursor = conn.cursor()
cursor.execute("UPDATE perfbenchmark_table SET age = 31 WHERE id = 1")
conn.commit()
cursor.close()
conn.close()
print("UPDATE benchmark with pyodbc completed.")
except Exception as e:
print(f"Error during UPDATE benchmark: {e}")
def bench_delete_pyodbc():
print("Running DELETE benchmark with pyodbc...")
try:
conn = pyodbc.connect(CONNECTION_STRING)
cursor = conn.cursor()
cursor.execute("DELETE FROM perfbenchmark_table WHERE id = 1")
conn.commit()
cursor.close()
conn.close()
print("DELETE benchmark with pyodbc completed.")
except Exception as e:
print(f"Error during DELETE benchmark: {e}")
def bench_complex_query_pyodbc():
print("Running COMPLEX QUERY benchmark with pyodbc...")
try:
conn = pyodbc.connect(CONNECTION_STRING)
cursor = conn.cursor()
cursor.execute(
"""SELECT name, COUNT(*)
FROM perfbenchmark_table
GROUP BY name
HAVING COUNT(*) > 1
"""
)
cursor.fetchall()
cursor.close()
conn.close()
print("COMPLEX QUERY benchmark with pyodbc completed.")
except Exception as e:
print(f"Error during COMPLEX QUERY benchmark: {e}")
def bench_100_inserts_pyodbc():
print("Running 100 INSERTS benchmark with pyodbc...")
try:
conn = pyodbc.connect(CONNECTION_STRING)
cursor = conn.cursor()
data = [(i, "John Doe", 30) for i in range(100)]
cursor.executemany("INSERT INTO perfbenchmark_table (id, name, age) VALUES (?, ?, ?)", data)
conn.commit()
cursor.close()
conn.close()
print("100 INSERTS benchmark with pyodbc completed.")
except Exception as e:
print(f"Error during 100 INSERTS benchmark: {e}")
def bench_fetchone_pyodbc():
print("Running FETCHONE benchmark with pyodbc...")
try:
conn = pyodbc.connect(CONNECTION_STRING)
cursor = conn.cursor()
cursor.execute("SELECT * FROM perfbenchmark_table")
cursor.fetchone()
cursor.close()
conn.close()
print("FETCHONE benchmark with pyodbc completed.")
except Exception as e:
print(f"Error during FETCHONE benchmark: {e}")
def bench_fetchmany_pyodbc():
print("Running FETCHMANY benchmark with pyodbc...")
try:
conn = pyodbc.connect(CONNECTION_STRING)
cursor = conn.cursor()
cursor.execute("SELECT * FROM perfbenchmark_table")
cursor.fetchmany(10)
cursor.close()
conn.close()
print("FETCHMANY benchmark with pyodbc completed.")
except Exception as e:
print(f"Error during FETCHMANY benchmark: {e}")
def bench_executemany_pyodbc():
print("Running EXECUTEMANY benchmark with pyodbc...")
try:
conn = pyodbc.connect(CONNECTION_STRING)
cursor = conn.cursor()
cursor.fast_executemany = True
data = [(i, "John Doe", 30) for i in range(100)]
cursor.executemany("INSERT INTO perfbenchmark_table (id, name, age) VALUES (?, ?, ?)", data)
conn.commit()
cursor.close()
conn.close()
print("EXECUTEMANY benchmark with pyodbc completed.")
except Exception as e:
print(f"Error during EXECUTEMANY benchmark: {e}")
def bench_stored_procedure_pyodbc():
print("Running STORED PROCEDURE benchmark with pyodbc...")
try:
conn = pyodbc.connect(CONNECTION_STRING)
cursor = conn.cursor()
cursor.execute("{CALL perfbenchmark_stored_procedure}")
cursor.fetchall()
cursor.close()
conn.close()
print("STORED PROCEDURE benchmark with pyodbc completed.")
except Exception as e:
print(f"Error during STORED PROCEDURE benchmark: {e}")
def bench_nested_query_pyodbc():
print("Running NESTED QUERY benchmark with pyodbc...")
try:
conn = pyodbc.connect(CONNECTION_STRING)
cursor = conn.cursor()
cursor.execute(
"""SELECT * FROM (
SELECT name, age FROM perfbenchmark_table
) AS subquery
WHERE age > 25
"""
)
cursor.fetchall()
cursor.close()
conn.close()
print("NESTED QUERY benchmark with pyodbc completed.")
except Exception as e:
print(f"Error during NESTED QUERY benchmark: {e}")
def bench_join_query_pyodbc():
print("Running JOIN QUERY benchmark with pyodbc...")
try:
conn = pyodbc.connect(CONNECTION_STRING)
cursor = conn.cursor()
cursor.execute(
"""SELECT a.name, b.age
FROM perfbenchmark_table a
JOIN perfbenchmark_table b ON a.id = b.id
"""
)
cursor.fetchall()
cursor.close()
conn.close()
print("JOIN QUERY benchmark with pyodbc completed.")
except Exception as e:
print(f"Error during JOIN QUERY benchmark: {e}")
def bench_transaction_pyodbc():
print("Running TRANSACTION benchmark with pyodbc...")
try:
conn = pyodbc.connect(CONNECTION_STRING)
cursor = conn.cursor()
try:
cursor.execute("BEGIN TRANSACTION")
cursor.execute(
"INSERT INTO perfbenchmark_table (id, name, age) VALUES (1, 'John Doe', 30)"
)
cursor.execute("UPDATE perfbenchmark_table SET age = 31 WHERE id = 1")
cursor.execute("DELETE FROM perfbenchmark_table WHERE id = 1")
cursor.execute("COMMIT")
except:
cursor.execute("ROLLBACK")
cursor.close()
conn.close()
print("TRANSACTION benchmark with pyodbc completed.")
except Exception as e:
print(f"Error during TRANSACTION benchmark: {e}")
def bench_large_data_set_pyodbc():
print("Running LARGE DATA SET benchmark with pyodbc...")
try:
conn = pyodbc.connect(CONNECTION_STRING)
cursor = conn.cursor()
cursor.execute("SELECT * FROM perfbenchmark_table")
while cursor.fetchone():
pass
cursor.close()
conn.close()
print("LARGE DATA SET benchmark with pyodbc completed.")
except Exception as e:
print(f"Error during LARGE DATA SET benchmark: {e}")
def bench_update_with_join_pyodbc():
print("Running UPDATE WITH JOIN benchmark with pyodbc...")
try:
conn = pyodbc.connect(CONNECTION_STRING)
cursor = conn.cursor()
cursor.execute(
"""UPDATE perfbenchmark_child_table
SET description = 'Updated Child 1'
FROM perfbenchmark_child_table c
JOIN perfbenchmark_parent_table p ON c.parent_id = p.id
WHERE p.name = 'Parent 1'
"""
)
conn.commit()
cursor.close()
conn.close()
print("UPDATE WITH JOIN benchmark with pyodbc completed.")
except Exception as e:
print(f"Error during UPDATE WITH JOIN benchmark: {e}")
def bench_delete_with_join_pyodbc():
print("Running DELETE WITH JOIN benchmark with pyodbc...")
try:
conn = pyodbc.connect(CONNECTION_STRING)
cursor = conn.cursor()
cursor.execute(
"""DELETE c
FROM perfbenchmark_child_table c
JOIN perfbenchmark_parent_table p ON c.parent_id = p.id
WHERE p.name = 'Parent 1'
"""
)
conn.commit()
cursor.close()
conn.close()
print("DELETE WITH JOIN benchmark with pyodbc completed.")
except Exception as e:
print(f"Error during DELETE WITH JOIN benchmark: {e}")
def bench_multiple_connections_pyodbc():
print("Running MULTIPLE CONNECTIONS benchmark with pyodbc...")
try:
connections = []
for _ in range(10):
conn = pyodbc.connect(CONNECTION_STRING)
connections.append(conn)
for conn in connections:
cursor = conn.cursor()
cursor.execute("SELECT * FROM perfbenchmark_table")
cursor.fetchall()
cursor.close()
for conn in connections:
conn.close()
print("MULTIPLE CONNECTIONS benchmark with pyodbc completed.")
except Exception as e:
print(f"Error during MULTIPLE CONNECTIONS benchmark: {e}")
def bench_1000_connections_pyodbc():
print("Running 1000 CONNECTIONS benchmark with pyodbc...")
try:
threads = []
for _ in range(1000):
thread = threading.Thread(target=lambda: pyodbc.connect(CONNECTION_STRING).close())
threads.append(thread)
thread.start()
for thread in threads:
thread.join()
print("1000 CONNECTIONS benchmark with pyodbc completed.")
except Exception as e:
print(f"Error during 1000 CONNECTIONS benchmark: {e}")
# Define benchmark functions for mssql_python
def bench_select_mssql_python():
print("Running SELECT benchmark with mssql_python...")
try:
conn = mssql_python.connect(CONNECTION_STRING)
cursor = conn.cursor()
# Clear plan and data cache
cursor.execute("CHECKPOINT;")
cursor.execute("DBCC DROPCLEANBUFFERS;")
cursor.execute("DBCC FREEPROCCACHE;")
cursor.execute("SELECT * FROM perfbenchmark_table")
cursor.fetchall()
cursor.close()
conn.close()
print("SELECT benchmark with mssql_python completed.")
except Exception as e:
print(f"Error during SELECT benchmark with mssql_python: {e}")
def bench_insert_mssql_python():
print("Running INSERT benchmark with mssql_python...")
try:
conn = mssql_python.connect(CONNECTION_STRING)
cursor = conn.cursor()
cursor.execute("INSERT INTO perfbenchmark_table (id, name, age) VALUES (1, 'John Doe', 30)")
conn.commit()
cursor.close()
conn.close()
print("INSERT benchmark with mssql_python completed.")
except Exception as e:
print(f"Error during INSERT benchmark with mssql_python: {e}")
def bench_update_mssql_python():
print("Running UPDATE benchmark with mssql_python...")
try:
conn = mssql_python.connect(CONNECTION_STRING)
cursor = conn.cursor()
cursor.execute("UPDATE perfbenchmark_table SET age = 31 WHERE id = 1")
conn.commit()
cursor.close()
conn.close()
print("UPDATE benchmark with mssql_python completed.")
except Exception as e:
print(f"Error during UPDATE benchmark with mssql_python: {e}")
def bench_delete_mssql_python():
print("Running DELETE benchmark with mssql_python...")
try:
conn = mssql_python.connect(CONNECTION_STRING)
cursor = conn.cursor()
cursor.execute("DELETE FROM perfbenchmark_table WHERE id = 1")
conn.commit()
cursor.close()
conn.close()
print("DELETE benchmark with mssql_python completed.")
except Exception as e:
print(f"Error during DELETE benchmark with mssql_python: {e}")
def bench_complex_query_mssql_python():
print("Running COMPLEX QUERY benchmark with mssql_python...")
try:
conn = mssql_python.connect(CONNECTION_STRING)
cursor = conn.cursor()
cursor.execute(
"""SELECT name, COUNT(*)
FROM perfbenchmark_table
GROUP BY name
HAVING COUNT(*) > 1
"""
)
cursor.fetchall()
cursor.close()
conn.close()
print("COMPLEX QUERY benchmark with mssql_python completed.")
except Exception as e:
print(f"Error during COMPLEX QUERY benchmark with mssql_python: {e}")
def bench_100_inserts_mssql_python():
print("Running 100 INSERTS benchmark with mssql_python...")
try:
conn = mssql_python.connect(CONNECTION_STRING)
cursor = conn.cursor()
data = [(i, "John Doe", 30) for i in range(100)]
cursor.executemany(
"INSERT INTO perfbenchmark_table (id, name, age) VALUES (?, 'John Doe', 30)", data
)
conn.commit()
cursor.close()
conn.close()
print("100 INSERTS benchmark with mssql_python completed.")
except Exception as e:
print(f"Error during 100 INSERTS benchmark with mssql_python: {e}")
def bench_fetchone_mssql_python():
print("Running FETCHONE benchmark with mssql_python...")
try:
conn = mssql_python.connect(CONNECTION_STRING)
cursor = conn.cursor()
cursor.execute("SELECT * FROM perfbenchmark_table")
cursor.fetchone()
cursor.close()
conn.close()
print("FETCHONE benchmark with mssql_python completed.")
except Exception as e:
print(f"Error during FETCHONE benchmark with mssql_python: {e}")
def bench_fetchmany_mssql_python():
print("Running FETCHMANY benchmark with mssql_python...")
try:
conn = mssql_python.connect(CONNECTION_STRING)
cursor = conn.cursor()
cursor.execute("SELECT * FROM perfbenchmark_table")
cursor.fetchmany(10)
cursor.close()
conn.close()
print("FETCHMANY benchmark with mssql_python completed.")
except Exception as e:
print(f"Error during FETCHMANY benchmark with mssql_python: {e}")
def bench_executemany_mssql_python():
print("Running EXECUTEMANY benchmark with mssql_python...")
try:
conn = mssql_python.connect(CONNECTION_STRING)
cursor = conn.cursor()
data = [(i, "John Doe", 30) for i in range(100)]
cursor.executemany("INSERT INTO perfbenchmark_table (id, name, age) VALUES (?, ?, ?)", data)
conn.commit()
cursor.close()
conn.close()
print("EXECUTEMANY benchmark with mssql_python completed.")
except Exception as e:
print(f"Error during EXECUTEMANY benchmark with mssql_python: {e}")
def bench_stored_procedure_mssql_python():
print("Running STORED PROCEDURE benchmark with mssql_python...")
try:
conn = mssql_python.connect(CONNECTION_STRING)
cursor = conn.cursor()
cursor.execute("{CALL perfbenchmark_stored_procedure}")
cursor.fetchall()
cursor.close()
conn.close()
print("STORED PROCEDURE benchmark with mssql_python completed.")
except Exception as e:
print(f"Error during STORED PROCEDURE benchmark with mssql_python: {e}")
def bench_nested_query_mssql_python():
print("Running NESTED QUERY benchmark with mssql_python...")
try:
conn = mssql_python.connect(CONNECTION_STRING)
cursor = conn.cursor()
cursor.execute(
"""SELECT * FROM (
SELECT name, age FROM perfbenchmark_table
) AS subquery
WHERE age > 25
"""
)
cursor.fetchall()
cursor.close()
conn.close()
print("NESTED QUERY benchmark with mssql_python completed.")
except Exception as e:
print(f"Error during NESTED QUERY benchmark with mssql_python: {e}")
def bench_join_query_mssql_python():
print("Running JOIN QUERY benchmark with mssql_python...")
try:
conn = mssql_python.connect(CONNECTION_STRING)
cursor = conn.cursor()
cursor.execute(
"""SELECT a.name, b.age
FROM perfbenchmark_table a
JOIN perfbenchmark_table b ON a.id = b.id
"""
)
cursor.fetchall()
cursor.close()
conn.close()
print("JOIN QUERY benchmark with mssql_python completed.")
except Exception as e:
print(f"Error during JOIN QUERY benchmark with mssql_python: {e}")
def bench_transaction_mssql_python():
print("Running TRANSACTION benchmark with mssql_python...")
try:
conn = mssql_python.connect(CONNECTION_STRING)
cursor = conn.cursor()
try:
cursor.execute("BEGIN TRANSACTION")
cursor.execute(
"INSERT INTO perfbenchmark_table (id, name, age) VALUES (1, 'John Doe', 30)"
)
cursor.execute("UPDATE perfbenchmark_table SET age = 31 WHERE id = 1")
cursor.execute("DELETE FROM perfbenchmark_table WHERE id = 1")
cursor.execute("COMMIT")
except:
cursor.execute("ROLLBACK")
cursor.close()
conn.close()
print("TRANSACTION benchmark with mssql_python completed.")
except Exception as e:
print(f"Error during TRANSACTION benchmark with mssql_python: {e}")
def bench_large_data_set_mssql_python():
print("Running LARGE DATA SET benchmark with mssql_python...")
try:
conn = mssql_python.connect(CONNECTION_STRING)
cursor = conn.cursor()
cursor.execute("SELECT * FROM perfbenchmark_table")
while cursor.fetchone():
pass
cursor.close()
conn.close()
print("LARGE DATA SET benchmark with mssql_python completed.")
except Exception as e:
print(f"Error during LARGE DATA SET benchmark with mssql_python: {e}")
def bench_update_with_join_mssql_python():
print("Running UPDATE WITH JOIN benchmark with mssql_python...")
try:
conn = mssql_python.connect(CONNECTION_STRING)
cursor = conn.cursor()
cursor.execute(
"""UPDATE perfbenchmark_child_table
SET description = 'Updated Child 1'
FROM perfbenchmark_child_table c
JOIN perfbenchmark_parent_table p ON c.parent_id = p.id
WHERE p.name = 'Parent 1'
"""
)
conn.commit()
cursor.close()
conn.close()
print("UPDATE WITH JOIN benchmark with mssql_python completed.")
except Exception as e:
print(f"Error during UPDATE WITH JOIN benchmark with mssql_python: {e}")
def bench_delete_with_join_mssql_python():
print("Running DELETE WITH JOIN benchmark with mssql_python...")
try:
conn = mssql_python.connect(CONNECTION_STRING)
cursor = conn.cursor()
cursor.execute(
"""DELETE c
FROM perfbenchmark_child_table c
JOIN perfbenchmark_parent_table p ON c.parent_id = p.id
WHERE p.name = 'Parent 1'
"""
)
conn.commit()
cursor.close()
conn.close()
print("DELETE WITH JOIN benchmark with mssql_python completed.")
except Exception as e:
print(f"Error during DELETE WITH JOIN benchmark with mssql_python: {e}")
def bench_multiple_connections_mssql_python():
print("Running MULTIPLE CONNECTIONS benchmark with mssql_python...")
try:
connections = []
for _ in range(10):
conn = mssql_python.connect(CONNECTION_STRING)
connections.append(conn)
for conn in connections:
cursor = conn.cursor()
cursor.execute("SELECT * FROM perfbenchmark_table")
cursor.fetchall()
cursor.close()
for conn in connections:
conn.close()
print("MULTIPLE CONNECTIONS benchmark with mssql_python completed.")
except Exception as e:
print(f"Error during MULTIPLE CONNECTIONS benchmark with mssql_python: {e}")
def bench_1000_connections_mssql_python():
print("Running 1000 CONNECTIONS benchmark with mssql_python...")
try:
threads = []
for _ in range(1000):
thread = threading.Thread(
target=lambda: mssql_python.connect(CONNECTION_STRING).close()
)
threads.append(thread)
thread.start()
for thread in threads:
thread.join()
print("1000 CONNECTIONS benchmark with mssql_python completed.")
except Exception as e:
print(f"Error during 1000 CONNECTIONS benchmark with mssql_python: {e}")
# Define benchmarks
__benchmarks__ = [
(bench_select_pyodbc, bench_select_mssql_python, "SELECT operation"),
(bench_insert_pyodbc, bench_insert_mssql_python, "INSERT operation"),
(bench_update_pyodbc, bench_update_mssql_python, "UPDATE operation"),
(bench_delete_pyodbc, bench_delete_mssql_python, "DELETE operation"),
(bench_complex_query_pyodbc, bench_complex_query_mssql_python, "Complex query operation"),
(
bench_multiple_connections_pyodbc,
bench_multiple_connections_mssql_python,
"Multiple connections operation",
),
(bench_fetchone_pyodbc, bench_fetchone_mssql_python, "Fetch one operation"),
(bench_fetchmany_pyodbc, bench_fetchmany_mssql_python, "Fetch many operation"),
(
bench_stored_procedure_pyodbc,
bench_stored_procedure_mssql_python,
"Stored procedure operation",
),
(
bench_1000_connections_pyodbc,
bench_1000_connections_mssql_python,
"1000 connections operation",
),
(bench_nested_query_pyodbc, bench_nested_query_mssql_python, "Nested query operation"),
(bench_large_data_set_pyodbc, bench_large_data_set_mssql_python, "Large data set operation"),
(bench_join_query_pyodbc, bench_join_query_mssql_python, "Join query operation"),
(bench_executemany_pyodbc, bench_executemany_mssql_python, "Execute many operation"),
(bench_100_inserts_pyodbc, bench_100_inserts_mssql_python, "100 inserts operation"),
(bench_transaction_pyodbc, bench_transaction_mssql_python, "Transaction operation"),
(
bench_update_with_join_pyodbc,
bench_update_with_join_mssql_python,
"Update with join operation",
),
(
bench_delete_with_join_pyodbc,
bench_delete_with_join_mssql_python,
"Delete with join operation",
),
]