-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_multicolumn.py
More file actions
400 lines (325 loc) · 14 KB
/
bench_multicolumn.py
File metadata and controls
400 lines (325 loc) · 14 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
#!/usr/bin/env python3
"""
VelesDB vs ClickHouse — Multi-column Benchmark
================================================
Compares multi-column filter + projection performance.
Both engines run in Docker, accessed via HTTP.
"""
import argparse
import json
import math
import os
import platform
import random
import statistics
import subprocess
import sys
import time
try:
import clickhouse_connect
except ImportError:
print("ERROR: clickhouse-connect not installed")
sys.exit(1)
from velesdb_client import VelesDBClient
# ---------------------------------------------------------------------------
# Configuration
# ---------------------------------------------------------------------------
SEED = 42
DIMENSION = 128
TOP_K = 100
WARMUP_ROUNDS = 10
MEASURE_ROUNDS = 100
DEFAULT_DATASETS = [10_000, 100_000]
BATCH_SIZE = 1000
CATEGORIES = ["tech", "science", "business", "sports", "health", "music", "art", "food"]
REGIONS = ["eu-west", "eu-east", "us-west", "us-east", "asia-pac",
"latam", "africa", "mena", "oceania", "nordic"]
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def pseudo_embedding(i: int, dim: int = DIMENSION) -> list:
return [(math.sin(i * 0.01 + j * 0.01)) for j in range(dim)]
def percentile(data, p):
s = sorted(data)
k = (len(s) - 1) * (p / 100)
f = int(k)
c = f + 1
if c >= len(s):
return s[f]
return s[f] + (k - f) * (s[c] - s[f])
def fmt_time(seconds: float) -> str:
if seconds < 0.001:
return f"{seconds * 1_000_000:.0f} µs"
return f"{seconds * 1_000:.2f} ms"
def measure(func, warmup=None, rounds=None) -> dict:
if warmup is None:
warmup = WARMUP_ROUNDS
if rounds is None:
rounds = MEASURE_ROUNDS
for _ in range(warmup):
func()
times = []
for _ in range(rounds):
t0 = time.perf_counter()
result = func()
t1 = time.perf_counter()
times.append(t1 - t0)
return {
"mean": statistics.mean(times),
"median": percentile(times, 50),
"p99": percentile(times, 99),
"min": min(times),
"stdev": statistics.stdev(times) if len(times) > 1 else 0,
"rounds": rounds,
"result_sample": result,
}
# ---------------------------------------------------------------------------
# Data generation
# ---------------------------------------------------------------------------
def generate_dataset(n: int) -> list[dict]:
random.seed(SEED)
rows = []
for i in range(n):
rows.append({
"id": i,
"category": CATEGORIES[i % len(CATEGORIES)],
"price": round(random.uniform(1.0, 999.99), 2),
"stock": random.randint(0, 200),
"rating": round(random.uniform(1.0, 5.0), 1),
"region": REGIONS[i % len(REGIONS)],
"title": f"Product {i}",
})
return rows
# ---------------------------------------------------------------------------
# ClickHouse setup
# ---------------------------------------------------------------------------
def setup_clickhouse(client, rows: list[dict], table: str = "products"):
client.command(f"DROP TABLE IF EXISTS {table}")
client.command(f"""
CREATE TABLE {table} (
id UInt64, category LowCardinality(String), price Float64,
stock UInt32, rating Float32, region LowCardinality(String), title String
) ENGINE = MergeTree() ORDER BY id
""")
col_names = ["id", "category", "price", "stock", "rating", "region", "title"]
for start in range(0, len(rows), 5000):
batch = rows[start:start + 5000]
data = [[r[c] for c in col_names] for r in batch]
client.insert(table, data, column_names=col_names)
return int(client.command(f"SELECT count() FROM {table}"))
# ---------------------------------------------------------------------------
# VelesDB setup
# ---------------------------------------------------------------------------
def setup_velesdb(client: VelesDBClient, rows: list[dict], col_name: str = "products"):
client.delete_collection(col_name)
client.create_collection(col_name, dimension=DIMENSION, metric="cosine")
for start in range(0, len(rows), BATCH_SIZE):
batch = rows[start:start + BATCH_SIZE]
points = []
for r in batch:
points.append({
"id": r["id"],
"vector": pseudo_embedding(r["id"], DIMENSION),
"payload": {
"category": r["category"], "price": r["price"],
"stock": r["stock"], "rating": r["rating"],
"region": r["region"], "title": r["title"],
},
})
client.upsert_points(col_name, points)
# ---------------------------------------------------------------------------
# Benchmark operations
# ---------------------------------------------------------------------------
def run_benchmarks(n: int, ch_client, veles: VelesDBClient, col_name: str) -> dict:
query_vec = pseudo_embedding(9999, DIMENSION)
results = {"dataset_size": n, "operations": {}}
# OP1: 3-predicate filter + 4-col projection
op_name = "filter_3pred_project_4col"
print(f" {op_name}...")
def ch_op1():
return ch_client.query(
"SELECT category, price, title, rating FROM products "
"WHERE category = 'tech' AND price > 100 AND stock < 50"
).result_rows
def veles_op1():
res = veles.search(col_name, vector=query_vec, top_k=TOP_K, filter={
"condition": {"type": "and", "conditions": [
{"type": "eq", "field": "category", "value": "tech"},
{"type": "gt", "field": "price", "value": 100},
{"type": "lt", "field": "stock", "value": 50},
]}
})
return [(r["payload"]["category"], r["payload"]["price"],
r["payload"]["title"], r["payload"]["rating"]) for r in res]
ch_m = measure(ch_op1)
veles_m = measure(veles_op1)
results["operations"][op_name] = {
"description": "WHERE category='tech' AND price>100 AND stock<50 → 4 cols",
"clickhouse": {k: v for k, v in ch_m.items() if k != "result_sample"},
"velesdb": {k: v for k, v in veles_m.items() if k != "result_sample"},
"ch_result_count": len(ch_m["result_sample"]),
"veles_result_count": len(veles_m["result_sample"]),
}
# OP2: Nested AND/OR filter
op_name = "filter_nested_and_or"
print(f" {op_name}...")
def ch_op2():
return ch_client.query(
"SELECT id, category, price, rating, region FROM products "
"WHERE category IN ('tech', 'science') AND price >= 50 AND price <= 500 AND rating >= 3.5"
).result_rows
def veles_op2():
res = veles.search(col_name, vector=query_vec, top_k=TOP_K, filter={
"condition": {"type": "and", "conditions": [
{"type": "in", "field": "category", "values": ["tech", "science"]},
{"type": "gte", "field": "price", "value": 50},
{"type": "lte", "field": "price", "value": 500},
{"type": "gte", "field": "rating", "value": 3.5},
]}
})
return [(r["id"], r["payload"]["category"], r["payload"]["price"],
r["payload"]["rating"], r["payload"]["region"]) for r in res]
ch_m = measure(ch_op2)
veles_m = measure(veles_op2)
results["operations"][op_name] = {
"description": "category IN (tech,science) AND price 50..500 AND rating>=3.5",
"clickhouse": {k: v for k, v in ch_m.items() if k != "result_sample"},
"velesdb": {k: v for k, v in veles_m.items() if k != "result_sample"},
"ch_result_count": len(ch_m["result_sample"]),
"veles_result_count": len(veles_m["result_sample"]),
}
# OP3: Single predicate + all columns
op_name = "filter_1pred_project_all"
print(f" {op_name}...")
def ch_op3():
return ch_client.query(
"SELECT * FROM products WHERE region = 'eu-west' LIMIT 100"
).result_rows
def veles_op3():
res = veles.search(col_name, vector=query_vec, top_k=TOP_K, filter={
"condition": {"type": "eq", "field": "region", "value": "eu-west"}
})
return [(r["id"], r["payload"]["category"], r["payload"]["price"],
r["payload"]["stock"], r["payload"]["rating"],
r["payload"]["region"], r["payload"]["title"]) for r in res]
ch_m = measure(ch_op3)
veles_m = measure(veles_op3)
results["operations"][op_name] = {
"description": "WHERE region='eu-west' LIMIT 100 → all columns",
"clickhouse": {k: v for k, v in ch_m.items() if k != "result_sample"},
"velesdb": {k: v for k, v in veles_m.items() if k != "result_sample"},
"ch_result_count": len(ch_m["result_sample"]),
"veles_result_count": len(veles_m["result_sample"]),
}
# OP4: Vector search + payload
op_name = "vector_search_payload"
print(f" {op_name}...")
def ch_op4():
return ch_client.query(
"SELECT id, category, price, rating FROM products ORDER BY price DESC LIMIT 100"
).result_rows
def veles_op4():
res = veles.search(col_name, vector=query_vec, top_k=TOP_K)
return [(r["id"], r["payload"]["category"], r["payload"]["price"],
r["payload"]["rating"]) for r in res]
ch_m = measure(ch_op4)
veles_m = measure(veles_op4)
results["operations"][op_name] = {
"description": "Top-100 retrieval + 4-col projection",
"clickhouse": {k: v for k, v in ch_m.items() if k != "result_sample"},
"velesdb": {k: v for k, v in veles_m.items() if k != "result_sample"},
"note": "Different ranking strategies",
}
return results
# ---------------------------------------------------------------------------
# Display
# ---------------------------------------------------------------------------
def print_results(all_results: list[dict], machine: dict):
print("\n" + "=" * 78)
print(" VelesDB vs ClickHouse — Multi-column Benchmark")
print("=" * 78)
print(f" Runtime: All engines in Docker, accessed via HTTP")
print(f" VelesDB: {machine.get('velesdb', '?')}")
print(f" CH: {machine.get('clickhouse', '?')}")
print(f" Rounds: {MEASURE_ROUNDS} (warmup: {WARMUP_ROUNDS})")
print("=" * 78)
for res in all_results:
n = res["dataset_size"]
print(f"\n{'─' * 78}")
print(f" Dataset: {n:,} rows")
for op_name, op_data in res["operations"].items():
ch = op_data["clickhouse"]
vl = op_data["velesdb"]
if ch["median"] > 0 and vl["median"] > 0:
ratio = ch["median"] / vl["median"]
winner = f"VelesDB {ratio:.1f}x faster" if ratio > 1 else f"ClickHouse {1/ratio:.1f}x faster"
else:
winner = "N/A"
print(f"\n ▸ {op_name}: {op_data['description']}")
print(f" {'Engine':<14} {'Median':>12} {'P99':>12} {'Mean':>12}")
print(f" {'─' * 50}")
print(f" {'ClickHouse':<14} {fmt_time(ch['median']):>12} {fmt_time(ch['p99']):>12} {fmt_time(ch['mean']):>12}")
print(f" {'VelesDB':<14} {fmt_time(vl['median']):>12} {fmt_time(vl['p99']):>12} {fmt_time(vl['mean']):>12}")
print(f" → {winner}")
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
def main():
global MEASURE_ROUNDS, WARMUP_ROUNDS
parser = argparse.ArgumentParser(description="VelesDB vs ClickHouse multi-column benchmark")
parser.add_argument("--datasets", type=int, nargs="+", default=DEFAULT_DATASETS)
parser.add_argument("--json", action="store_true")
parser.add_argument("--rounds", type=int, default=100)
parser.add_argument("--warmup", type=int, default=10)
parser.add_argument("--velesdb-host", default="localhost")
parser.add_argument("--velesdb-port", type=int, default=8080)
parser.add_argument("--ch-host", default="localhost")
parser.add_argument("--ch-port", type=int, default=8123)
args = parser.parse_args()
MEASURE_ROUNDS = args.rounds
WARMUP_ROUNDS = args.warmup
print("VelesDB vs ClickHouse — Multi-column Benchmark")
print("=" * 50)
# Connect to VelesDB
veles = VelesDBClient(host=args.velesdb_host, port=args.velesdb_port)
try:
info = veles.health()
machine = {"velesdb": info.get("version", "?")}
print(f" VelesDB {machine['velesdb']} OK")
except Exception as e:
print(f" ERROR VelesDB: {e}")
sys.exit(1)
# Connect to ClickHouse
try:
ch_client = clickhouse_connect.get_client(host=args.ch_host, port=args.ch_port)
machine["clickhouse"] = str(ch_client.command("SELECT version()"))
print(f" ClickHouse {machine['clickhouse']} OK")
except Exception as e:
print(f" ERROR ClickHouse: {e}")
sys.exit(1)
all_results = []
for n in args.datasets:
print(f"\n Dataset: {n:,} rows")
rows = generate_dataset(n)
print(" Loading into ClickHouse...")
t0 = time.perf_counter()
setup_clickhouse(ch_client, rows)
ch_load = time.perf_counter() - t0
print(f" ClickHouse: {ch_load:.2f}s")
col_name = f"products_{n}"
print(" Loading into VelesDB...")
t0 = time.perf_counter()
setup_velesdb(veles, rows, col_name)
veles_load = time.perf_counter() - t0
print(f" VelesDB: {veles_load:.2f}s")
results = run_benchmarks(n, ch_client, veles, col_name)
results["load_times"] = {"clickhouse": ch_load, "velesdb": veles_load}
all_results.append(results)
veles.delete_collection(col_name)
if args.json:
print(json.dumps({"machine": machine, "results": all_results}, indent=2, default=str))
else:
print_results(all_results, machine)
print("\n Done.")
if __name__ == "__main__":
main()