Spaces:
Sleeping
Sleeping
File size: 10,448 Bytes
2923639 | 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 | #!/usr/bin/env python3
"""
WayyDB Read/Write Performance Benchmarks
"""
import time
import sys
import numpy as np
# Add local build to path
sys.path.insert(0, "/home/rcgalbo/wayy-research/wf/wayyDB/build")
import wayy_db as wdb
def format_rate(rows: int, elapsed: float) -> str:
rate = rows / elapsed
if rate >= 1e6:
return f"{rate/1e6:.2f}M rows/sec"
elif rate >= 1e3:
return f"{rate/1e3:.2f}K rows/sec"
else:
return f"{rate:.2f} rows/sec"
def format_bytes(bytes_count: int, elapsed: float) -> str:
rate = bytes_count / elapsed
if rate >= 1e9:
return f"{rate/1e9:.2f} GB/sec"
elif rate >= 1e6:
return f"{rate/1e6:.2f} MB/sec"
else:
return f"{rate/1e3:.2f} KB/sec"
def bench_write(sizes: list[int]):
"""Benchmark table creation/write performance."""
print("\n=== WRITE PERFORMANCE ===\n")
print(f"{'Rows':<12} {'Time (ms)':<12} {'Rate':<20} {'Throughput':<15}")
print("-" * 60)
for n in sizes:
# Generate data
timestamps = np.arange(n, dtype=np.int64)
prices = np.random.uniform(100, 200, n).astype(np.float64)
volumes = np.random.randint(100, 10000, n).astype(np.int64)
symbols = np.random.randint(0, 100, n).astype(np.uint32)
# Time table creation
start = time.perf_counter()
table = wdb.Table(f"bench_{n}")
table.add_column_from_numpy("timestamp", timestamps, wdb.DType.Timestamp)
table.add_column_from_numpy("price", prices, wdb.DType.Float64)
table.add_column_from_numpy("volume", volumes, wdb.DType.Int64)
table.add_column_from_numpy("symbol", symbols, wdb.DType.Symbol)
table.set_sorted_by("timestamp")
elapsed = time.perf_counter() - start
# Calculate bytes (8+8+8+4 = 28 bytes per row)
bytes_per_row = 28
total_bytes = n * bytes_per_row
print(f"{n:<12,} {elapsed*1000:<12.2f} {format_rate(n, elapsed):<20} {format_bytes(total_bytes, elapsed):<15}")
return table # Return last table for further tests
def bench_read(table: wdb.Table):
"""Benchmark read/access performance."""
print("\n=== READ PERFORMANCE ===\n")
n = table.num_rows
# Column access
start = time.perf_counter()
for _ in range(100):
col = table["price"]
elapsed = time.perf_counter() - start
print(f"Column lookup (100x): {elapsed*1000:.3f}ms ({elapsed*10:.3f}ms per lookup)")
# Zero-copy numpy access
start = time.perf_counter()
for _ in range(100):
arr = table["price"].to_numpy()
elapsed = time.perf_counter() - start
print(f"to_numpy() (100x): {elapsed*1000:.3f}ms ({elapsed*10:.3f}ms per call)")
# Full table scan (sum all values)
col = table["price"]
start = time.perf_counter()
for _ in range(10):
total = wdb.ops.sum(col)
elapsed = time.perf_counter() - start
print(f"Full scan sum (10x): {elapsed*1000:.3f}ms ({elapsed*100:.3f}ms per scan)")
print(f" -> {format_rate(n * 10, elapsed)}")
def bench_aggregations(table: wdb.Table):
"""Benchmark aggregation operations."""
print("\n=== AGGREGATION PERFORMANCE ===\n")
n = table.num_rows
col = table["price"]
ops = [
("sum", wdb.ops.sum),
("avg", wdb.ops.avg),
("min", wdb.ops.min),
("max", wdb.ops.max),
("std", wdb.ops.std),
]
print(f"{'Operation':<12} {'Time (ms)':<12} {'Rate':<20}")
print("-" * 45)
for name, func in ops:
# Warm up
func(col)
# Benchmark
start = time.perf_counter()
for _ in range(100):
result = func(col)
elapsed = time.perf_counter() - start
print(f"{name:<12} {elapsed*10:.3f} {format_rate(n * 100, elapsed)}")
def bench_window_functions(table: wdb.Table):
"""Benchmark window functions."""
print("\n=== WINDOW FUNCTION PERFORMANCE ===\n")
n = table.num_rows
col = table["price"]
ops = [
("mavg(20)", lambda c: wdb.ops.mavg(c, 20)),
("msum(20)", lambda c: wdb.ops.msum(c, 20)),
("mstd(20)", lambda c: wdb.ops.mstd(c, 20)),
("ema(0.1)", lambda c: wdb.ops.ema(c, 0.1)),
("diff(1)", lambda c: wdb.ops.diff(c, 1)),
("pct_change", lambda c: wdb.ops.pct_change(c, 1)),
]
print(f"{'Operation':<15} {'Time (ms)':<12} {'Rate':<20}")
print("-" * 50)
for name, func in ops:
# Warm up
func(col)
# Benchmark
start = time.perf_counter()
for _ in range(10):
result = func(col)
elapsed = time.perf_counter() - start
print(f"{name:<15} {elapsed*100:.3f} {format_rate(n * 10, elapsed)}")
def bench_joins():
"""Benchmark temporal join operations."""
print("\n=== JOIN PERFORMANCE ===\n")
sizes = [(10_000, 10_000), (100_000, 100_000), (1_000_000, 1_000_000)]
print(f"{'Left x Right':<20} {'aj (ms)':<12} {'Rate':<20}")
print("-" * 55)
for left_n, right_n in sizes:
# Create left table (trades)
left = wdb.Table("trades")
left.add_column_from_numpy("timestamp",
np.sort(np.random.randint(0, left_n * 10, left_n)).astype(np.int64),
wdb.DType.Timestamp)
left.add_column_from_numpy("symbol",
np.random.randint(0, 10, left_n).astype(np.uint32),
wdb.DType.Symbol)
left.add_column_from_numpy("price",
np.random.uniform(100, 200, left_n).astype(np.float64),
wdb.DType.Float64)
left.set_sorted_by("timestamp")
# Create right table (quotes)
right = wdb.Table("quotes")
right.add_column_from_numpy("timestamp",
np.sort(np.random.randint(0, right_n * 10, right_n)).astype(np.int64),
wdb.DType.Timestamp)
right.add_column_from_numpy("symbol",
np.random.randint(0, 10, right_n).astype(np.uint32),
wdb.DType.Symbol)
right.add_column_from_numpy("bid",
np.random.uniform(99, 199, right_n).astype(np.float64),
wdb.DType.Float64)
right.add_column_from_numpy("ask",
np.random.uniform(101, 201, right_n).astype(np.float64),
wdb.DType.Float64)
right.set_sorted_by("timestamp")
# Warm up
if left_n <= 100_000:
wdb.ops.aj(left, right, ["symbol"], "timestamp")
# Benchmark as-of join
start = time.perf_counter()
result = wdb.ops.aj(left, right, ["symbol"], "timestamp")
elapsed = time.perf_counter() - start
size_str = f"{left_n//1000}K x {right_n//1000}K"
print(f"{size_str:<20} {elapsed*1000:<12.2f} {format_rate(left_n, elapsed)}")
def bench_persistence(n: int = 1_000_000):
"""Benchmark save/load/mmap performance."""
print("\n=== PERSISTENCE PERFORMANCE ===\n")
import tempfile
import os
# Create table
table = wdb.Table("persist_test")
table.add_column_from_numpy("timestamp",
np.arange(n, dtype=np.int64), wdb.DType.Timestamp)
table.add_column_from_numpy("price",
np.random.uniform(100, 200, n).astype(np.float64), wdb.DType.Float64)
table.add_column_from_numpy("volume",
np.random.randint(100, 10000, n).astype(np.int64), wdb.DType.Int64)
table.set_sorted_by("timestamp")
bytes_total = n * (8 + 8 + 8) # 24 bytes per row
with tempfile.TemporaryDirectory() as tmpdir:
path = os.path.join(tmpdir, "test_table")
# Benchmark save
start = time.perf_counter()
table.save(path)
save_elapsed = time.perf_counter() - start
print(f"Save {n:,} rows: {save_elapsed*1000:.2f}ms ({format_bytes(bytes_total, save_elapsed)})")
# Benchmark load (copies data)
start = time.perf_counter()
loaded = wdb.Table.load(path)
load_elapsed = time.perf_counter() - start
print(f"Load {n:,} rows: {load_elapsed*1000:.2f}ms ({format_bytes(bytes_total, load_elapsed)})")
# Benchmark mmap (zero-copy)
start = time.perf_counter()
mmapped = wdb.Table.mmap(path)
mmap_elapsed = time.perf_counter() - start
print(f"Mmap {n:,} rows: {mmap_elapsed*1000:.2f}ms ({format_bytes(bytes_total, mmap_elapsed)})")
print(f" -> mmap is {load_elapsed/mmap_elapsed:.0f}x faster than load")
def bench_concurrent():
"""Benchmark concurrent read performance."""
print("\n=== CONCURRENT READ PERFORMANCE ===\n")
import threading
n = 1_000_000
table = wdb.Table("concurrent_test")
table.add_column_from_numpy("price",
np.random.uniform(100, 200, n).astype(np.float64), wdb.DType.Float64)
col = table["price"]
def worker(results, idx):
for _ in range(10):
results[idx] = wdb.ops.sum(col)
for num_threads in [1, 2, 4, 8]:
results = [0.0] * num_threads
threads = [threading.Thread(target=worker, args=(results, i))
for i in range(num_threads)]
start = time.perf_counter()
for t in threads:
t.start()
for t in threads:
t.join()
elapsed = time.perf_counter() - start
ops_per_sec = (num_threads * 10) / elapsed
print(f"{num_threads} threads: {elapsed*1000:.2f}ms ({ops_per_sec:.1f} ops/sec, {format_rate(n * num_threads * 10, elapsed)})")
if __name__ == "__main__":
print("=" * 60)
print(" WayyDB Performance Benchmarks")
print("=" * 60)
# Write benchmarks with increasing sizes
sizes = [10_000, 100_000, 1_000_000, 10_000_000]
table = bench_write(sizes)
# Use 1M row table for read tests
table_1m = wdb.Table("bench_1m")
n = 1_000_000
table_1m.add_column_from_numpy("timestamp", np.arange(n, dtype=np.int64), wdb.DType.Timestamp)
table_1m.add_column_from_numpy("price", np.random.uniform(100, 200, n).astype(np.float64), wdb.DType.Float64)
table_1m.add_column_from_numpy("volume", np.random.randint(100, 10000, n).astype(np.int64), wdb.DType.Int64)
table_1m.add_column_from_numpy("symbol", np.random.randint(0, 100, n).astype(np.uint32), wdb.DType.Symbol)
table_1m.set_sorted_by("timestamp")
bench_read(table_1m)
bench_aggregations(table_1m)
bench_window_functions(table_1m)
bench_joins()
bench_persistence()
bench_concurrent()
print("\n" + "=" * 60)
print(" Benchmarks Complete")
print("=" * 60)
|