name: Benchmark Metrics

on:
  workflow_dispatch:

jobs:
  benchmark:
    runs-on: self-hosted

    steps:
    - name: Checkout code
      uses: actions/checkout@v5

    - name: Setup Go (fast)
      run: |
        REQUIRED_GO="1.25.0"
        if command -v go &> /dev/null && go version | grep -q "go${REQUIRED_GO}"; then
          echo "Go ${REQUIRED_GO} already installed - skipping download"
        else
          echo "Installing Go ${REQUIRED_GO}..."
          curl -sL "https://go.dev/dl/go${REQUIRED_GO}.linux-amd64.tar.gz" | sudo tar -C /usr/local -xzf -
          export PATH="/usr/local/go/bin:$PATH"
          echo "/usr/local/go/bin" >> $GITHUB_PATH
          echo "Go ${REQUIRED_GO} installed"
        fi
        go version

    - name: Run benchmarks
      run: |
        echo "Running performance benchmarks..."
        go test -bench=. -benchmem ./tests/benchmarks/... > benchmark_results.txt 2>&1

        grep "Benchmark.*-[0-9].*" benchmark_results.txt > clean_benchmarks.txt || true

    - name: Convert to Prometheus metrics
      run: |
        echo "# HELP benchmark_time_per_op Time per operation in nanoseconds" > prometheus_metrics.txt
        echo "# TYPE benchmark_time_per_op gauge" >> prometheus_metrics.txt
        echo "# HELP benchmark_memory_per_op Memory per operation in bytes" >> prometheus_metrics.txt
        echo "# TYPE benchmark_memory_per_op gauge" >> prometheus_metrics.txt
        echo "# HELP benchmark_allocs_per_op Allocations per operation" >> prometheus_metrics.txt
        echo "# TYPE benchmark_allocs_per_op gauge" >> prometheus_metrics.txt

        while IFS= read -r line; do
          if [[ -n "$line" ]]; then
            BENCHMARK_NAME=$(echo "$line" | awk '{print $1}' | sed 's/-[0-9]*$//')
            TIME_PER_OP=$(echo "$line" | awk '{print $3}')
            MEMORY_PER_OP=$(echo "$line" | awk '{print $4}')
            ALLOCS_PER_OP=$(echo "$line" | awk '{print $5}')

            CLEAN_NAME=$(echo "$BENCHMARK_NAME" | sed 's/[^a-zA-Z0-9_]/_/g')

            echo "benchmark_time_per_op{benchmark=\"$CLEAN_NAME\"} ${TIME_PER_OP/ns/}" >> prometheus_metrics.txt
            echo "benchmark_memory_per_op{benchmark=\"$CLEAN_NAME\"} ${MEMORY_PER_OP/B\/op/}" >> prometheus_metrics.txt
            echo "benchmark_allocs_per_op{benchmark=\"$CLEAN_NAME\"} ${ALLOCS_PER_OP/allocs\/op/}" >> prometheus_metrics.txt
          fi
        done < clean_benchmarks.txt

    - name: Push to Prometheus Pushgateway
      env:
        PROMETHEUS_PUSHGATEWAY_URL: ${{ secrets['PROMETHEUS_PUSHGATEWAY_URL'] }}
      run: |
        if [ -n "$PROMETHEUS_PUSHGATEWAY_URL" ]; then
          echo "Pushing metrics to Prometheus..."
          curl --data-binary @prometheus_metrics.txt \
            "$PROMETHEUS_PUSHGATEWAY_URL/metrics/job/benchmark/instance/${{ github.run_id }}"
        else
          echo "PROMETHEUS_PUSHGATEWAY_URL not configured, skipping push"
        fi

    - name: Upload benchmark results
      uses: actions/upload-artifact@v4
      with:
        name: benchmark-results
        path: |
          benchmark_results.txt
          clean_benchmarks.txt
          prometheus_metrics.txt
        retention-days: 30

    - name: Display results summary
      run: |
        echo "=== Benchmark Results Summary ==="
        cat prometheus_metrics.txt | grep "benchmark_time_per_op" | head -10
