#!/bin/bash
# Run RAID 0 Performance Test Suite
# Expected: 28 GB/s read, 20 GB/s write, 2400K IOPS

set -e

echo "=== RAID 0 Performance Test Suite ==="
echo "Target: 4 NVMe disks in RAID 0"
echo "Stripe size: 64KB"
echo ""

# Check mount
if ! mountpoint -q /mnt/raid0_media; then
    echo "Error: /mnt/raid0_media is not mounted"
    echo "Run: mount /dev/md0 /mnt/raid0_media"
    exit 1
fi

# Test 1: Basic dd test (quick)
echo "=== Test 1: dd quick test ==="
echo "Write test (10GB):"
dd if=/dev/zero of=/mnt/raid0_media/test_dd.dat bs=1M count=10240 conv=fdatasync oflag=direct

echo ""
echo "Read test (10GB):"
dd if=/mnt/raid0_media/test_dd.dat of=/dev/null bs=1M count=10240 iflag=direct

echo ""

# Test 2: fio comprehensive test
echo "=== Test 2: fio comprehensive test ==="
fio /Users/accusys/markbase/scripts/raid0_performance_test.fio

echo ""

# Test 3: AJA System Test equivalent
echo "=== Test 3: AJA System Test equivalent ==="
echo "Simulating AJA ProRes 4444 4K test:"
echo "  Frame size: 4096 × 2160"
echo "  Frame rate: 60 fps"
echo "  Codec: ProRes 4444"
echo "  Bitrate: ~800 MB/s per stream"
echo ""

fio --name=aja_equivalent \
    --filename=/mnt/raid0_media/aja_frames.dat \
    --size=100G \
    --bs=256k \
    --rw=read \
    --direct=1 \
    --numjobs=4 \
    --iodepth=16 \
    --group_reporting

echo ""

# Test 4: Multiple stream test
echo "=== Test 4: Multiple concurrent streams ==="
echo "Testing 4 concurrent video streams (4 × 800 MB/s = 3200 MB/s target):"

# Create 4 test files
for i in {1..4}; do
    dd if=/dev/zero of=/mnt/raid0_media/stream_$i.dat bs=1M count=80000 &
done
wait

echo "Write complete, now testing concurrent read..."

# Read 4 streams simultaneously
for i in {1..4}; do
    dd if=/mnt/raid0_media/stream_$i.dat of=/dev/null bs=256k &
done
wait

echo ""

# Test 5: Bandwidth saturation test
echo "=== Test 5: Bandwidth saturation test ==="
echo "Finding maximum sustained bandwidth..."

fio --name=saturation_test \
    --filename=/mnt/raid0_media/saturation.dat \
    --size=200G \
    --bs=1M \
    --rw=read \
    --direct=1 \
    --numjobs=8 \
    --iodepth=64 \
    --group_reporting \
    --time_based \
    --runtime=60

echo ""

# Summary
echo "=== Test Summary ==="
echo ""
echo "Performance results saved to:"
echo "  /mnt/raid0_media/test_dd.dat"
echo "  /mnt/raid0_media/stream_*.dat"
echo ""

# Get disk stats
echo "Disk statistics:"
iostat -x /dev/md0 1 5

echo ""
echo "RAID status:"
mdadm --detail /dev/md0 | grep -E "(State|Active Devices|Working Devices)"

echo ""
echo "Expected vs Actual:"
echo "  Read: 28 GB/s (4 × 7000 MB/s)"
echo "  Write: 20 GB/s (4 × 5000 MB/s)"
echo "  IOPS: 2400K (4 × 600K)"
echo ""
echo "If results are lower than expected, check:"
echo "  1. NVMe PCIe bandwidth (PCIe 4.0 × 4 lanes per disk)"
echo "  2. CPU bottlenecks (check top)"
echo "  3. NUMA issues (check numactl --hardware)"
echo "  4. Kernel RAID vs userspace (mdadm vs custom implementation)"
echo ""

# Cleanup (optional)
echo "Cleanup test files? (y/n)"
read -r answer
if [ "$answer" = "y" ]; then
    rm -f /mnt/raid0_media/test_*.dat
    rm -f /mnt/raid0_media/stream_*.dat
    echo "✅ Test files cleaned"
fi