-
Notifications
You must be signed in to change notification settings - Fork 193
Add script to benchmark column stats creation #3106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 11 commits
77c7a25
adabce9
e3a2717
73c969d
4c95eef
a923e70
92cf7ab
6be8596
5336d74
1cb6325
ea73298
b84a38e
9ec27ff
1317110
8296670
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import json | ||
| import logging | ||
| import resource | ||
| import sys | ||
| import time | ||
|
|
||
| from ahl.mongo import NativeMongoose | ||
|
|
||
| logging.getLogger("man.vault.client").setLevel(logging.WARNING) | ||
| logging.getLogger("man.secrets.api").setLevel(logging.WARNING) | ||
|
|
||
|
|
||
| def main(): | ||
| cols = int(sys.argv[1]) | ||
|
|
||
| lib = NativeMongoose("mktdatad").get_library("pmarkovski.columns_stats", api="v2") | ||
| nvs = lib._nvs | ||
| column_stats_spec = {f"col_{i}": {"MINMAX"} for i in range(cols)} | ||
|
|
||
| start = time.time() | ||
| nvs.create_column_stats("test_symbol", column_stats_spec) | ||
| end = time.time() | ||
|
|
||
| peak_rss_mb = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss | ||
|
|
||
| nvs.drop_column_stats("test_symbol") | ||
|
|
||
| print(json.dumps({ | ||
| "elapsed_seconds": end - start, | ||
| "peak_rss_mb": peak_rss_mb / 1024, | ||
| })) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,114 @@ | ||||||||||||
| import json | ||||||||||||
| import statistics | ||||||||||||
| import subprocess | ||||||||||||
| import sys | ||||||||||||
| from dataclasses import dataclass, field | ||||||||||||
| from pathlib import Path | ||||||||||||
|
|
||||||||||||
| from ahl.mongo import NativeMongoose | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| WARMUP_RUNS = 2 | ||||||||||||
| RUNS = 10 | ||||||||||||
| WRITE_SYMBOL_SCRIPT = Path(__file__).parent / "col_stats_bench_write_symbol.py" | ||||||||||||
| CREATE_STATS_SCRIPT = Path(__file__).parent / "col_stats_bench_create_stats.py" | ||||||||||||
|
|
||||||||||||
| SCENARIOS = [ | ||||||||||||
| (10, 10), | ||||||||||||
| (1_000, 1_000), | ||||||||||||
| (100_000, 1_000), | ||||||||||||
| (100_000, 10_000), | ||||||||||||
| (1_000_000, 1_000), | ||||||||||||
| (1_000_000, 5_000), | ||||||||||||
| (10_000_000, 1_000), | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This push reinstates the massive-scenario block that the previous review summary tracked as resolved. Be aware of what this implies for the worker:
If these are intentional targets against the shared Man Group mongo, fine — but (a) call this out in the PR description so reviewers know the expected wall-clock and storage cost, and (b) consider gating the heavy scenarios behind a |
||||||||||||
| ] | ||||||||||||
|
|
||||||||||||
| @dataclass | ||||||||||||
| class Result: | ||||||||||||
| rows: int = 0 | ||||||||||||
| cols: int = 0 | ||||||||||||
| symbol_write_time: float = 0.0 | ||||||||||||
| stats_create_times: list = field(default_factory=list) | ||||||||||||
| stats_rss_use: list = field(default_factory=list) | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| results = [Result() for _ in SCENARIOS] | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| def run_subprocess(script, args, label): | ||||||||||||
| try: | ||||||||||||
| completed = subprocess.run( | ||||||||||||
| [sys.executable, str(script), *map(str, args)], | ||||||||||||
| stdout=subprocess.PIPE, stderr=sys.stderr, text=True, check=True, | ||||||||||||
| ) | ||||||||||||
| return json.loads(completed.stdout) | ||||||||||||
| except subprocess.CalledProcessError as e: | ||||||||||||
| killed_by_signal = e.returncode < 0 | ||||||||||||
| reason = f"killed by signal {-e.returncode}" if killed_by_signal else f"exit code {e.returncode}" | ||||||||||||
| raise RuntimeError(f"[{label}] subprocess failed ({reason})") from None | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| def measure(scenario, index): | ||||||||||||
| rows, cols = scenario | ||||||||||||
| results[index].rows = rows | ||||||||||||
| results[index].cols = cols | ||||||||||||
|
|
||||||||||||
| print(f" [write_symbol] {rows}x{cols}", file=sys.stderr) | ||||||||||||
| results[index].symbol_write_time = run_subprocess( | ||||||||||||
| WRITE_SYMBOL_SCRIPT, [rows, cols], "write_symbol" | ||||||||||||
| )["elapsed_seconds"] | ||||||||||||
|
|
||||||||||||
| for i in range(1, WARMUP_RUNS + 1): | ||||||||||||
| print(f" [create_stats] warmup {i}/{WARMUP_RUNS}", file=sys.stderr) | ||||||||||||
| run_subprocess(CREATE_STATS_SCRIPT, [cols], "create_stats") | ||||||||||||
|
|
||||||||||||
| for i in range(1, RUNS + 1): | ||||||||||||
| print(f" [create_stats] run {i}/{RUNS}", file=sys.stderr) | ||||||||||||
| r = run_subprocess(CREATE_STATS_SCRIPT, [cols], "create_stats") | ||||||||||||
|
|
||||||||||||
| results[index].stats_create_times.append(r["elapsed_seconds"]) | ||||||||||||
| results[index].stats_rss_use.append(r["peak_rss_mb"]) | ||||||||||||
|
|
||||||||||||
| cleanup() | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| def print_results(): | ||||||||||||
| cw = 14 | ||||||||||||
| header = ( | ||||||||||||
| f"{'rows':>12} {'cols':>8}" | ||||||||||||
| f" {'write_s':>{cw}}" | ||||||||||||
| f" {'time_mean':>{cw}} {'time_median':>{cw}} {'time_max':>{cw}}" | ||||||||||||
| f" {'rss_mean_mb':>{cw}} {'rss_median_mb':>{cw}} {'rss_max_mb':>{cw}}" | ||||||||||||
| ) | ||||||||||||
| print() | ||||||||||||
| print(header) | ||||||||||||
| print("-" * len(header)) | ||||||||||||
|
|
||||||||||||
| for r in results: | ||||||||||||
| t = r.stats_create_times | ||||||||||||
| m = r.stats_rss_use | ||||||||||||
| print( | ||||||||||||
| f"{r.rows:>12,} {r.cols:>8,}" | ||||||||||||
| f" {r.symbol_write_time:>{cw}.2f}" | ||||||||||||
| f" {statistics.mean(t):>{cw}.2f} {statistics.median(t):>{cw}.2f} {max(t):>{cw}.2f}" | ||||||||||||
| f" {statistics.mean(m):>{cw}.1f} {statistics.median(m):>{cw}.1f} {max(m):>{cw}.1f}" | ||||||||||||
| ) | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| def cleanup(): | ||||||||||||
| lib = NativeMongoose("mktdatad").get_library("pmarkovski.columns_stats", api="v2") | ||||||||||||
| try: | ||||||||||||
| lib.delete("test_symbol") | ||||||||||||
| except Exception: | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bare
Suggested change
Also note that this is called from |
||||||||||||
| pass | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| if __name__ == "__main__": | ||||||||||||
| cleanup() | ||||||||||||
| try: | ||||||||||||
| for i, scenario in enumerate(SCENARIOS): | ||||||||||||
| print(f"\n=== scenario {scenario[0]}x{scenario[1]} ===", file=sys.stderr) | ||||||||||||
| measure(scenario, i) | ||||||||||||
| finally: | ||||||||||||
| cleanup() | ||||||||||||
| print_results() | ||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import json | ||
| import logging | ||
| import resource | ||
| import sys | ||
| import time | ||
|
|
||
| import numpy as np | ||
| import pandas as pd | ||
| from ahl.mongo import NativeMongoose | ||
|
|
||
| logging.getLogger("man.vault.client").setLevel(logging.WARNING) | ||
| logging.getLogger("man.secrets.api").setLevel(logging.WARNING) | ||
|
|
||
|
|
||
| CHUNK_ROWS = 100_000 | ||
|
|
||
| def main(): | ||
| rows, cols = int(sys.argv[1]), int(sys.argv[2]) | ||
| column_names = [f"col_{i}" for i in range(cols)] | ||
|
|
||
| lib = NativeMongoose("mktdatad").get_library("pmarkovski.columns_stats", api="v2") | ||
|
|
||
| total_elapsed = 0.0 | ||
|
|
||
| for chunk_start in range(0, rows, CHUNK_ROWS): | ||
| chunk_row_count = min(CHUNK_ROWS, rows - chunk_start) | ||
|
|
||
| chunk = pd.DataFrame( | ||
| np.random.rand(chunk_row_count, cols).astype(np.float64), | ||
| columns=column_names, | ||
| ) | ||
|
|
||
| chunk_mb = chunk.memory_usage(deep=True).sum() / 1024 / 1024 | ||
| print(f" chunk [{chunk_start}:{chunk_start + chunk_row_count}] {chunk.shape} {chunk_mb:.1f} MB", file=sys.stderr, flush=True) | ||
|
|
||
| start_time = time.time() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: If the intent of this move was to exclude total_elapsed = 0.0
for chunk_start in range(0, rows, CHUNK_ROWS):
chunk_row_count = min(CHUNK_ROWS, rows - chunk_start)
chunk = pd.DataFrame(
np.random.rand(chunk_row_count, cols).astype(np.float64),
columns=column_names,
)
start_time = time.time()
if chunk_start == 0:
lib.write(SYMBOL_NAME, chunk)
else:
lib.append(SYMBOL_NAME, chunk)
total_elapsed += time.time() - start_time
elapsed_seconds = total_elapsedAlso: line 27 has trailing whitespace and lines 24/32 add stray blank lines — these will likely be flagged by |
||
|
|
||
| if chunk_start == 0: | ||
| lib.write("test_symbol", chunk) | ||
| else: | ||
| lib.append("test_symbol", chunk) | ||
|
|
||
| total_elapsed += (time.time() - start_time) | ||
|
|
||
| peak_rss_mb = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1024 # ru_maxrss is KB on Linux | ||
|
|
||
| print(json.dumps({ | ||
| "elapsed_seconds": total_elapsed, | ||
| "peak_rss_mb": peak_rss_mb, | ||
| })) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This push replaces self-contained LMDB with
ahl.mongo.NativeMongooseand hardcodes a user-specific library namespacepmarkovski.columns_stats. Two concrete problems:mktdatadinstance.Please parametrise connection/library/symbol via argv or env vars (e.g.
ARCTICDB_BENCH_LIBRARY,ARCTICDB_BENCH_SYMBOL) and pass them from the orchestrator, or at least defineLIBRARY/SYMBOLconstants in a shared module rather than triplicating the literals.