-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenhalo_test_suite.py
More file actions
1305 lines (1057 loc) · 52.5 KB
/
Copy pathopenhalo_test_suite.py
File metadata and controls
1305 lines (1057 loc) · 52.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
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
OpenHalo Performance & Compatibility Testing Pipeline
Tests queries using OpenHalo and a standard MySQL database for comparison.
Integrated with Markdown Report Test Suite.
"""
import time
import json
import mysql.connector
from dataclasses import dataclass, asdict
from typing import List, Dict, Tuple
from statistics import mean, median
import sys
import uuid
from copy import deepcopy
import random
import concurrent.futures
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('Agg')
import numpy as np # Useful for data manipulation
@dataclass
class QueryResult:
target: str # 'OpenHalo' or 'MySQL'
query_id: str
query_type: str
times: List[float]
mean_time: float
median_time: float
p95_time : float
status: str
rows: int
error: str = None
# --- Dual Database Connector ---
class DualDatabaseConnector:
"""Manages connections to both OpenHalo and standard MySQL."""
def __init__(self, openhalo_config: Dict, mysql_config: Dict):
self.openhalo_config = openhalo_config
self.mysql_config = mysql_config
self.openhalo_conn = None
self.mysql_conn = None
def connect(self):
"""Establish connections."""
print("Attempting to connect to OpenHalo...")
try:
self.openhalo_conn = mysql.connector.connect(**self.openhalo_config)
self.openhalo_conn.autocommit = False # Important for transaction tests
print("✓ Connected to OpenHalo (Port: {})".format(self.openhalo_config['port']))
except Exception as e:
print(f"✗ OpenHalo connection failed: {e}")
sys.exit(1)
print("Attempting to connect to MySQL...")
try:
self.mysql_conn = mysql.connector.connect(**self.mysql_config)
self.mysql_conn.autocommit = False
print("✓ Connected to MySQL (Port: {})".format(self.mysql_config['port']))
except Exception as e:
print(f"✗ MySQL connection failed: {e}")
print("Warning: Continuing tests with OpenHalo only.")
def close(self):
# Protect OpenHalo connection closure
if self.openhalo_conn:
try:
self.openhalo_conn.close()
print("\nClosed OpenHalo connection.")
except:
pass # Already closed or network error, ignore
# Protect MySQL connection closure
if self.mysql_conn:
try:
self.mysql_conn.close()
print("Closed MySQL connection.")
except:
pass
# --- Schema Inspector ---
import random
class DynamicQueryBuilder:
"""
Generates random but valid SQL queries based on the table structure.
"""
# Schema definition to know what to generate
SCHEMA = {
'name_basics': {
'columns': ['nconst', 'primaryname', 'birthyear', 'deathyear', 'primaryprofession', 'knownfortitles'],
'numeric': ['birthyear', 'deathyear'],
'string': ['nconst', 'primaryname', 'primaryprofession', 'knownfortitles']
},
'films': {
'columns': ['film_id', 'title', 'release_year', 'rating', 'genre'],
'numeric': ['release_year', 'rating'],
'string': ['film_id', 'title', 'genre']
}
# Add other tables if necessary
}
def __init__(self, table_name):
self.table = table_name
self.meta = self.SCHEMA.get(table_name)
if not self.meta:
raise ValueError(f"Table {table_name} not defined in SCHEMA")
def _get_random_value(self, column):
"""Generates a fictive value for WHERE clauses (very basic)"""
if column in self.meta['numeric']:
return str(random.randint(1950, 2020))
else:
# For strings, return a generic value or a pattern
return "'%actor%'" if 'profession' in column else "'TestValue'"
def build_select(self, mode='random', limit=10):
"""
Generates a dynamic SELECT.
modes: 'star', 'single', 'multi', 'random'
"""
cols = self.meta['columns']
# Column selection
if mode == 'random':
mode = random.choice(['star', 'single', 'multi'])
if mode == 'star':
selected_cols = "*"
elif mode == 'single':
selected_cols = random.choice(cols)
elif mode == 'multi':
# Take between 2 and the max number of columns
nb_cols = random.randint(2, len(cols))
selected_cols = ", ".join(random.sample(cols, nb_cols))
query = f"SELECT {selected_cols} FROM {self.table}"
# Optional addition of a WHERE clause (1 in 3 times)
if random.random() > 0.7:
query += self._build_random_where_clause()
# Optional addition of an ORDER BY clause (1 in 3 times)
if random.random() > 0.7:
col_sort = random.choice(cols)
direction = random.choice(['ASC', 'DESC'])
query += f" ORDER BY {col_sort} {direction}"
query += f" LIMIT {limit};"
return f"Dynamic SELECT ({mode})", query
def _build_random_where_clause(self):
"""Construit une clause WHERE simple"""
col = random.choice(self.meta['columns'])
if col in self.meta['numeric']:
operator = random.choice(['>', '<', '=', '>=', '<=', '!='])
val = self._get_random_value(col)
return f" WHERE {col} {operator} {val}"
else:
operator = random.choice(['=', '!=', 'LIKE'])
val = self._get_random_value(col)
return f" WHERE {col} {operator} {val}"
def build_aggregation(self):
"""Génère une agrégation (COUNT, MAX, AVG)"""
agg_func = random.choice(['COUNT', 'MIN', 'MAX'])
# Prefer to do AVG/SUM on numbers
if agg_func in ['AVG', 'SUM']:
col = random.choice(self.meta['numeric'])
else:
col = random.choice(self.meta['columns']) # COUNT/MIN/MAX work on all
# Sometimes we group, sometimes not
group_by = ""
group_col = random.choice(self.meta['string']) # We often group by string (e.g., profession)
if random.choice([True, False]):
base = f"SELECT {group_col}, {agg_func}({col}) FROM {self.table} GROUP BY {group_col}"
# Often need an order by with group by for consistency
base += f" ORDER BY {agg_func}({col}) DESC LIMIT 10;"
return f"Dynamic AGG ({agg_func} by {group_col})", base
else:
return f"Dynamic AGG Simple ({agg_func})", f"SELECT {agg_func}({col}) FROM {self.table};"
def build_complex_where(self, limit=10):
"""Generates WHERE clauses with IN, BETWEEN, and OR"""
cols = self.meta['columns']
# Choose 2 random columns to make a complex condition
col1 = random.choice(cols)
mode = random.choice(['IN', 'BETWEEN', 'OR_MIX'])
query = f"SELECT * FROM {self.table} WHERE "
if mode == 'IN':
# Generates (val1, val2, val3)
vals = [self._get_random_value(col1) for _ in range(3)]
query += f"{col1} IN ({', '.join(vals)})"
elif mode == 'BETWEEN' and col1 in self.meta['numeric']:
val_start = random.randint(1900, 1980)
val_end = val_start + random.randint(5, 20)
query += f"{col1} BETWEEN {val_start} AND {val_end}"
else: # OR MIX
col2 = random.choice(cols)
val1 = self._get_random_value(col1)
val2 = self._get_random_value(col2)
query += f"({col1} = {val1} OR {col2} = {val2})"
query += f" LIMIT {limit};"
return f"Dyn Complex Filter ({mode})", query
def build_scalar_function(self, limit=10):
"""Teste les fonctions de manipulation de chaînes/maths"""
str_col = random.choice(self.meta['string'])
num_col = random.choice(self.meta['numeric'])
func_type = random.choice(['STRING', 'MATH'])
if func_type == 'STRING':
# Test LENGTH, LOWER, CONCAT, LEFT
func = random.choice([
f"LENGTH({str_col})",
f"LOWER({str_col})",
f"CONCAT({str_col}, '_test')",
f"LEFT({str_col}, 3)"
])
query = f"SELECT {str_col}, {func} as res FROM {self.table} WHERE {str_col} IS NOT NULL"
else: # MATH
# Test arithmetic operations
calc = random.choice([
f"({num_col} * 2)",
f"({num_col} % 10)", # Modulo
f"ABS({num_col} - 2000)"
])
query = f"SELECT {num_col}, {calc} as math_res FROM {self.table} WHERE {num_col} IS NOT NULL"
query += f" LIMIT {limit};"
return f"Dyn Scalar Func ({func_type})", query
def build_subquery(self, limit=10):
"""Generates a subquery (WHERE col > (SELECT AVG...))"""
# Use a numeric column for comparison
num_col = random.choice(self.meta['numeric'])
# Subquery that calculates an average or a min
sub = f"(SELECT AVG({num_col}) FROM {self.table} WHERE {num_col} IS NOT NULL)"
# Main query
query = f"SELECT * FROM {self.table} WHERE {num_col} > {sub} LIMIT {limit};"
return f"Dyn Subquery (Compare to AVG)", query
def build_dml_lifecycle(self):
"""
Generates a suite INSERT -> UPDATE -> SELECT -> DELETE.
Returns a list of tuples (desc, sql).
"""
# Unique ID to avoid breaking production
unique_id = f"nm99{random.randint(10000, 99999)}"
name = f"AutoTest_{random.randint(1,999)}"
steps = []
# 1. INSERT
sql_ins = f"INSERT INTO {self.table} (nconst, primaryname, birthyear, primaryprofession) VALUES ('{unique_id}', '{name}', 2025, 'tester');"
steps.append((f"Dyn DML 1: INSERT {unique_id}", sql_ins))
# 2. UPDATE
sql_upd = f"UPDATE {self.table} SET birthyear = 2026 WHERE nconst = '{unique_id}';"
steps.append((f"Dyn DML 2: UPDATE {unique_id}", sql_upd))
# 3. VERIFY
sql_sel = f"SELECT * FROM {self.table} WHERE nconst = '{unique_id}';"
steps.append((f"Dyn DML 3: SELECT {unique_id}", sql_sel))
# 4. DELETE
sql_del = f"DELETE FROM {self.table} WHERE nconst = '{unique_id}';"
steps.append((f"Dyn DML 4: DELETE {unique_id}", sql_del))
return steps
# --- Dual Query Tester ---
class DualQueryTester:
def __init__(self, db_connector: DualDatabaseConnector, iterations: int = 5, warmup: int = 1):
self.db = db_connector
self.iterations = iterations
self.warmup = warmup
self.results: List[QueryResult] = []
def execute_query(self, query: str, conn) -> Tuple[List, float]:
"""Execute a query on a given connection and return results + execution time"""
try:
# Check if the connection is active, otherwise reconnect (3 attempts)
if conn:
conn.ping(reconnect=True, attempts=3, delay=1)
except Exception:
# If the ping fails, let the cursor try its luck (and fail properly)
pass
cursor = conn.cursor()
try:
start = time.perf_counter()
# Handle multiple statements if necessary, though simpler is better for timing
cursor.execute(query)
query_clean = query.strip().upper().lstrip('(').strip()
if any(query_clean.startswith(x) for x in ['SELECT', 'WITH', 'SHOW', 'DESCRIBE', 'CALL', 'CHECK']):
try:
results = cursor.fetchall()
except mysql.connector.Error as e:
if "No result set" in str(e):
results = []
else:
raise e
else:
results = []
conn.commit()
end = time.perf_counter()
return results, (end - start) * 1000 # ms
except mysql.connector.Error as e:
# Do not always rollback here to allow testing transactional errors
# but rollback on fatal errors to clean the connection
conn.rollback()
raise e
finally:
cursor.close()
def classify_performance(self, mean_time: float) -> str:
if mean_time <= 50: return "OK"
elif mean_time <= 200: return "Warning"
else: return "Problem"
def test_single_target(self, target: str, conn, query_id: str, query_type: str, query: str, skip: bool):
times = []
rows_count = 0
if skip:
return QueryResult(
target=target,
query_id=query_id,
query_type=query_type,
times=[],
mean_time=0,
median_time=0,
p95_time=0,
status="Skipped",
rows=0,
error="Query type not tested"
)
try:
# Warmup (only for SELECTs to avoid side effects on INSERTs)
is_select = query.strip().upper().startswith(('SELECT', 'WITH', 'SHOW'))
if is_select and self.warmup > 0:
try:
warmup_q = f"{query.rstrip(';')} LIMIT 1;" if 'LIMIT' not in query.upper() and 'SHOW' not in query.upper() else query
self.execute_query(warmup_q, conn)
except Exception:
pass
# Iterations (1 for non-selects to avoid duplicates errors, self.iterations for SELECTs)
run_count = self.iterations if is_select else 1
for _ in range(run_count):
results, elapsed = self.execute_query(query, conn)
times.append(elapsed)
if rows_count == 0:
rows_count = len(results) if results else 0
mean_time = mean(times)
median_time = median(times)
p95_val = 0
status = self.classify_performance(mean_time)
print(f" [{target}] Mean: {mean_time:.2f}ms, Rows: {rows_count}, Status: {status}")
return QueryResult(
target=target,
query_id=query_id,
query_type=query_type,
times=times,
mean_time=mean_time,
median_time=median_time,
p95_time=p95_val,
status=status,
rows=rows_count,
error=None
)
except Exception as e:
error_msg = str(e)
status = "Error"
# Specific detection for the compatibility report
if "syntax error" in error_msg.lower():
status = "SyntaxError"
elif "doesn't exist" in error_msg.lower() or "unknown" in error_msg.lower():
status = "MissingFeature"
print(f" [{target}] ✗ {status}: {error_msg.splitlines()[0][:100]}...")
return QueryResult(
target=target,
query_id=query_id,
query_type=query_type,
times=[],
mean_time=0,
median_time=0,
p95_time=0,
status=status,
rows=0,
error=error_msg
)
def test_query(self, query_id: str, query_type: str, query: str, skip: bool = False):
print(f"\nTesting: {query_id} ({query_type})")
# Test OpenHalo
oh_res = self.test_single_target('OpenHalo', self.db.openhalo_conn, query_id, query_type, query, skip)
self.results.append(oh_res)
# Test MySQL
if self.db.mysql_conn:
mysql_res = self.test_single_target('MySQL', self.db.mysql_conn, query_id, query_type, query, False)
self.results.append(mysql_res)
def generate_report(self, output_file: str = "openhalo_full_compatibility_report.json"):
print("\n" + "="*60)
print("FULL COMPATIBILITY REPORT GENERATION")
print("="*60)
output_data = {
"meta": {"timestamp": time.time()},
"queries": [asdict(r) for r in self.results]
}
with open(output_file, 'w') as f:
json.dump(output_data, f, indent=2)
print(f"\n✓ Report saved to {output_file}")
def generate_summary(self):
print("\n" + "=" * 60)
print("SYNTHESIS REPORT – USEFUL METRICS")
print("=" * 60)
oh = [r for r in self.results if r.target == "OpenHalo"]
mysql = [r for r in self.results if r.target == "MySQL"]
oh_map = {r.query_id: r for r in oh}
mysql_map = {r.query_id: r for r in mysql}
# ---- Global success stats ----
print("\n📌 OpenHalo execution summary")
print(f" Total queries tested : {len(oh)}")
print(f" ✅ OK : {sum(r.status == 'OK' for r in oh)}")
print(f" ⚠ Problems : {sum(r.status == 'Problem' for r in oh)}")
print(f" ❌ Errors : {sum(r.status in ('Error','SyntaxError','MissingFeature') for r in oh)}")
# ---- Slowest OpenHalo queries ----
# This is the most interesting part: the bottlenecks
slow_oh = sorted(
[r for r in oh if r.mean_time > 0],
key=lambda r: r.mean_time,
reverse=True
)[:10]
print("\n🐢 TOP 10 QUERIES THE SLOWEST ON OPENHALO (vs MySQL)")
print(f" {'ID':<12} | {'OpenHalo (ms)':>15} | {'MySQL (ms)':>15} | {'Difference':>12}")
print("-" * 65)
for r in slow_oh:
oh_time = r.mean_time
# Looking for the corresponding time in MySQL
my_r = mysql_map.get(r.query_id)
my_time = my_r.mean_time if my_r else 0
# Calculating the difference
diff_str = ""
if my_time > 0:
ratio = oh_time / my_time
if ratio > 1.5:
diff_str = f"x{ratio:.1f} slower 🔴"
elif ratio < 0.7:
diff_str = f"x{1/ratio:.1f} faster 🟢"
else:
diff_str = "Similar ⚪"
else:
diff_str = "N/A"
print(f" {r.query_id:<12} | {oh_time:>15.2f} | {my_time:>15.2f} | {diff_str}")
# ---- OpenHalo Wins (Faster than MySQL) ----
fast_oh = []
for qid, oh_r in oh_map.items():
my_r = mysql_map.get(qid)
# We only compare if both succeeded
if my_r and oh_r.mean_time > 0 and my_r.mean_time > 0:
# If OH is at least 10% faster (ratio < 0.9)
if oh_r.mean_time < (my_r.mean_time * 0.9):
fast_oh.append((qid, oh_r.mean_time, my_r.mean_time))
# Sort by performance gain (largest difference first)
fast_oh.sort(key=lambda x: x[2] - x[1], reverse=True)
print("\n🚀 TOP QUERIES WHERE OPENHALO BEATS MYSQL (Hall of Fame)")
print(f" {'ID':<12} | {'OpenHalo (ms)':>15} | {'MySQL (ms)':>15} | {'Gain':>12}")
print("-" * 65)
if fast_oh:
for qid, oh_t, my_t in fast_oh[:10]: # Top 10
gain = my_t - oh_t
ratio = my_t / oh_t
print(f" {qid:<12} | {oh_t:>15.2f} | {my_t:>15.2f} | -{gain:.1f}ms (x{ratio:.1f})")
else:
print(" No significant wins detected on this dataset.")
# ---- MySQL faster than OpenHalo ----
print("\n⚡ Queries faster on MySQL than OpenHalo")
for qid, oh_r in oh_map.items():
my_r = mysql_map.get(qid)
if not my_r or oh_r.mean_time == 0 or my_r.mean_time == 0:
continue
delta = oh_r.mean_time - my_r.mean_time
if delta > 5:
print(
f" {qid:<15} OH={oh_r.mean_time:>7.2f} ms | "
f"MySQL={my_r.mean_time:>7.2f} ms → Δ {delta:.2f} ms"
)
# ---- Missing / unsupported features ----
print("\n🚫 Unsupported / failing features on OpenHalo")
for r in oh:
if r.status in ("MissingFeature", "SyntaxError"):
print(f" {r.query_id:<15} {r.query_type} → {r.status}")
# ---- Averages ----
oh_times = [r.mean_time for r in oh if r.mean_time > 0]
my_times = [r.mean_time for r in mysql if r.mean_time > 0]
print("\n📊 Average execution time")
if oh_times:
print(f" OpenHalo : {mean(oh_times):.2f} ms")
if my_times:
print(f" MySQL : {mean(my_times):.2f} ms")
print("\n✅ End of synthesis report")
# ---- Category Breakdown ----
print("\n📂 PERFORMANCE PAR CATÉGORIE")
print(f" {'Catégorie':<25} | {'OpenHalo Avg':>12} | {'MySQL Avg':>12}")
print("-" * 55)
categories = {
'Simple SELECT': ['md_1', 'dyn_sel'],
'Aggregations': ['md_3', 'dyn_agg'],
'Joins': ['md_6'],
'Subqueries': ['md_11', 'dyn_sub'],
'DML (Write)': ['md_4', 'dyn_dml'],
'String/Math': ['md_8', 'dyn_func']
}
for cat_name, prefixes in categories.items():
# Filter results that start with one of the prefixes
oh_cat = [r.mean_time for r in oh if any(r.query_id.startswith(p) for p in prefixes) and r.mean_time > 0]
my_cat = [r.mean_time for r in mysql if any(r.query_id.startswith(p) for p in prefixes) and r.mean_time > 0]
oh_val = f"{mean(oh_cat):.2f} ms" if oh_cat else "N/A"
my_val = f"{mean(my_cat):.2f} ms" if my_cat else "N/A"
print(f" {cat_name:<25} | {oh_val:>12} | {my_val:>12}")
class StressTester:
def __init__(self, db_config, num_threads=10, duration_seconds=5):
self.db_config = db_config
self.num_threads = num_threads
self.duration = duration_seconds
def _worker_task(self):
"""Simulates an active user and measures latencies"""
try:
conn = mysql.connector.connect(**self.db_config)
cursor = conn.cursor()
except:
return [], 1 # Returns an empty list and 1 error
latencies = [] # Storing the time of each query here
errors = 0
start_time = time.time()
while time.time() - start_time < self.duration:
try:
req_start = time.perf_counter() # Start timer
# Simple read query
cursor.execute("SELECT * FROM name_basics WHERE primaryprofession = 'actor' LIMIT 1")
cursor.fetchall()
req_end = time.perf_counter() # End timer
# Add the duration in milliseconds (ms) to the list
latencies.append((req_end - req_start) * 1000)
except Exception:
errors += 1
conn.close()
return latencies, errors
def run_benchmark(self, target_name):
print(f"\n🔥 STRESS TEST: {target_name} ({self.num_threads} threads, {self.duration}s)")
all_latencies = []
total_errors = 0
with concurrent.futures.ThreadPoolExecutor(max_workers=self.num_threads) as executor:
futures = [executor.submit(self._worker_task) for _ in range(self.num_threads)]
for future in concurrent.futures.as_completed(futures):
lats, e = future.result()
all_latencies.extend(lats) # We merge the results from all threads
total_errors += e
total_queries = len(all_latencies)
# Statistical calculations
if self.duration > 0:
tps = total_queries / self.duration
else:
tps = 0
if all_latencies:
avg_lat = mean(all_latencies)
all_latencies.sort()
# P95: Latency worse than 95% of users
p95_lat = all_latencies[int(len(all_latencies) * 0.95)]
else:
avg_lat = 0
p95_lat = 0
print(f" ➜ TPS (Transac/Sec): {tps:.2f}")
print(f" ➜ Average Latency : {avg_lat:.2f} ms")
print(f" ➜ P95 Latency : {p95_lat:.2f} ms")
print(f" ➜ Errors : {total_errors}")
# We return a dictionary, not just a float
return {
"tps": tps,
"avg_latency": avg_lat,
"p95_latency": p95_lat
}
def test_bulk_insert(target_name, config, batch_size=5000):
print(f"\n📦 BULK INSERT TEST: {target_name} ({batch_size} rows)")
try:
conn = mysql.connector.connect(**config)
cursor = conn.cursor()
cursor.execute("DROP TABLE IF EXISTS bulk_test")
cursor.execute("CREATE TABLE bulk_test (id INT, val VARCHAR(50))")
data = [(i, f"val_{i}") for i in range(batch_size)]
start = time.perf_counter()
# executemany is optimized for bulk
cursor.executemany("INSERT INTO bulk_test (id, val) VALUES (%s, %s)", data)
conn.commit()
end = time.perf_counter()
duration_ms = (end - start) * 1000
print(f" ➜ Time: {duration_ms:.2f} ms")
print(f" ➜ Rate: {batch_size / (end - start):.0f} rows/sec")
cursor.execute("DROP TABLE bulk_test")
conn.close()
except Exception as e:
print(f" ➜ Failed: {e}")
def main():
# --- Configuration ---
openhalo_config = {'host': 'localhost', 'port': 3306, 'user': 'halo', 'password': 'halo', 'database': 'testdb'}
mysql_config = {'host': 'localhost', 'port': 3309, 'user': 'halo', 'password': 'halo', 'database': 'testdb'}
# --- Setup ---
print("="*60)
print("OpenHalo vs MySQL - Full Markdown Compatibility Suite")
print("="*60)
db = DualDatabaseConnector(openhalo_config, mysql_config)
db.connect()
# Reduced iterations for compatibility check
tester = DualQueryTester(db, iterations=3, warmup=1)
builder = DynamicQueryBuilder('name_basics')
table_nb = "name_basics"
# =========================================================================
# TESTS FROM MARKDOWN REPORT
# =========================================================================
# --- 1. Basic Queries ---
print("\n--- 1. Basic Queries ---")
tester.test_query("md_1.1", "Simple Field Query",
f"SELECT * FROM {table_nb} WHERE primaryprofession = 'actor';")
tester.test_query("md_1.2", "Multi-Criteria Pattern Match",
f"SELECT * FROM {table_nb} WHERE birthyear > 1970 AND primaryprofession LIKE '%actor%';")
# --- 2. Filtering and Sorting ---
print("\n--- 2. Filtering and Sorting ---")
tester.test_query("md_2.1", "ORDER BY Multiple Conditions",
f"SELECT primaryname, birthyear, primaryprofession FROM {table_nb} WHERE deathyear IS NULL AND birthyear IS NOT NULL ORDER BY birthyear ASC LIMIT 10;")
# --- 3. Aggregation and Statistics ---
print("\n--- 3. Aggregation and Statistics ---")
tester.test_query("md_3.1", "GROUP BY with COUNT",
f"SELECT primaryprofession, COUNT(*) AS total FROM {table_nb} GROUP BY primaryprofession ORDER BY total DESC LIMIT 10;")
tester.test_query("md_3.2", "AVG Aggregation Multi Column",
f"SELECT primaryprofession, AVG(birthyear) AS avg_birthyear, COUNT(*) AS total FROM {table_nb} WHERE birthyear IS NOT NULL GROUP BY primaryprofession ORDER BY total DESC LIMIT 10;")
tester.test_query("md_3.3", "MIN/MAX Functions",
f"SELECT MAX(birthyear) AS most_recent, MIN(birthyear) AS oldest FROM {table_nb} WHERE birthyear IS NOT NULL;")
tester.test_query("md_3.4", "Advanced Grouping (FLOOR)",
f"SELECT FLOOR(birthyear/10)*10 AS decade, COUNT(*) AS total FROM {table_nb} WHERE birthyear IS NOT NULL GROUP BY decade ORDER BY decade DESC;")
# --- 4. Generating Dynamic SELECTs ---
# We will generate 10 completely different select queries
print("\n--- Generating 10 Random SELECT/WHERE/ORDER scenarios ---")
for i in range(1, 11):
# The builder returns a description and the SQL query
desc, sql = builder.build_select(mode='random', limit=random.randint(5, 50))
query_id = f"dyn_sel_{i}"
tester.test_query(query_id, desc, sql)
# --- 5. Generating Dynamic Aggregations ---
print("\n--- Generating 5 Random Aggregation scenarios ---")
for i in range(1, 6):
desc, sql = builder.build_aggregation()
query_id = f"dyn_agg_{i}"
tester.test_query(query_id, desc, sql)
# Phase 3: Complex Filters (IN, BETWEEN)
print("\n--- 3. GGenerating Complex Filters ---")
for i in range(1, 6):
desc, sql = builder.build_complex_where()
tester.test_query(f"dyn_cplx_{i:02d}", desc, sql)
# Phase 4: Scalar Functions (String/Math)
print("\n--- 4. Scalar Functions ---")
for i in range(1, 6):
desc, sql = builder.build_scalar_function()
tester.test_query(f"dyn_func_{i:02d}", desc, sql)
# Phase 5: Subqueries
print("\n--- 5. Subqueries ---")
for i in range(1, 4):
desc, sql = builder.build_subquery()
tester.test_query(f"dyn_sub_{i:02d}", desc, sql)
# Phase 6: DML Lifecycle (Insert/Update/Delete)
print("\n--- 6. DML Lifecycle (Safe) ---")
# We generate a complete sequence of DML operations
dml_steps = builder.build_dml_lifecycle()
for desc, sql in dml_steps:
# For DML, we don't want to execute it 3 times (otherwise duplicate key error), so we temporarily force iterations=1
old_iter = tester.iterations
tester.iterations = 1
tester.test_query("dyn_dml", "DML Lifecycle", sql)
tester.iterations = old_iter
# --- 7. Data Manipulation (CRUD) ---
print("\n--- 7. Data Manipulation (CRUD) ---")
# Using specific ID from MD report: nm9999999
crud_id = 'nm9999999'
# Preventive cleanup
tester.test_query("md_4.0_cleanup", "Pre-CRUD Cleanup",
f"DELETE FROM {table_nb} WHERE nconst = '{crud_id}';")
tester.test_query("md_4.1", "INSERT Operation",
f"INSERT INTO {table_nb} (nconst, primaryname, birthyear, deathyear, primaryprofession, knownfortitles) VALUES ('{crud_id}', 'Test Actor', 1990, NULL, 'actor', 'tt1234567');")
tester.test_query("md_4.2", "SELECT Verification",
f"SELECT * FROM {table_nb} WHERE nconst = '{crud_id}';")
tester.test_query("md_4.3", "UPDATE Operation",
f"UPDATE {table_nb} SET birthyear = 1985 WHERE nconst = '{crud_id}';")
tester.test_query("md_4.3_verify", "Verify UPDATE",
f"SELECT birthyear FROM {table_nb} WHERE nconst = '{crud_id}';")
tester.test_query("md_4.4", "DELETE Operation",
f"DELETE FROM {table_nb} WHERE nconst = '{crud_id}';")
# --- 8. Index Management ---
print("\n--- 8. Index Management ---")
# --- PRELIMINARY CLEANUP (Clean Slate) ---
# To ensure the "CREATE INDEX" test works every time (and truly measures the time),
# we must first drop the indexes if they already exist.
def safe_drop_index(conn, table, index_name):
try:
if conn:
cursor = conn.cursor()
cursor.execute(f"DROP INDEX {index_name} ON {table}")
conn.commit()
cursor.close()
except:
pass # We ignore the error if the index did not exist yet
# We clean up on both databases
safe_drop_index(db.openhalo_conn, table_nb, 'idx_profession')
safe_drop_index(db.mysql_conn, table_nb, 'idx_profession')
safe_drop_index(db.openhalo_conn, table_nb, 'idx_birthyear')
safe_drop_index(db.mysql_conn, table_nb, 'idx_birthyear')
# --- TEST EXECUTION ---
# Test 5.1 : Now it will work because we cleaned up before
tester.test_query("md_5.1", "CREATE INDEX VARCHAR",
f"CREATE INDEX idx_profession ON {table_nb}(primaryprofession);")
# Test 5.2
tester.test_query("md_5.2", "CREATE INDEX INT",
f"CREATE INDEX idx_birthyear ON {table_nb}(birthyear);")
# Test 5.3 : Verification
tester.test_query("md_5.3", "SHOW INDEX", f"SHOW INDEX FROM {table_nb};")
# --- 6. Join Operations ---
print("\n--- 6. Join Operations ---")
# Prerequisite: films and film_actor tables must exist for these to pass
tester.test_query("md_6.1", "Multi-Table INNER JOIN",
f"""
SELECT nb.primaryname, f.title, f.release_year
FROM {table_nb} nb
JOIN film_actor fa ON nb.nconst = fa.nconst
JOIN films f ON fa.film_id = f.film_id
LIMIT 10;
""")
tester.test_query("md_6.2", "LEFT JOIN with Aggregation",
f"""
SELECT nb.primaryname, COUNT(fa.film_id) AS nb_films
FROM {table_nb} nb
LEFT JOIN film_actor fa ON nb.nconst = fa.nconst
WHERE nb.birthyear > 1980
GROUP BY nb.nconst, nb.primaryname
ORDER BY nb_films DESC LIMIT 10;
""")
tester.test_query("md_6.3", "JOIN Multiple Conditions",
f"""
SELECT nb.primaryname, f.title, f.rating, fa.role
FROM {table_nb} nb
JOIN film_actor fa ON nb.nconst = fa.nconst
JOIN films f ON fa.film_id = f.film_id
WHERE f.rating > 7.0 AND nb.primaryprofession LIKE '%actor%'
ORDER BY f.rating DESC LIMIT 10;
""")
tester.test_query("md_6.4", "SELF JOIN",
"SELECT f1.title AS film1, f2.title AS film2, f1.genre FROM films f1 JOIN films f2 ON f1.genre = f2.genre AND f1.film_id < f2.film_id LIMIT 10;")
tester.test_query("md_6.5", "JOIN with HAVING and DISTINCT",
f"""
SELECT nb.primaryname, COUNT(DISTINCT f.genre) AS nb_genres
FROM {table_nb} nb
JOIN film_actor fa ON nb.nconst = fa.nconst
JOIN films f ON fa.film_id = f.film_id
GROUP BY nb.nconst, nb.primaryname
HAVING COUNT(DISTINCT f.genre) > 1 LIMIT 10;
""")
tester.test_query("md_6.6", "Subquery with JOIN",
f"""
SELECT f.title, f.rating
FROM films f
WHERE f.film_id IN (
SELECT fa.film_id
FROM film_actor fa
JOIN {table_nb} nb ON fa.nconst = nb.nconst
WHERE nb.birthyear < 1950
) LIMIT 10;
""")
# --- 7. Views and Transactions ---
print("\n--- 7. Views and Transactions ---")
tester.test_query("md_7.1", "CREATE VIEW",
f"CREATE OR REPLACE VIEW actor_summary AS SELECT primaryname, birthyear, primaryprofession FROM {table_nb} WHERE primaryprofession = 'actor' ORDER BY birthyear DESC;")
tester.test_query("md_7.2", "Query VIEW", "SELECT * FROM actor_summary LIMIT 10;")
tester.test_query("md_7.3", "DROP VIEW", "DROP VIEW actor_summary;")
# Transactions (using explicit SQL though connectors handle this via autocommit settings)
# Note: Explicit START TRANSACTION inside execute might behave differently depending on connector,
# but we are testing if the database accepts the syntax.
trans_id = 'nm8888888'
# 7.4 Commit
tester.test_query("md_7.4_a", "Transaction START", "START TRANSACTION;")
tester.test_query("md_7.4_b", "Transaction INSERT", f"INSERT INTO {table_nb} (nconst, primaryname, birthyear) VALUES ('{trans_id}', 'Trans Test', 1995);")
tester.test_query("md_7.4_c", "Transaction COMMIT", "COMMIT;")
tester.test_query("md_7.4_d", "Verify Commit", f"SELECT * FROM {table_nb} WHERE nconst = '{trans_id}';")
# 7.5 Rollback
tester.test_query("md_7.5_a", "Rollback START", "START TRANSACTION;")
tester.test_query("md_7.5_b", "Rollback DELETE", f"DELETE FROM {table_nb} WHERE nconst = '{trans_id}';")
tester.test_query("md_7.5_c", "Rollback EXEC", "ROLLBACK;")
tester.test_query("md_7.5_d", "Verify Rollback (Row should exist)", f"SELECT * FROM {table_nb} WHERE nconst = '{trans_id}';")
# Cleanup transaction test
tester.test_query("md_7_cleanup", "Cleanup Trans", f"DELETE FROM {table_nb} WHERE nconst = '{trans_id}';")
# --- 8. String Functions ---
print("\n--- 8. String Functions ---")
tester.test_query("md_8.1", "CONCAT", f"SELECT CONCAT(primaryname, ' (', birthyear, ')') AS full_info FROM {table_nb} WHERE birthyear IS NOT NULL LIMIT 10;")
tester.test_query("md_8.2", "SUBSTRING", f"SELECT primaryname, SUBSTRING(primaryname, 1, 10) AS short_name FROM {table_nb} LIMIT 10;")
tester.test_query("md_8.3", "UPPER/LOWER", f"SELECT UPPER(primaryname), LOWER(primaryprofession) FROM {table_nb} LIMIT 10;")
tester.test_query("md_8.4", "LENGTH", f"SELECT primaryname, LENGTH(primaryname) AS len FROM {table_nb} ORDER BY len DESC LIMIT 10;")
tester.test_query("md_8.5", "REPLACE", f"SELECT primaryname, REPLACE(primaryname, ' ', '_') FROM {table_nb} LIMIT 10;")
tester.test_query("md_8.6", "TRIM", f"SELECT primaryname, TRIM(primaryname) FROM {table_nb} LIMIT 10;")
# --- 9. Advanced SQL ---
print("\n--- 9. Advanced SQL ---")
tester.test_query("md_9.1", "UNION (Expected Fail on OH)",
f"(SELECT primaryname FROM {table_nb} WHERE primaryprofession = 'actor' LIMIT 5) UNION (SELECT primaryname FROM {table_nb} WHERE primaryprofession = 'actress' LIMIT 5);")
tester.test_query("md_9.2", "CASE WHEN",
f"""
SELECT primaryname,
CASE
WHEN birthyear < 1950 THEN 'Vintage'
WHEN birthyear BETWEEN 1950 AND 1980 THEN 'Classic'
ELSE 'Modern'
END AS era
FROM {table_nb} LIMIT 10;
""")
# 9. Show Table Status (Manquant dans le script original)
try:
# We try changing DB (which often fails on OH according to the report)
# then display the status
tester.test_query("prob_9_use", "USE DB", f"USE {openhalo_config['database']};")
tester.test_query("prob_9_status", "SHOW TABLE STATUS", "SHOW TABLE STATUS;")
except:
pass
# --- 10. Database Constraints ---
print("\n--- 10. Database Constraints ---")
# Note: Using ALTER TABLE requires exclusive locks usually
tester.test_query("md_10.1", "Add UNIQUE Constraint", "ALTER TABLE films ADD CONSTRAINT unique_title UNIQUE (title);")
# Test violation
tester.test_query("md_10.1_fail", "Test UNIQUE Violation", "INSERT INTO films (film_id, title) VALUES ('tt999', 'Example Film 1');")
# Step 1: Remove actors in film_actor who do not exist in name_basics
tester.test_query("md_10.2_pre", "Cleanup Orphan Records",
f"DELETE FROM film_actor WHERE nconst NOT IN (SELECT nconst FROM {table_nb});")
# Step 2: Now that the data is clean, we can add the FK
tester.test_query("md_10.2", "Add FK Constraint",
f"ALTER TABLE film_actor ADD CONSTRAINT fk_actor FOREIGN KEY (nconst) REFERENCES {table_nb}(nconst);")
tester.test_query("md_10.3", "Add CHECK Constraint",
"ALTER TABLE films ADD CONSTRAINT check_year CHECK (release_year > 1800 AND release_year <= 2100);")
# Drop constraints
tester.test_query("md_10.4_a", "Drop UNIQUE", "ALTER TABLE films DROP CONSTRAINT unique_title;")
tester.test_query("md_10.4_b", "Drop CHECK", "ALTER TABLE films DROP CONSTRAINT check_year;")
tester.test_query("md_10.4_c", "Drop FK", "ALTER TABLE film_actor DROP CONSTRAINT fk_actor;")
# --- 11. Advanced Subqueries ---
print("\n--- 11. Advanced Subqueries ---")
tester.test_query("md_11.1", "Derived Table",
f"SELECT * FROM (SELECT primaryname, birthyear FROM {table_nb} WHERE birthyear > 1980) AS young_actors LIMIT 10;")
tester.test_query("md_11.2", "Correlated Subquery",
f"""