diff --git a/test.py b/test.py index edb13a3..c672a02 100755 --- a/test.py +++ b/test.py @@ -1,27 +1,31 @@ +import argparse +import filecmp import os +import shutil import subprocess +import sys +import tempfile import time -# --- Configuration --- +TEST_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "test_data") +DEFAULT_SOURCE_DIR = os.path.join(TEST_DIR, "source") +DEFAULT_DEST_DIR = os.path.join(TEST_DIR, "dest") + SERVER_CMD = ["./build/server"] base_client_cmd = ["./build/client"] -# --- Resource Limit Configuration --- -# 💾 Disk throttling settings -DISK_DEVICE = ( - "/dev/nvme0n1p5" # IMPORTANT: Change this to your disk (e.g., /dev/nvme0n1) -) -READ_BPS_MAX = "15M" # Max read speed (M for megabytes) -WRITE_BPS_MAX = "10M" # Max write speed +DISK_DEVICE = "/dev/nvme0n1p5" +READ_BPS_MAX = "15M" +WRITE_BPS_MAX = "10M" -# 🐢 Network throttling settings (Linux tc) NET_LIMIT = "100mbit" NET_DELAY = "100ms" NETWORK_INTERFACE = "lo" -NET_LIMIT_CMD = f"sudo tc qdisc add dev {NETWORK_INTERFACE} root netem rate {NET_LIMIT} delay {NET_DELAY}".split() +NET_LIMIT_CMD = ( + f"sudo tc qdisc add dev {NETWORK_INTERFACE} root netem rate {NET_LIMIT} delay {NET_DELAY}".split() +) NET_RESET_CMD = f"sudo tc qdisc del dev {NETWORK_INTERFACE} root".split() -# --- Build the client command prefix with throttling --- CLIENT_CMD_PREFIX = [ "sudo", "systemd-run", @@ -32,7 +36,6 @@ CLIENT_CMD_PREFIX = [ f"IOWriteBandwidthMax={DISK_DEVICE} {WRITE_BPS_MAX}", ] -# --- Test Cases --- TEST_CASES = [ {"name": "Standard (Single-threaded)", "flags": []}, {"name": "Multithreading (-m)", "flags": ["-m"]}, @@ -48,179 +51,229 @@ TEST_CASES = [ ] -def run_suite(env_name, apply_limits): +def generate_test_files(source_dir): + if os.path.exists(source_dir): + shutil.rmtree(source_dir) + os.makedirs(source_dir) + + files = { + "small.txt": b"hello world\n", + "medium.txt": b"line\n" * 1000, + "binary.bin": bytes(range(256)), + "nested/subdir/deep.txt": b"deeply nested file\n", + "nested/another.txt": b"another nested file\n" * 50, + } + for rel_path, content in files.items(): + full_path = os.path.join(source_dir, rel_path) + os.makedirs(os.path.dirname(full_path), exist_ok=True) + with open(full_path, "wb") as f: + f.write(content) + + +def verify_transfer(source_dir, dest_dir): + source_dir = os.path.abspath(source_dir) + dest_dir = os.path.abspath(dest_dir) + + received_prefix = os.path.join(dest_dir, source_dir.lstrip(os.sep)) + if not os.path.exists(received_prefix): + return [], ["no received files found"] + + mismatches = [] + missing = [] + + for root, dirs, files in os.walk(source_dir): + for f in files: + src_path = os.path.join(root, f) + rel = os.path.relpath(src_path, source_dir) + dst_path = os.path.join(received_prefix, rel) + + if not os.path.exists(dst_path): + missing.append(rel) + elif not filecmp.cmp(src_path, dst_path, shallow=False): + mismatches.append(rel) + + return mismatches, missing + + +def run_suite(env_name, apply_limits, source_dir, dest_dir): results = [] print(f"\n{'=' * 60}") - print(f"🚀 Starting Suite: {env_name}") + print(f"Suite: {env_name}") print(f"{'=' * 60}") if apply_limits: - print( - f"Applying Disk I/O Limits: Reads <= {READ_BPS_MAX}, Writes <= {WRITE_BPS_MAX}" - ) - print(f"Applying Network Limits: {NET_LIMIT}, {NET_DELAY} delay") + print(f" Disk I/O: Reads <= {READ_BPS_MAX}, Writes <= {WRITE_BPS_MAX}") + print(f" Network: {NET_LIMIT}, {NET_DELAY} delay") client_prefix = CLIENT_CMD_PREFIX else: - print("Running Baseline (No limits applied)") - client_prefix = [] # Run normally without systemd-run/limits + print(" Baseline (no limits)") + client_prefix = [] try: - # SETUP: Apply or ensure clean network limits if apply_limits: subprocess.run(NET_LIMIT_CMD, check=True) else: - # Silently attempt to clear any leftover rules just to ensure a clean baseline subprocess.run(NET_RESET_CMD, capture_output=True) for case in TEST_CASES: name = case["name"] flags = case["flags"] + print(f"\n --- {name} ---") - print(f"\n--- Running: {name} ---") + if os.path.exists(dest_dir): + shutil.rmtree(dest_dir) server_process = None try: - # 1. Start the server - print(" Starting server...") server_process = subprocess.Popen( SERVER_CMD, stdout=subprocess.DEVNULL, stderr=None ) - time.sleep(0.5) # Allow server to bind to port + time.sleep(0.5) + + env = os.environ.copy() + env["FASTSYNC_SOURCE_DIR"] = source_dir + env["FASTSYNC_DEST_DIR"] = dest_dir + env["FASTSYNC_SAVE_TO_DISK"] = "true" - # 2. Build and run the client client_cmd = client_prefix + base_client_cmd + flags - print(f" Running client: {' '.join(client_cmd)}") + print(f" Running: {' '.join(client_cmd)}") start_time = time.monotonic() client_result = subprocess.run( - client_cmd, text=True, capture_output=True + client_cmd, env=env, text=True, capture_output=True ) end_time = time.monotonic() - duration = end_time - start_time + if server_process: + try: + server_process.wait(timeout=5) + except subprocess.TimeoutExpired: + server_process.kill() + server_process.wait() + server_process = None + + mismatches, missing = [], [] if client_result.returncode == 0: - results.append( - { - "environment": env_name, - "name": name, - "status": "Success", - "time": f"{duration:.4f}s", - "error": "", - } - ) + mismatches, missing = verify_transfer(source_dir, dest_dir) + + entry = { + "name": name, + "suite": env_name, + "time": f"{duration:.4f}s" if client_result.returncode == 0 else "N/A", + } + + if client_result.returncode == 0 and not mismatches and not missing: + entry["status"] = "Success" + entry["error"] = "" else: - print(f" ⚠️ Failed (code: {client_result.returncode})") - err_msg = ( - client_result.stderr.strip().split("\n")[0] - if client_result.stderr - else ( - client_result.stdout.strip().split("\n")[0] - if client_result.stdout - else "No output" + entry["status"] = "Failed" + errors = [] + if client_result.returncode != 0: + err = ( + client_result.stderr.strip().split("\n")[0] + if client_result.stderr + else ( + client_result.stdout.strip().split("\n")[0] + if client_result.stdout + else "No output" + ) ) - ) - results.append( - { - "environment": env_name, - "name": name, - "status": "Failed", - "time": "N/A", - "error": f"Exit code {client_result.returncode}: {err_msg[:40]}", - } - ) + errors.append(f"Exit code {client_result.returncode}: {err[:80]}") + if missing: + errors.append(f"Missing ({len(missing)}): {', '.join(missing[:5])}") + if mismatches: + errors.append(f"Mismatch ({len(mismatches)}): {', '.join(mismatches[:3])}") + entry["error"] = " | ".join(errors) + + results.append(entry) except subprocess.TimeoutExpired: - print(" ⚠️ Timeout (exceeded 15s)") results.append( - { - "environment": env_name, - "name": name, - "status": "Timeout", - "time": "N/A", - "error": "Exceeded 15 seconds", - } + {"name": name, "status": "Timeout", "time": "N/A", "error": "Exceeded 15s"} ) except Exception as e: - print(f" ❌ Error: {e}") results.append( - { - "environment": env_name, - "name": name, - "status": "Error", - "time": "N/A", - "error": str(e), - } + {"name": name, "status": "Error", "time": "N/A", "error": str(e)} ) finally: - # Clean up the server for this test case if server_process: - print(" Stopping server...") try: - server_process.terminate() server_process.wait(timeout=5) except subprocess.TimeoutExpired: server_process.kill() server_process.wait() except subprocess.CalledProcessError as e: - print(f"❌ Error running system limit command: {' '.join(e.cmd)}") - print("Are you running this script with 'sudo' privileges?") - + print(f" Error running limit command: {' '.join(e.cmd)}") finally: - # TEARDOWN: Remove network limits if they were applied if apply_limits: - print("\nCleaning up limits for this suite...") try: subprocess.run(NET_RESET_CMD, check=True, capture_output=True) - print("Network limits removed.") - except Exception as e: - print(f"⚠️ Could not reset network settings: {e}") + except Exception: + pass return results -os.system("cmake -B build -S .") -os.system("cd build && make") +def main(): + parser = argparse.ArgumentParser(description="FastSync integration test / benchmark") + parser.add_argument("--source-dir", default=DEFAULT_SOURCE_DIR, + help="Source directory for test files (default: %(default)s)") + parser.add_argument("--dest-dir", default=DEFAULT_DEST_DIR, + help="Destination directory for received files (default: %(default)s)") + parser.add_argument("--keep-data", action="store_true", + help="Keep test_data directory after run") + parser.add_argument("--no-throttled", action="store_true", + help="Skip throttled suite (requires sudo)") + parser.add_argument("--no-unlimited", action="store_true", + help="Skip unlimited suite") + args = parser.parse_args() -# --- Main Execution --- -all_results = [] + os.system("cmake -B build -S . > /dev/null 2>&1") + ret = os.system("cd build && make -j$(nproc) 2>&1 | tail -3") + if ret != 0: + print("Build failed") + sys.exit(1) -# 1. Run Baseline (No Limits) -all_results.extend(run_suite("Unlimited", apply_limits=False)) + generate_test_files(args.source_dir) + os.makedirs(args.dest_dir, exist_ok=True) -# # 2. Run Throttled (With Limits) -all_results.extend(run_suite("Throttled", apply_limits=True)) + try: + all_results = [] -# --- Print Comparison Table --- -print("\n" + "=" * 105) -print(f"{'fastSync BENCHMARK RESULTS (COMPARISON)':^105}") -print("=" * 105) -print( - f"{'Configuration':<45} | {'Environment':<12} | {'Status':<10} | {'Time':<10} | {'Details/Error':<20}" -) -print("-" * 105) + if not args.no_unlimited: + all_results.extend( + run_suite("Unlimited", False, args.source_dir, args.dest_dir) + ) -# Sort results by test case name first, then environment to easily compare -# This groups the baseline and throttled results for the same test next to each other -# sorted_results = sorted( -# all_results, -# key=lambda x: ( -# TEST_CASES.index( -# next(item for item in TEST_CASES if item["name"] == x["name"]) -# ), -# x["environment"], -# ), -# ) + if not args.no_throttled: + all_results.extend( + run_suite("Throttled", True, args.source_dir, args.dest_dir) + ) -for res in all_results: - status_symbol = ( - "✅" - if res["status"] == "Success" - else ("⏳" if res["status"] == "Timeout" else "❌") - ) - status_str = f"{status_symbol} {res['status']}" - print( - f"{res['name']:<45} | {res['environment']:<12} | {status_str:<10} | {res['time']:<10} | {res['error']:<20}" - ) -print("=" * 105) + print("\n" + "=" * 110) + print(f"{'RESULTS':^110}") + print("=" * 110) + print(f"{'Configuration':<45} | {'Suite':<12} | {'Status':<8} | {'Time':<10} | {'Details'}") + print("-" * 110) + + for res in all_results: + print( + f"{res['name']:<45} | {res['suite']:<12} | {res['status']:<8} | {res['time']:<10} | {res['error']}" + ) + + failed = [r for r in all_results if r["status"] != "Success"] + if failed: + print(f"\n {len(failed)} test(s) FAILED") + sys.exit(1) + else: + print(f"\n ALL {len(all_results)} TESTS PASSED") + + finally: + if not args.keep_data: + shutil.rmtree(TEST_DIR, ignore_errors=True) + + +if __name__ == "__main__": + main()