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

IMAGE="lomelidev/llama.cpp:cu130-nccl-sse-faall-10974"
MODEL_DIR="/home/lab/models/ornith15-9b"

PER_SLOT=262144

RUN="$HOME/bench/ornith/concurrency-256k-$(date -u +%Y%m%dT%H%M%SZ)"

mkdir -p "$RUN"/{responses,requests,logs,gpu}

echo "$RUN" > "$HOME/bench/ornith/LAST_MATRIX"

SUMMARY="$RUN/summary.tsv"

printf "case\tslots\tconcurrent\tctx_total\tctx_slot\tVRAM_MiB\tFREE_MiB\tavg_req_tps\tagg_tps\twall_s\treq1_tps\treq2_tps\treq3_tps\tFA_warn\tcapping\tstatus\n" \
  > "$SUMMARY"


make_requests() {

cat > "$RUN/requests/1.json" <<'JSON'
{
  "model": "ornith-bench",
  "messages": [
    {
      "role": "user",
      "content": "Implement a production-ready concurrent worker pool in Go with bounded concurrency, context cancellation, panic recovery, graceful shutdown and backpressure. Explain the main design decisions."
    }
  ],
  "temperature": 0.6,
  "top_p": 0.95,
  "top_k": 20,
  "seed": 111111,
  "max_tokens": 1024,
  "stream": false
}
JSON

cat > "$RUN/requests/2.json" <<'JSON'
{
  "model": "ornith-bench",
  "messages": [
    {
      "role": "user",
      "content": "Design a production Go message pipeline with deduplication, bounded retries, exponential backoff, per-job deadlines and graceful shutdown. Provide complete code and explain the architecture."
    }
  ],
  "temperature": 0.6,
  "top_p": 0.95,
  "top_k": 20,
  "seed": 222222,
  "max_tokens": 1024,
  "stream": false
}
JSON

cat > "$RUN/requests/3.json" <<'JSON'
{
  "model": "ornith-bench",
  "messages": [
    {
      "role": "user",
      "content": "Debug and rewrite a Go HTTP service whose goroutines occasionally leak during shutdown. Use contexts, errgroup, channel ownership and bounded queues correctly. Include production-quality code and reasoning."
    }
  ],
  "temperature": 0.6,
  "top_p": 0.95,
  "top_k": 20,
  "seed": 333333,
  "max_tokens": 1024,
  "stream": false
}
JSON

}


run_case() {

    CASE="$1"
    NP="$2"
    CONCURRENT="$3"

    CTX_TOTAL=$(( PER_SLOT * NP ))

    echo
    echo "============================================================"
    echo " CASE: $CASE"
    echo " slots=$NP"
    echo " concurrent=$CONCURRENT"
    echo " total_ctx=$CTX_TOTAL"
    echo " target_ctx_per_slot=$PER_SLOT"
    echo "============================================================"

    docker rm -f ornith >/dev/null 2>&1 || true


    docker run -d \
      --name ornith \
      --gpus "device=0" \
      --ipc=host \
      -p 8120:8080 \
      -v "$MODEL_DIR:/models:ro" \
      -e GGML_CUDA_GRAPH_OPT=1 \
      "$IMAGE" \
      -m /models/Ornith-1.5-9B-Q4_K_L.gguf \
      -a ornith-bench \
      -ngl 999 \
      -fa on \
      -ctk q4_0 \
      -ctv q4_0 \
      -c "$CTX_TOTAL" \
      -np "$NP" \
      --no-kv-unified \
      --cont-batching \
      --no-mmproj \
      -b 4096 \
      -ub 2048 \
      --cache-ram 32768 \
      --ctx-checkpoints 4 \
      --cache-prompt \
      --jinja \
      --metrics \
      --host 0.0.0.0 \
      --port 8080 \
      --temp 0.6 \
      --top-p 0.95 \
      --top-k 20 \
      --min-p 0 \
      > "$RUN/logs/$CASE.container-id.txt"


    READY=0

    for i in $(seq 1 180); do

        if curl -fsS http://127.0.0.1:8120/health >/dev/null 2>&1; then
            READY=1
            break
        fi

        if ! docker ps --format '{{.Names}}' | grep -qx ornith; then
            break
        fi

        sleep 1
    done


    docker logs ornith > "$RUN/logs/$CASE.start.log" 2>&1 || true


    if [[ "$READY" != "1" ]]; then

        echo "ERROR: $CASE no logró cargar"

        docker logs ornith 2>&1 | tail -100 || true

        printf "%s\t%s\t%s\t%s\tFAIL\tFAIL\tFAIL\t0\t0\t0\t0\t0\t0\t0\t0\tLOAD_FAIL\n" \
          "$CASE" "$NP" "$CONCURRENT" "$CTX_TOTAL" \
          >> "$SUMMARY"

        return
    fi


    SLOT_CTX=$(
      grep -oE 'n_ctx_slot = [0-9]+' "$RUN/logs/$CASE.start.log" \
        | tail -1 \
        | awk '{print $3}'
    )

    SLOT_CTX="${SLOT_CTX:-0}"


    CAPPING=$(
      grep -ci "capping" "$RUN/logs/$CASE.start.log" || true
    )


    if [[ "$SLOT_CTX" != "$PER_SLOT" ]]; then

        echo "ERROR: contexto por slot incorrecto"
        echo "esperado=$PER_SLOT real=$SLOT_CTX"

        printf "%s\t%s\t%s\t%s\t%s\tFAIL\tFAIL\t0\t0\t0\t0\t0\t0\t0\t%s\tBAD_CTX\n" \
          "$CASE" "$NP" "$CONCURRENT" "$CTX_TOTAL" "$SLOT_CTX" "$CAPPING" \
          >> "$SUMMARY"

        return
    fi


    GPU_MEM=$(nvidia-smi \
      --query-gpu=memory.used,memory.free \
      --format=csv,noheader,nounits \
      -i 0)

    VRAM=$(echo "$GPU_MEM" | awk -F',' '{gsub(/ /,"",$1); print $1}')
    FREE=$(echo "$GPU_MEM" | awk -F',' '{gsub(/ /,"",$2); print $2}')


    nvidia-smi \
      --query-gpu=index,memory.used,memory.free,utilization.gpu,power.draw,temperature.gpu \
      --format=csv \
      > "$RUN/gpu/$CASE.before.txt"


    echo
    echo "Lanzando $CONCURRENT requests SIMULTÁNEOS..."


    START_NS=$(date +%s%N)

    PIDS=()

    for I in $(seq 1 "$CONCURRENT"); do

        curl -sS \
          --max-time 300 \
          http://127.0.0.1:8120/v1/chat/completions \
          -H 'Content-Type: application/json' \
          --data-binary @"$RUN/requests/$I.json" \
          > "$RUN/responses/$CASE-req$I.json" &

        PIDS+=("$!")

    done


    CURL_OK=1

    for PID in "${PIDS[@]}"; do

        if ! wait "$PID"; then
            CURL_OK=0
        fi

    done


    END_NS=$(date +%s%N)

    WALL_NS=$(( END_NS - START_NS ))

    WALL_S=$(awk -v n="$WALL_NS" 'BEGIN {printf "%.3f", n/1000000000}')


    docker logs ornith > "$RUN/logs/$CASE.final.log" 2>&1 || true


    FA_WARN=$(
      grep -c "no FlashAttention vector kernel" \
        "$RUN/logs/$CASE.final.log" || true
    )

    CAPPING=$(
      grep -ci "capping" \
        "$RUN/logs/$CASE.final.log" || true
    )


    TPS1=0
    TPS2=0
    TPS3=0

    SUM_TPS=0
    SUM_TOKENS=0
    VALID=0


    for I in $(seq 1 "$CONCURRENT"); do

        FILE="$RUN/responses/$CASE-req$I.json"

        if jq -e '.timings.predicted_per_second' "$FILE" >/dev/null 2>&1; then

            TPS=$(
              jq -r '.timings.predicted_per_second // 0' "$FILE"
            )

            TOKENS=$(
              jq -r '.usage.completion_tokens // 0' "$FILE"
            )

            case "$I" in
              1) TPS1="$TPS" ;;
              2) TPS2="$TPS" ;;
              3) TPS3="$TPS" ;;
            esac

            SUM_TPS=$(awk -v a="$SUM_TPS" -v b="$TPS" \
              'BEGIN {print a+b}')

            SUM_TOKENS=$(( SUM_TOKENS + TOKENS ))

            VALID=$(( VALID + 1 ))

        fi

    done


    if [[ "$VALID" -gt 0 ]]; then

        AVG_TPS=$(awk -v s="$SUM_TPS" -v n="$VALID" \
          'BEGIN {printf "%.3f", s/n}')

        AGG_TPS=$(awk -v t="$SUM_TOKENS" -v s="$WALL_S" \
          'BEGIN {printf "%.3f", t/s}')

    else

        AVG_TPS=0
        AGG_TPS=0

    fi


    if [[ "$CURL_OK" == "1" && "$VALID" == "$CONCURRENT" ]]; then
        STATUS="OK"
    else
        STATUS="REQUEST_FAIL"
    fi


    nvidia-smi \
      --query-gpu=index,memory.used,memory.free,utilization.gpu,power.draw,temperature.gpu \
      --format=csv \
      > "$RUN/gpu/$CASE.after.txt"


    printf "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" \
      "$CASE" \
      "$NP" \
      "$CONCURRENT" \
      "$CTX_TOTAL" \
      "$SLOT_CTX" \
      "$VRAM" \
      "$FREE" \
      "$AVG_TPS" \
      "$AGG_TPS" \
      "$WALL_S" \
      "$TPS1" \
      "$TPS2" \
      "$TPS3" \
      "$FA_WARN" \
      "$CAPPING" \
      "$STATUS" \
      >> "$SUMMARY"


    echo
    echo "--- RESULTADO $CASE ---"
    echo "Slots:           $NP"
    echo "Concurrentes:    $CONCURRENT"
    echo "Ctx/slot:        $SLOT_CTX"
    echo "VRAM:            $VRAM MiB"
    echo "Libre:           $FREE MiB"
    echo
    echo "Req1:            $TPS1 t/s"
    echo "Req2:            $TPS2 t/s"
    echo "Req3:            $TPS3 t/s"
    echo
    echo "Promedio/request $AVG_TPS t/s"
    echo "Throughput total $AGG_TPS t/s"
    echo "Wall batch:      $WALL_S s"
    echo "Status:          $STATUS"

    echo
    column -t -s $'\t' "$SUMMARY"

}


make_requests


# ============================================================
# OPCIÓN A — 2 slots × 256K
# ============================================================

run_case A2-one  2 1
run_case A2-two  2 2


# ============================================================
# OPCIÓN B — 3 slots × 256K
# ============================================================

run_case B3-one    3 1
run_case B3-two    3 2
run_case B3-three  3 3


echo
echo "============================================================"
echo " BENCHMARK CONCURRENCIA TERMINADO"
echo "============================================================"
echo

column -t -s $'\t' "$SUMMARY"

echo
echo "Resultados:"
echo "$RUN"
