#!/bin/bash

# 设置测试目录
TEST_DIR="/var/lib/docker/"
# 设置大文件和小文件的大小
LARGE_FILE_SIZE=3G  # 1GB
SMALL_FILE_SIZE=10M  # 1MB
LARGE_FILE="${TEST_DIR}/large_test_file"
SMALL_FILE="${TEST_DIR}/small_test_file"

# 创建测试目录，如果不存在
mkdir -p "$TEST_DIR"

# 测试大文件读写速度
echo "开始大文件读写测试..."

# 写入大文件并测量时间
start_time=$(date +%s.%N)
dd if=/dev/zero of="$LARGE_FILE" bs=1M count=1024 conv=fdatasync
end_time=$(date +%s.%N)
write_time=$(echo "$end_time - $start_time" | bc)
write_speed=$(echo "scale=2; 1024 / $write_time" | bc)

echo "大文件写入速度: ${write_speed} MB/s"

# 读取大文件并测量时间
start_time=$(date +%s.%N)
dd if="$LARGE_FILE" of=/dev/null bs=1M
end_time=$(date +%s.%N)
read_time=$(echo "$end_time - $start_time" | bc)
read_speed=$(echo "scale=2; 1024 / $read_time" | bc)

echo "大文件读取速度: ${read_speed} MB/s"

# 清理大文件
rm -f "$LARGE_FILE"

# 测试小文件读写速度
echo "开始小文件读写测试..."

# 写入小文件并测量时间
start_time=$(date +%s.%N)
for i in $(seq 1 3); do
    dd if=/dev/zero of="${TEST_DIR}/small_test_file_$i" bs=1M count=1 conv=fdatasync
done
end_time=$(date +%s.%N)
write_time=$(echo "$end_time - $start_time" | bc)
write_speed=$(echo "scale=2; 1024 / $write_time" | bc)

echo "小文件写入速度: ${write_speed} MB/s"

# 读取小文件并测量时间
start_time=$(date +%s.%N)
for i in $(seq 1 3); do
    dd if="${TEST_DIR}/small_test_file_$i" of=/dev/null bs=1M
done
end_time=$(date +%s.%N)
read_time=$(echo "$end_time - $start_time" | bc)
read_speed=$(echo "scale=2; 1024 / $read_time" | bc)

echo "小文件读取速度: ${read_speed} MB/s"

# 清理小文件
rm -f "${TEST_DIR}/small_test_file_"*

echo "测试完成!"
