#!/bin/bash
# 性能基准测试脚本

set -e

echo "=== MarkBase Performance Benchmark ==="

USER_ID="${1:-demo}"
DEVICE="${2:-/dev/mapper/markbase_$USER_ID}"
TEST_SIZE="${3:-1G}"

echo "Configuration:"
echo "  User ID: $USER_ID"
echo "  Device: $DEVICE"
echo "  Test Size: $TEST_SIZE"

echo ""
echo "=== Test 1: Sequential Read ==="
fio --filename=$DEVICE \
    --direct=1 \
    --rw=read \
    --bs=4k \
    --size=$TEST_SIZE \
    --numjobs=1 \
    --iodepth=32 \
    --group_reporting \
    --name=seq_read_4k

echo ""
echo "=== Test 2: Sequential Write ==="
fio --filename=$DEVICE \
    --direct=1 \
    --rw=write \
    --bs=4k \
    --size=$TEST_SIZE \
    --numjobs=1 \
    --iodepth=32 \
    --group_reporting \
    --name=seq_write_4k

echo ""
echo "=== Test 3: Random Read ==="
fio --filename=$DEVICE \
    --direct=1 \
    --rw=randread \
    --bs=4k \
    --size=$TEST_SIZE \
    --numjobs=1 \
    --iodepth=32 \
    --group_reporting \
    --name=rand_read_4k

echo ""
echo "=== Test 4: Random Write ==="
fio --filename=$DEVICE \
    --direct=1 \
    --rw=randwrite \
    --bs=4k \
    --size=$TEST_SIZE \
    --numjobs=1 \
    --iodepth=32 \
    --group_reporting \
    --name=rand_write_4k

echo ""
echo "=== Test 5: Mixed Read/Write (70/30) ==="
fio --filename=$DEVICE \
    --direct=1 \
    --rw=randrw \
    --rwmixread=70 \
    --bs=4k \
    --size=$TEST_SIZE \
    --numjobs=1 \
    --iodepth=32 \
    --group_reporting \
    --name=mixed_rw_4k

echo ""
echo "=== Test 6: Large Block Sequential Read ==="
fio --filename=$DEVICE \
    --direct=1 \
    --rw=read \
    --bs=1M \
    --size=$TEST_SIZE \
    --numjobs=1 \
    --iodepth=32 \
    --group_reporting \
    --name=seq_read_1m

echo ""
echo "=== Test 7: Large Block Sequential Write ==="
fio --filename=$DEVICE \
    --direct=1 \
    --rw=write \
    --bs=1M \
    --size=$TEST_SIZE \
    --numjobs=1 \
    --iodepth=32 \
    --group_reporting \
    --name=seq_write_1m

echo ""
echo "=== Test 8: Concurrent Jobs (10 workers) ==="
fio --filename=$DEVICE \
    --direct=1 \
    --rw=randread \
    --bs=4k \
    --size=$TEST_SIZE \
    --numjobs=10 \
    --iodepth=32 \
    --group_reporting \
    --name=concurrent_10_jobs

echo ""
echo "=== Benchmark Complete ==="
echo "Results saved to: /tmp/fio_results/"
echo ""
echo "Summary:"
echo "  Sequential Read 4K: Check above output for bw="
echo "  Sequential Write 4K: Check above output for bw="
echo "  Random Read 4K: Check above output for iops="
echo "  Random Write 4K: Check above output for iops="
echo ""
echo "Expected results:"
echo "  RAID5 Sequential: ~1500 MB/s"
echo "  RAID5 Random: ~300000 iops"
echo "  iSCSI Sequential: ~1200 MB/s"
echo "  iSCSI Random: ~250000 iops"