# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
apiVersion: v1
kind: Pod
metadata:
  name: qwen3-vl-agg-benchmark
  labels:
    app: benchmark
spec:
  containers:
    - name: benchmark
      image: python:3.11
      command:
        - /bin/bash
        - -lc
        - |
          set -euo pipefail
          ulimit -n 1048576
          ulimit -u 65536

          apt update && apt install -y tmux curl jq
          pip install aiperf

          echo "Waiting for model '${MODEL_NAME}' at http://${FRONTEND}:8000/v1/models..."
          until curl -s "http://${FRONTEND}:8000/v1/models" | jq -e --arg model "${MODEL_NAME}" '.data[]? | select(.id == $model)' >/dev/null 2>&1; do
            echo "[$(date '+%H:%M:%S')] Model not ready, retrying in 5s..."
            sleep 5
          done
          echo "Model '${MODEL_NAME}' is ready."

          INPUT_FILE="${DATASET_DIR}/qwen3_vl_1000req_1img_pool200.jsonl"
          if [ ! -f "${INPUT_FILE}" ]; then
            echo "Dataset not found: ${INPUT_FILE}"
            exit 1
          fi

          RUN_DIR="${ARTIFACT_BASE_DIR}/${CACHE_MODE}"
          mkdir -p "${RUN_DIR}"

          echo "Running benchmark ..."
          aiperf profile \
            --model "${MODEL_NAME}" \
            --input-file "${INPUT_FILE}" \
            --custom-dataset-type single_turn \
            --url "http://${FRONTEND}:8000" \
            --streaming \
            --ui-type none \
            --request-count "${REQUEST_COUNT}" \
            --concurrency "${CONCURRENCY}" \
            --request-rate-mode constant \
            --warmup-request-count "${WARMUP_REQUEST_COUNT}" \
            --artifact-dir "${RUN_DIR}" \
            --extra-inputs "max_tokens:${MAX_TOKENS}" \
            --extra-inputs "min_tokens:${MAX_TOKENS}" \
            --extra-inputs "ignore_eos:true"

          echo "Run complete. Artifacts in ${RUN_DIR}"
          sleep 3600
      env:
        - name: MODEL_NAME
          value: Qwen/Qwen3-VL-30B-A3B-Instruct-FP8
        - name: FRONTEND
          value: qwen3-vl-agg-frontend
        - name: CACHE_MODE
          value: cache_on
        - name: MAX_TOKENS
          value: "150"
        - name: REQUEST_COUNT
          value: "1000"
        - name: CONCURRENCY
          value: "64"
        - name: WARMUP_REQUEST_COUNT
          value: "3"
        - name: DATASET_DIR
          value: /perf-cache/datasets
        - name: ARTIFACT_BASE_DIR
          value: /perf-cache/artifacts/qwen3_vl_30b_embedding_cache/agg
      resources:
        requests:
          cpu: "8"
          memory: 16Gi
        limits:
          cpu: "16"
          memory: 32Gi
      volumeMounts:
        - name: perf-cache
          mountPath: /perf-cache
  volumes:
    - name: perf-cache
      persistentVolumeClaim:
        claimName: perf-cache
  restartPolicy: Never
