Merge pull request 'test: auto-generate test files, verify content, CLI args, test_data dir' (#7) from improve-test-py into main

Reviewed-on: #7
This commit is contained in:
2026-07-04 22:56:58 +02:00
5 changed files with 274 additions and 152 deletions
+2
View File
@@ -1,2 +1,4 @@
build build
data_copied data_copied
test_data/
__pycache__/
+8
View File
@@ -207,6 +207,14 @@ int main(int argc, char *argv[]) {
config->compression_level); config->compression_level);
} }
} }
} else if (strcmp(argv[i], "--source-dir") == 0 && i + 1 < argc) {
free(config->send_directory);
config->send_directory = str_dup(argv[++i]);
} else if (strcmp(argv[i], "--dest-dir") == 0 && i + 1 < argc) {
free(config->receive_root_directory);
config->receive_root_directory = str_dup(argv[++i]);
} else if (strcmp(argv[i], "--save-to-disk") == 0) {
config->save_to_disk = true;
} else { } else {
handle_arg(argv[i], "-m", &config->use_multithreading, handle_arg(argv[i], "-m", &config->use_multithreading,
"Enabled Multithreading"); "Enabled Multithreading");
+63 -28
View File
@@ -7,10 +7,14 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/stat.h>
#include <unistd.h>
DirectoryScanner *directory_scanner_create(char *root_directory) { DirectoryScanner *directory_scanner_create(char *root_directory) {
DirectoryScanner *scanner = malloc(sizeof(DirectoryScanner)); DirectoryScanner *scanner = malloc(sizeof(DirectoryScanner));
scanner->directories = queue_create(100, free); scanner->directories = queue_create(100, free);
scanner->current_dir = NULL;
scanner->current_path = NULL;
queue_enqueue(scanner->directories, str_dup(root_directory)); queue_enqueue(scanner->directories, str_dup(root_directory));
return scanner; return scanner;
} }
@@ -18,11 +22,16 @@ DirectoryScanner *directory_scanner_create(char *root_directory) {
void directory_scanner_destroy(DirectoryScanner *scanner) { void directory_scanner_destroy(DirectoryScanner *scanner) {
if (scanner == NULL) if (scanner == NULL)
return; return;
if (scanner->current_dir) {
closedir(scanner->current_dir);
scanner->current_dir = NULL;
}
free(scanner->current_path);
queue_destroy(scanner->directories); queue_destroy(scanner->directories);
free(scanner); free(scanner);
} }
Chunk *chunk_data_to_chunk(ArrayList *chunk_data) { static Chunk *chunk_data_to_chunk(ArrayList *chunk_data) {
void **chunk_items = array_list_to_array(chunk_data); void **chunk_items = array_list_to_array(chunk_data);
Chunk *chunk = chunk_create((File **)chunk_items, chunk_data->size); Chunk *chunk = chunk_create((File **)chunk_items, chunk_data->size);
free(chunk_items); free(chunk_items);
@@ -31,40 +40,66 @@ Chunk *chunk_data_to_chunk(ArrayList *chunk_data) {
return chunk; return chunk;
} }
static int open_next_directory(DirectoryScanner *scanner) {
if (scanner->current_dir) {
closedir(scanner->current_dir);
scanner->current_dir = NULL;
}
free(scanner->current_path);
if (queue_is_empty(scanner->directories))
return 0;
scanner->current_path = (char *)queue_dequeue(scanner->directories);
scanner->current_dir = opendir(scanner->current_path);
if (scanner->current_dir == NULL) {
perror("Could not open directory!");
exit(EXIT_FAILURE);
}
return 1;
}
Chunk *directory_scanner_next(DirectoryScanner *scanner) { Chunk *directory_scanner_next(DirectoryScanner *scanner) {
ArrayList *chunk_data = array_list_create(file_destroy); ArrayList *chunk_data = array_list_create(file_destroy);
unsigned long long chunk_data_size = 0; unsigned long long chunk_data_size = 0;
while (!queue_is_empty(scanner->directories)) { while (1) {
char *path = (char *)queue_dequeue(scanner->directories); if (scanner->current_dir == NULL) {
DIR *dir; if (!open_next_directory(scanner))
struct dirent *entry; break;
dir = opendir(path);
if (dir == NULL) {
perror("Could not open directory!");
exit(EXIT_FAILURE);
} }
while ((entry = readdir(dir)) != NULL) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { struct dirent *entry = readdir(scanner->current_dir);
continue; if (entry == NULL) {
} closedir(scanner->current_dir);
char *cur_path = path_cat(path, entry->d_name); scanner->current_dir = NULL;
struct stat stats; free(scanner->current_path);
stat(cur_path, &stats); scanner->current_path = NULL;
if (!S_ISREG(stats.st_mode)) continue;
queue_enqueue(scanner->directories, (void *)cur_path); }
else {
File *file = file_create(cur_path, &stats); if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
array_list_add(chunk_data, file); continue;
chunk_data_size += file->stats.st_size;
if (chunk_data_size > DESIRED_CHUNK_SIZE) char *cur_path = path_cat(scanner->current_path, entry->d_name);
return chunk_data_to_chunk(chunk_data); struct stat stats;
free(cur_path); if (stat(cur_path, &stats) != 0) {
} free(cur_path);
continue;
}
if (!S_ISREG(stats.st_mode)) {
queue_enqueue(scanner->directories, (void *)cur_path);
} else {
File *file = file_create(cur_path, &stats);
array_list_add(chunk_data, file);
chunk_data_size += file->stats.st_size;
if (chunk_data_size > DESIRED_CHUNK_SIZE)
return chunk_data_to_chunk(chunk_data);
free(cur_path);
} }
closedir(dir);
free(path);
} }
if (chunk_data->size > 0) if (chunk_data->size > 0)
return chunk_data_to_chunk(chunk_data); return chunk_data_to_chunk(chunk_data);
return NULL; return NULL;
+4
View File
@@ -3,8 +3,12 @@
#include "chunk.h" #include "chunk.h"
#include "queue.h" #include "queue.h"
#include <dirent.h>
typedef struct { typedef struct {
Queue *directories; Queue *directories;
DIR *current_dir;
char *current_path;
} DirectoryScanner; } DirectoryScanner;
DirectoryScanner *directory_scanner_create(char *root_directory); DirectoryScanner *directory_scanner_create(char *root_directory);
+197 -124
View File
@@ -1,27 +1,31 @@
import argparse
import filecmp
import os import os
import shutil
import subprocess import subprocess
import sys
import tempfile
import time 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"] SERVER_CMD = ["./build/server"]
base_client_cmd = ["./build/client"] base_client_cmd = ["./build/client"]
# --- Resource Limit Configuration --- DISK_DEVICE = "/dev/nvme0n1p5"
# 💾 Disk throttling settings READ_BPS_MAX = "15M"
DISK_DEVICE = ( WRITE_BPS_MAX = "10M"
"/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
# 🐢 Network throttling settings (Linux tc)
NET_LIMIT = "100mbit" NET_LIMIT = "100mbit"
NET_DELAY = "100ms" NET_DELAY = "100ms"
NETWORK_INTERFACE = "lo" 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() NET_RESET_CMD = f"sudo tc qdisc del dev {NETWORK_INTERFACE} root".split()
# --- Build the client command prefix with throttling ---
CLIENT_CMD_PREFIX = [ CLIENT_CMD_PREFIX = [
"sudo", "sudo",
"systemd-run", "systemd-run",
@@ -32,11 +36,10 @@ CLIENT_CMD_PREFIX = [
f"IOWriteBandwidthMax={DISK_DEVICE} {WRITE_BPS_MAX}", f"IOWriteBandwidthMax={DISK_DEVICE} {WRITE_BPS_MAX}",
] ]
# --- Test Cases ---
TEST_CASES = [ TEST_CASES = [
{"name": "Standard (Single-threaded)", "flags": []}, {"name": "Standard (Single-threaded)", "flags": []},
{"name": "Multithreading (-m)", "flags": ["-m"]}, {"name": "Multithreading (-m)", "flags": ["-m"]},
{"name": "Compression (-c 0)", "flags": ["-c 0"]}, {"name": "Compression (-c)", "flags": ["-c"]},
{"name": "Chunk Serialization (-s)", "flags": ["-s"]}, {"name": "Chunk Serialization (-s)", "flags": ["-s"]},
{"name": "Compression + Chunk Serialization (-c -s)", "flags": ["-c", "-s"]}, {"name": "Compression + Chunk Serialization (-c -s)", "flags": ["-c", "-s"]},
{"name": "Multithreading + Compression (-m -c)", "flags": ["-m", "-c"]}, {"name": "Multithreading + Compression (-m -c)", "flags": ["-m", "-c"]},
@@ -48,179 +51,249 @@ 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)
target_total = 50 * 1024 * 1024
written = 0
files = {
"small.txt": b"hello world\n",
"medium.txt": b"the quick brown fox jumps over the lazy dog\n" * 5000,
"binary.bin": bytes(range(256)) * 1000,
"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)
written += len(content)
i = 0
while written < target_total:
chunk_size = min(5 * 1024 * 1024, target_total - written)
rel_path = f"bulk/file_{i}.dat"
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(b"0" * chunk_size)
written += chunk_size
i += 1
total_mb = written / (1024 * 1024)
print(f" Generated {total_mb:.1f} MB of test data in {source_dir}")
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 = [] results = []
print(f"\n{'=' * 60}") print(f"\n{'=' * 60}")
print(f"🚀 Starting Suite: {env_name}") print(f"Suite: {env_name}")
print(f"{'=' * 60}") print(f"{'=' * 60}")
if apply_limits: if apply_limits:
print( print(f" Disk I/O: Reads <= {READ_BPS_MAX}, Writes <= {WRITE_BPS_MAX}")
f"Applying Disk I/O Limits: Reads <= {READ_BPS_MAX}, Writes <= {WRITE_BPS_MAX}" print(f" Network: {NET_LIMIT}, {NET_DELAY} delay")
)
print(f"Applying Network Limits: {NET_LIMIT}, {NET_DELAY} delay")
client_prefix = CLIENT_CMD_PREFIX client_prefix = CLIENT_CMD_PREFIX
else: else:
print("Running Baseline (No limits applied)") print(" Baseline (no limits)")
client_prefix = [] # Run normally without systemd-run/limits client_prefix = []
try: try:
# SETUP: Apply or ensure clean network limits
if apply_limits: if apply_limits:
subprocess.run(NET_LIMIT_CMD, check=True) subprocess.run(NET_LIMIT_CMD, check=True)
else: else:
# Silently attempt to clear any leftover rules just to ensure a clean baseline
subprocess.run(NET_RESET_CMD, capture_output=True) subprocess.run(NET_RESET_CMD, capture_output=True)
for case in TEST_CASES: for case in TEST_CASES:
name = case["name"] name = case["name"]
flags = case["flags"] 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 server_process = None
try: try:
# 1. Start the server
print(" Starting server...")
server_process = subprocess.Popen( server_process = subprocess.Popen(
SERVER_CMD, stdout=subprocess.DEVNULL, stderr=None SERVER_CMD, stdout=subprocess.DEVNULL, stderr=None
) )
time.sleep(0.5) # Allow server to bind to port time.sleep(0.5)
# 2. Build and run the client env = os.environ.copy()
client_cmd = client_prefix + base_client_cmd + flags
print(f" Running client: {' '.join(client_cmd)}") client_cmd = (
client_prefix
+ base_client_cmd
+ ["--source-dir", source_dir, "--dest-dir", dest_dir, "--save-to-disk"]
+ flags
)
print(f" Running: {' '.join(client_cmd)}")
start_time = time.monotonic() start_time = time.monotonic()
client_result = subprocess.run( client_result = subprocess.run(
client_cmd, text=True, capture_output=True client_cmd, env=env, text=True, capture_output=True
) )
end_time = time.monotonic() end_time = time.monotonic()
duration = end_time - start_time 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: if client_result.returncode == 0:
results.append( mismatches, missing = verify_transfer(source_dir, dest_dir)
{
"environment": env_name, entry = {
"name": name, "name": name,
"status": "Success", "suite": env_name,
"time": f"{duration:.4f}s", "time": f"{duration:.4f}s" if client_result.returncode == 0 else "N/A",
"error": "", }
}
) if client_result.returncode == 0 and not mismatches and not missing:
entry["status"] = "Success"
entry["error"] = ""
else: else:
print(f" ⚠️ Failed (code: {client_result.returncode})") entry["status"] = "Failed"
err_msg = ( errors = []
client_result.stderr.strip().split("\n")[0] if client_result.returncode != 0:
if client_result.stderr err = (
else ( client_result.stderr.strip().split("\n")[0]
client_result.stdout.strip().split("\n")[0] if client_result.stderr
if client_result.stdout else (
else "No output" client_result.stdout.strip().split("\n")[0]
if client_result.stdout
else "No output"
)
) )
) errors.append(f"Exit code {client_result.returncode}: {err[:80]}")
results.append( if missing:
{ errors.append(f"Missing ({len(missing)}): {', '.join(missing[:5])}")
"environment": env_name, if mismatches:
"name": name, errors.append(f"Mismatch ({len(mismatches)}): {', '.join(mismatches[:3])}")
"status": "Failed", entry["error"] = " | ".join(errors)
"time": "N/A",
"error": f"Exit code {client_result.returncode}: {err_msg[:40]}", results.append(entry)
}
)
except subprocess.TimeoutExpired: except subprocess.TimeoutExpired:
print(" ⚠️ Timeout (exceeded 15s)")
results.append( results.append(
{ {"name": name, "suite": env_name, "status": "Timeout", "time": "N/A", "error": "Exceeded 15s"}
"environment": env_name,
"name": name,
"status": "Timeout",
"time": "N/A",
"error": "Exceeded 15 seconds",
}
) )
except Exception as e: except Exception as e:
print(f" ❌ Error: {e}")
results.append( results.append(
{ {"name": name, "suite": env_name, "status": "Error", "time": "N/A", "error": str(e)}
"environment": env_name,
"name": name,
"status": "Error",
"time": "N/A",
"error": str(e),
}
) )
finally: finally:
# Clean up the server for this test case
if server_process: if server_process:
print(" Stopping server...")
try: try:
server_process.terminate()
server_process.wait(timeout=5) server_process.wait(timeout=5)
except subprocess.TimeoutExpired: except subprocess.TimeoutExpired:
server_process.kill() server_process.kill()
server_process.wait() server_process.wait()
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
print(f" Error running system limit command: {' '.join(e.cmd)}") print(f" Error running limit command: {' '.join(e.cmd)}")
print("Are you running this script with 'sudo' privileges?")
finally: finally:
# TEARDOWN: Remove network limits if they were applied
if apply_limits: if apply_limits:
print("\nCleaning up limits for this suite...")
try: try:
subprocess.run(NET_RESET_CMD, check=True, capture_output=True) subprocess.run(NET_RESET_CMD, check=True, capture_output=True)
print("Network limits removed.") except Exception:
except Exception as e: pass
print(f"⚠️ Could not reset network settings: {e}")
return results return results
os.system("cmake -B build -S .") def main():
os.system("cd build && make") 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 --- os.system("cmake -B build -S . > /dev/null 2>&1")
all_results = [] 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) generate_test_files(args.source_dir)
all_results.extend(run_suite("Unlimited", apply_limits=False)) os.makedirs(args.dest_dir, exist_ok=True)
# # 2. Run Throttled (With Limits) try:
all_results.extend(run_suite("Throttled", apply_limits=True)) all_results = []
# --- Print Comparison Table --- if not args.no_unlimited:
print("\n" + "=" * 105) all_results.extend(
print(f"{'fastSync BENCHMARK RESULTS (COMPARISON)':^105}") run_suite("Unlimited", False, args.source_dir, args.dest_dir)
print("=" * 105) )
print(
f"{'Configuration':<45} | {'Environment':<12} | {'Status':<10} | {'Time':<10} | {'Details/Error':<20}"
)
print("-" * 105)
# Sort results by test case name first, then environment to easily compare if not args.no_throttled:
# This groups the baseline and throttled results for the same test next to each other all_results.extend(
# sorted_results = sorted( run_suite("Throttled", True, args.source_dir, args.dest_dir)
# all_results, )
# key=lambda x: (
# TEST_CASES.index(
# next(item for item in TEST_CASES if item["name"] == x["name"])
# ),
# x["environment"],
# ),
# )
for res in all_results: print("\n" + "=" * 110)
status_symbol = ( print(f"{'RESULTS':^110}")
"" print("=" * 110)
if res["status"] == "Success" print(f"{'Configuration':<45} | {'Suite':<12} | {'Status':<8} | {'Time':<10} | {'Details'}")
else ("" if res["status"] == "Timeout" else "") print("-" * 110)
)
status_str = f"{status_symbol} {res['status']}" for res in all_results:
print( print(
f"{res['name']:<45} | {res['environment']:<12} | {status_str:<10} | {res['time']:<10} | {res['error']:<20}" f"{res['name']:<45} | {res['suite']:<12} | {res['status']:<8} | {res['time']:<10} | {res['error']}"
) )
print("=" * 105)
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()