#!/usr/bin/env bash
set -euo pipefail

# =========================
# Manual Config (edit here)
# =========================
# 可执行文件路径
DB_BENCH_BIN="/home/lian/env/rocksdb-test/db_bench"
# RocksDB 数据目录
DB_PATH="/tmp/rocksdb_manual"

# 测试类型 sets:
# - "fillseq"
# - "fillrandom"
# - "readseq"
# - "readrandom"
# - "overwrite"
# - "fillrandom,readrandom"
BENCHMARKS="fillrandom,readrandom"

# key数
NUM=1000000
# 线程数
THREADS=1
# 随机种子
SEED=1

# key大小
KEY_SIZE=16
# value大小
VALUE_SIZE=400
# SST block大小
BLOCK_SIZE=4096

# block cache 大小
CACHE_SIZE=$((512 * 1024 * 1024))              # bytes
# memtable 大小
WRITE_BUFFER_SIZE=$((64 * 1024 * 1024))        # bytes
# memtable 个数
MAX_WRITE_BUFFER_NUMBER=4
# L1文件目标大小
TARGET_FILE_SIZE_BASE=$((64 * 1024 * 1024))    # bytes
# 可打开文件数
OPEN_FILES=-1

# 后台 并行度
MAX_BACKGROUND_JOBS=4
# 压缩算法
COMPRESSION_TYPE="none"
# 开启WAL
DISABLE_WAL=1
SYNC=0

# direct I/O
USE_DIRECT_READS=0
USE_DIRECT_IO_FOR_FLUSH_AND_COMPACTION=0

# mmap I/O
USE_MMAP_READS=0
USE_MMAP_WRITES=0

# 统计
STATISTICS=0
# 统计打印
STATS_INTERVAL_SECONDS=5
# 直方图
HISTOGRAM=0

# =========================
# Run
# =========================
if [[ ! -x "$DB_BENCH_BIN" ]]; then
    echo "db_bench not found or not executable: $DB_BENCH_BIN" >&2
    exit 1
fi

echo "== db_bench manual run =="
echo "DB_BENCH_BIN=$DB_BENCH_BIN"
echo "DB_PATH=$DB_PATH"
echo "BENCHMARKS=$BENCHMARKS"
echo "NUM=$NUM THREADS=$THREADS"
echo "KEY_SIZE=$KEY_SIZE VALUE_SIZE=$VALUE_SIZE BLOCK_SIZE=$BLOCK_SIZE"
echo "CACHE_SIZE=$CACHE_SIZE WRITE_BUFFER_SIZE=$WRITE_BUFFER_SIZE"
echo "MAX_WRITE_BUFFER_NUMBER=$MAX_WRITE_BUFFER_NUMBER TARGET_FILE_SIZE_BASE=$TARGET_FILE_SIZE_BASE"
echo "OPEN_FILES=$OPEN_FILES MAX_BACKGROUND_JOBS=$MAX_BACKGROUND_JOBS"
echo "COMPRESSION_TYPE=$COMPRESSION_TYPE DISABLE_WAL=$DISABLE_WAL SYNC=$SYNC"
echo "USE_DIRECT_READS=$USE_DIRECT_READS USE_DIRECT_IO_FOR_FLUSH_AND_COMPACTION=$USE_DIRECT_IO_FOR_FLUSH_AND_COMPACTION"
echo "USE_MMAP_READS=$USE_MMAP_READS USE_MMAP_WRITES=$USE_MMAP_WRITES"
echo "STATISTICS=$STATISTICS STATS_INTERVAL_SECONDS=$STATS_INTERVAL_SECONDS HISTOGRAM=$HISTOGRAM"
echo

exec "$DB_BENCH_BIN" \
  --db="$DB_PATH" \
  --benchmarks="$BENCHMARKS" \
  --num="$NUM" \
  --threads="$THREADS" \
  --seed="$SEED" \
  --key_size="$KEY_SIZE" \
  --value_size="$VALUE_SIZE" \
  --block_size="$BLOCK_SIZE" \
  --cache_size="$CACHE_SIZE" \
  --write_buffer_size="$WRITE_BUFFER_SIZE" \
  --max_write_buffer_number="$MAX_WRITE_BUFFER_NUMBER" \
  --target_file_size_base="$TARGET_FILE_SIZE_BASE" \
  --open_files="$OPEN_FILES" \
  --max_background_jobs="$MAX_BACKGROUND_JOBS" \
  --compression_type="$COMPRESSION_TYPE" \
  --disable_wal="$DISABLE_WAL" \
  --sync="$SYNC" \
  --use_direct_reads="$USE_DIRECT_READS" \
  --use_direct_io_for_flush_and_compaction="$USE_DIRECT_IO_FOR_FLUSH_AND_COMPACTION" \
  --mmap_read="$USE_MMAP_READS" \
  --mmap_write="$USE_MMAP_WRITES" \
  --statistics="$STATISTICS" \
  --stats_interval_seconds="$STATS_INTERVAL_SECONDS" \
  --histogram="$HISTOGRAM"
