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

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$ROOT_DIR"

PROFILE="${1:-full}"

case "$PROFILE" in
  quick)
    export TSINK_BPP_RUNS="${TSINK_BPP_RUNS:-1}"
    export TSINK_ACTIVE_SERIES="${TSINK_ACTIVE_SERIES:-1000}"
    export TSINK_PRIME_ALL_SERIES="${TSINK_PRIME_ALL_SERIES:-1}"
    export TSINK_WARMUP_POINTS="${TSINK_WARMUP_POINTS:-50000}"
    export TSINK_MEASURE_POINTS="${TSINK_MEASURE_POINTS:-200000}"
    export TSINK_BATCH_SIZE="${TSINK_BATCH_SIZE:-2048}"
    export TSINK_MIN_POINTS_PER_SERIES="${TSINK_MIN_POINTS_PER_SERIES:-4096}"
    ;;
  full)
    export TSINK_BPP_RUNS="${TSINK_BPP_RUNS:-3}"
    export TSINK_ACTIVE_SERIES="${TSINK_ACTIVE_SERIES:-10000}"
    export TSINK_PRIME_ALL_SERIES="${TSINK_PRIME_ALL_SERIES:-1}"
    export TSINK_WARMUP_POINTS="${TSINK_WARMUP_POINTS:-250000}"
    export TSINK_MEASURE_POINTS="${TSINK_MEASURE_POINTS:-1000000}"
    export TSINK_BATCH_SIZE="${TSINK_BATCH_SIZE:-4096}"
    export TSINK_MIN_POINTS_PER_SERIES="${TSINK_MIN_POINTS_PER_SERIES:-4096}"
    ;;
  mixed-order)
    export TSINK_BPP_RUNS="${TSINK_BPP_RUNS:-1}"
    export TSINK_ACTIVE_SERIES="${TSINK_ACTIVE_SERIES:-2048}"
    export TSINK_PRIME_ALL_SERIES="${TSINK_PRIME_ALL_SERIES:-1}"
    export TSINK_WARMUP_POINTS="${TSINK_WARMUP_POINTS:-100000}"
    export TSINK_MEASURE_POINTS="${TSINK_MEASURE_POINTS:-400000}"
    export TSINK_BATCH_SIZE="${TSINK_BATCH_SIZE:-2048}"
    export TSINK_MIN_POINTS_PER_SERIES="${TSINK_MIN_POINTS_PER_SERIES:-192}"
    export TSINK_MAX_ACTIVE_PARTITION_HEADS_PER_SERIES="${TSINK_MAX_ACTIVE_PARTITION_HEADS_PER_SERIES:-4}"
    export TSINK_OOO_CROSS_PARTITION_PERMILLE="${TSINK_OOO_CROSS_PARTITION_PERMILLE:-150}"
    export TSINK_OOO_CROSS_PARTITIONS="${TSINK_OOO_CROSS_PARTITIONS:-4}"
    export TSINK_PARTITION_SECONDS="${TSINK_PARTITION_SECONDS:-600}"
    ;;
  *)
    echo "Unknown profile: $PROFILE (expected: quick|full|mixed-order)" >&2
    exit 1
    ;;
esac

export TSINK_OOO_MAX_SECONDS="${TSINK_OOO_MAX_SECONDS:-300}"
export TSINK_OOO_PERMILLE="${TSINK_OOO_PERMILLE:-250}"
export TSINK_OOO_CROSS_PARTITION_PERMILLE="${TSINK_OOO_CROSS_PARTITION_PERMILLE:-0}"
export TSINK_OOO_CROSS_PARTITIONS="${TSINK_OOO_CROSS_PARTITIONS:-0}"
export TSINK_SPARSE_EMIT_PERMILLE="${TSINK_SPARSE_EMIT_PERMILLE:-120}"
export TSINK_SHORT_LIVED_LIFETIME_STEPS="${TSINK_SHORT_LIVED_LIFETIME_STEPS:-512}"
export TSINK_RETENTION_SECONDS="${TSINK_RETENTION_SECONDS:-604800}"
export TSINK_PARTITION_SECONDS="${TSINK_PARTITION_SECONDS:-86400}"
export TSINK_STEP_SECONDS="${TSINK_STEP_SECONDS:-10}"
export TSINK_SETTLE_MILLIS="${TSINK_SETTLE_MILLIS:-1500}"
export TSINK_FAIL_ON_TARGET="${TSINK_FAIL_ON_TARGET:-0}"
export TSINK_MAX_ACTIVE_PARTITION_HEADS_PER_SERIES="${TSINK_MAX_ACTIVE_PARTITION_HEADS_PER_SERIES:-8}"

prime_points=0
case "${TSINK_PRIME_ALL_SERIES,,}" in
  1|true|yes|y) prime_points="$TSINK_ACTIVE_SERIES" ;;
esac

target_total_points=$((TSINK_ACTIVE_SERIES * TSINK_MIN_POINTS_PER_SERIES))
required_ingest_points=$((target_total_points - prime_points))
if (( required_ingest_points < 0 )); then
  required_ingest_points=0
fi

configured_ingest_points=$((TSINK_WARMUP_POINTS + TSINK_MEASURE_POINTS))
if (( configured_ingest_points < required_ingest_points )); then
  TSINK_WARMUP_POINTS=$((required_ingest_points * 3 / 10))
  TSINK_MEASURE_POINTS=$((required_ingest_points - TSINK_WARMUP_POINTS))
fi

cat <<CONFIG
Running workload benchmark with profile=$PROFILE
  TSINK_BPP_RUNS=$TSINK_BPP_RUNS
  TSINK_ACTIVE_SERIES=$TSINK_ACTIVE_SERIES
  TSINK_PRIME_ALL_SERIES=$TSINK_PRIME_ALL_SERIES
  TSINK_MIN_POINTS_PER_SERIES=$TSINK_MIN_POINTS_PER_SERIES
  TSINK_WARMUP_POINTS=$TSINK_WARMUP_POINTS
  TSINK_MEASURE_POINTS=$TSINK_MEASURE_POINTS
  TSINK_BATCH_SIZE=$TSINK_BATCH_SIZE
  TSINK_MAX_ACTIVE_PARTITION_HEADS_PER_SERIES=$TSINK_MAX_ACTIVE_PARTITION_HEADS_PER_SERIES
  TSINK_OOO_MAX_SECONDS=$TSINK_OOO_MAX_SECONDS
  TSINK_OOO_CROSS_PARTITION_PERMILLE=$TSINK_OOO_CROSS_PARTITION_PERMILLE
  TSINK_OOO_CROSS_PARTITIONS=$TSINK_OOO_CROSS_PARTITIONS
  TSINK_SHORT_LIVED_LIFETIME_STEPS=$TSINK_SHORT_LIVED_LIFETIME_STEPS
  TSINK_RETENTION_SECONDS=$TSINK_RETENTION_SECONDS
  TSINK_PARTITION_SECONDS=$TSINK_PARTITION_SECONDS
CONFIG

cargo bench --bench workload -- --nocapture
