-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_join.py
More file actions
120 lines (94 loc) · 4.2 KB
/
bench_join.py
File metadata and controls
120 lines (94 loc) · 4.2 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
#!/usr/bin/env python3
# Copyright (c) 2024-2026 Anton Kundenko <singaraiona@gmail.com>
# All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""Join benchmark runner for Teide on 10M join dataset."""
import ctypes
import time
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "bindings", "python"))
from teide import TeideLib
N_ITER = 5 # fewer iterations since joins are expensive
JOIN_DIR = os.path.join(os.path.dirname(__file__),
"..", "rayforce-bench", "datasets", "h2oai_join_1e7")
X_CSV = os.path.join(JOIN_DIR, "J1_1e7_NA_0_0.csv")
Y_CSV = os.path.join(JOIN_DIR, "J1_1e7_1e7_0_0.csv")
def run_join(lib, left_table, right_table, label, key_names, join_type):
"""Run a join benchmark.
join_type: 0=INNER, 1=LEFT
"""
g = lib.graph_new(left_table)
try:
left_node = lib.const_table(g, left_table)
right_node = lib.const_table(g, right_table)
left_keys = [lib.scan(g, k) for k in key_names]
# Right keys: use const_vec for each right key column
right_keys = []
for k in key_names:
name_id = lib.sym_intern(k)
col_vec = lib._lib.td_table_get_col(right_table, name_id)
right_keys.append(lib.const_vec(g, col_vec))
root = lib.join(g, left_node, left_keys, right_node, right_keys, join_type)
root = lib.optimize(g, root)
times = []
nrows = ncols = 0
for _ in range(N_ITER):
t0 = time.perf_counter()
result = lib.execute(g, root)
times.append(time.perf_counter() - t0)
if not result or result < 32:
print(f" {label:12s} FAILED")
return
nrows = lib.table_nrows(result)
ncols = lib.table_ncols(result)
lib.release(result)
elapsed = sorted(times)[len(times) // 2]
print(f" {label:12s} {elapsed*1000:8.1f} ms {nrows:>10,} rows x {ncols} cols")
finally:
lib.graph_free(g)
def main():
for path in [X_CSV, Y_CSV]:
if not os.path.exists(os.path.abspath(path)):
print(f"CSV not found: {os.path.abspath(path)}")
sys.exit(1)
lib = TeideLib()
lib.arena_init()
lib.sym_init()
print(f"Loading x: {os.path.abspath(X_CSV)} ...")
t0 = time.perf_counter()
x = lib.read_csv(os.path.abspath(X_CSV))
print(f" {lib.table_nrows(x):,} rows in {(time.perf_counter()-t0)*1000:.0f} ms")
print(f"Loading y: {os.path.abspath(Y_CSV)} ...")
t0 = time.perf_counter()
y = lib.read_csv(os.path.abspath(Y_CSV))
print(f" {lib.table_nrows(y):,} rows in {(time.perf_counter()-t0)*1000:.0f} ms\n")
print("Join benchmarks (execution time only, excludes build/optimize):")
print(f" {'Query':12s} {'Time':>8s} Result")
print(f" {'-'*12} {'-'*8} {'-'*20}")
# j1: INNER JOIN on (id1,id2,id3) — nearly 1:1, ~10M result rows
run_join(lib, x, y, "j1-inner", ["id1", "id2", "id3"], 0)
# j2: LEFT JOIN on (id1,id2,id3) — 10M result rows
run_join(lib, x, y, "j2-left", ["id1", "id2", "id3"], 1)
print("\nDone.")
lib.release(y)
lib.release(x)
if __name__ == "__main__":
main()