#!/usr/bin/env bash # Test: high latency slows first read; cached second read is much faster # # Injects 500 ms of latency via tc netem, reads a 100 KB file (cold cache), # then removes the latency and reads the same file again (warm cache). # The second read should be significantly faster because it hits the VFS # cache and skips the network round-trip. set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" source "$SCRIPT_DIR/../harness/helpers.sh" source "$SCRIPT_DIR/../harness/mock-nas.sh" require_root setup_test_env trap teardown_test_env EXIT # --- Arrange --- start_mock_nas nas_create_file "file.dat" 100 # 100 KB gen_config start_warpgate wait_for_mount wait_for_rc_api # --- Act --- # Inject 500 ms latency on the host-side veth inject_latency 500 # Time the first read (cold cache, over high-latency link) time1_start=$(date +%s%N) cat "$TEST_MOUNT/file.dat" > /dev/null time1_end=$(date +%s%N) first_read_ns=$(( time1_end - time1_start )) # Remove latency injection clear_network_injection # Time the second read (should come from local VFS cache) time2_start=$(date +%s%N) cat "$TEST_MOUNT/file.dat" > /dev/null time2_end=$(date +%s%N) second_read_ns=$(( time2_end - time2_start )) # --- Assert --- first_read_ms=$(( first_read_ns / 1000000 )) second_read_ms=$(( second_read_ns / 1000000 )) echo " first read: ${first_read_ms} ms (cold cache, 500 ms netem delay)" echo " second read: ${second_read_ms} ms (warm cache, no injection)" # The cached read should be at least 5x faster than the latency-impacted read. # With 500 ms netem the first read is typically > 500 ms; a cached read is < 50 ms. if [[ "$first_read_ms" -gt 0 ]] && [[ $(( second_read_ms * 5 )) -lt "$first_read_ms" ]]; then echo " cache speedup confirmed (>5x)" else echo "FAIL: cached read was not significantly faster than the high-latency read" >&2 echo " first_read_ms=$first_read_ms second_read_ms=$second_read_ms" >&2 exit 1 fi echo "PASS: $(basename "$0" .sh)"