#!/bin/bash

# 获取脚本所在的绝对路径，确保后续引用都正确
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"

# 可执行文件和结果根目录都基于 SCRIPT_DIR
EXECUTABLE="${SCRIPT_DIR}/bin/speedtest"
RESULTS_ROOT="${SCRIPT_DIR}/results"

# 测试类型
names=('dyn_pol')  

# B 参数：-1, 0, 4, 8, …, 64
B_VALUES=( {0..40..4})

# 固定 E 和 T
E_VALUE=0
T_VALUE=1024

# seed 配置
BASE_SEED=137
NUM_SEEDS=10

# 确保结果根目录存在
mkdir -p "$RESULTS_ROOT"

for name in "${names[@]}"; do
  for b in "${B_VALUES[@]}"; do
    # 为当前 b 建目录
    outdir="${RESULTS_ROOT}/${name}/b${b}"
    mkdir -p "$outdir"

    echo "=== test=${name}, b=${b}, e=${E_VALUE}, t=${T_VALUE}: will run seeds ${BASE_SEED}..$((BASE_SEED + NUM_SEEDS - 1)) ==="

    for ((i=0; i<NUM_SEEDS; i++)); do
      seed=$((BASE_SEED + i))
      TEST_NAME="${name}_b${b}_e${E_VALUE}_t${T_VALUE}_s${seed}"
      LOG_FILE="${outdir}/${TEST_NAME}.out"
      MEM_FILE="${outdir}/${TEST_NAME}.mem"

      echo "  -> seed=${seed}"

      # 在子 shell 里切到 outdir，再执行命令，保证任何输出文件都在这里
      (
        cd "$outdir"
        /usr/bin/time -v "$EXECUTABLE" "${name}_tbg" "$b" "$E_VALUE" "$T_VALUE" "$seed"
      ) \
        1> "$LOG_FILE" \
        2> "$MEM_FILE"
    done

    echo
  done
done

echo "All tests completed. Results in ${RESULTS_ROOT}/"
