#!/bin/bash

# Optima Ops CLI - 性能测试脚本
# 对比 TypeScript 和 Rust 版本的性能

set -e

echo "========================================"
echo "  Optima Ops CLI 性能测试"
echo "========================================"
echo ""

# 检查环境
check_requirements() {
    echo "检查环境..."

    # 检查 Node.js
    if ! command -v node &> /dev/null; then
        echo "❌ Node.js 未安装"
        exit 1
    fi
    echo "✓ Node.js $(node --version)"

    # 检查 npm
    if ! command -v npm &> /dev/null; then
        echo "❌ npm 未安装"
        exit 1
    fi
    echo "✓ npm $(npm --version)"

    # 检查 Rust
    if ! command -v cargo &> /dev/null; then
        echo "❌ Rust 未安装"
        echo "请运行: curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh"
        exit 1
    fi
    echo "✓ Rust $(rustc --version)"

    # 检查 TypeScript 版本
    if [ ! -d "../optima-ops-cli" ]; then
        echo "❌ TypeScript 版本不存在"
        exit 1
    fi
    echo "✓ TypeScript 版本找到"

    echo ""
}

# 构建 Rust 版本
build_rust() {
    echo "构建 Rust 版本..."
    cargo build --release --quiet
    echo "✓ Rust 版本构建完成"
    echo ""
}

# 测试函数
run_benchmark() {
    local name=$1
    local command=$2
    local iterations=${3:-3}

    echo "测试: $name"
    echo "命令: $command"
    echo "迭代: $iterations 次"
    echo ""

    local total_time=0

    for i in $(seq 1 $iterations); do
        echo -n "  第 $i 次... "

        # 使用 time 命令测量
        local start=$(date +%s%N)
        eval $command > /dev/null 2>&1
        local end=$(date +%s%N)

        local elapsed=$(( (end - start) / 1000000 ))  # 转换为毫秒
        total_time=$((total_time + elapsed))

        echo "${elapsed}ms"
    done

    local avg_time=$((total_time / iterations))
    echo "  平均: ${avg_time}ms"
    echo ""

    echo $avg_time
}

# 主测试流程
main() {
    check_requirements
    build_rust

    echo "========================================"
    echo "  开始性能测试"
    echo "========================================"
    echo ""

    # TypeScript 路径
    TS_CLI="../optima-ops-cli"
    # Rust 路径
    RUST_CLI="./target/release/oor"

    # 测试结果数组
    declare -A ts_results
    declare -A rust_results

    # ===== 测试 1: 版本命令 =====
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    echo "测试 1: version 命令"
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    echo ""

    ts_results[version]=$(run_benchmark "TypeScript - version" "npm run --prefix $TS_CLI dev -- version" 3)
    rust_results[version]=$(run_benchmark "Rust - version" "$RUST_CLI version" 3)

    # ===== 测试 2: 环境命令 =====
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    echo "测试 2: env 命令"
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    echo ""

    ts_results[env]=$(run_benchmark "TypeScript - env" "npm run --prefix $TS_CLI dev -- env" 3)
    rust_results[env]=$(run_benchmark "Rust - env" "$RUST_CLI env" 3)

    # ===== 测试 3: help 命令 =====
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    echo "测试 3: --help 命令"
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    echo ""

    ts_results[help]=$(run_benchmark "TypeScript - help" "npm run --prefix $TS_CLI dev -- --help" 3)
    rust_results[help]=$(run_benchmark "Rust - help" "$RUST_CLI --help" 3)

    # 注意: 以下命令需要 SSH 连接,可能需要凭证
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    echo "注意: 以下测试需要 SSH 连接到生产环境"
    echo "如果没有配置 SSH 密钥,将跳过这些测试"
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    echo ""

    # 检查 SSH 密钥
    if [ -f ~/.ssh/optima-ec2-key ]; then
        echo "✓ 找到 SSH 密钥,继续网络测试..."
        echo ""

        # ===== 测试 4: services health =====
        echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
        echo "测试 4: services health (需要网络)"
        echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
        echo ""

        ts_results[health]=$(run_benchmark "TypeScript - services health" "npm run --prefix $TS_CLI dev -- services health" 2)
        rust_results[health]=$(run_benchmark "Rust - services health" "$RUST_CLI services health" 2)

    else
        echo "⚠ 未找到 SSH 密钥,跳过网络测试"
        echo ""
    fi

    # ===== 生成报告 =====
    echo ""
    echo "========================================"
    echo "  性能测试报告"
    echo "========================================"
    echo ""

    printf "%-20s %-15s %-15s %-15s\n" "测试" "TypeScript" "Rust" "提升"
    echo "────────────────────────────────────────────────────────────────"

    for test in version env help health; do
        if [ -n "${ts_results[$test]}" ] && [ -n "${rust_results[$test]}" ]; then
            ts_time=${ts_results[$test]}
            rust_time=${rust_results[$test]}

            if [ $rust_time -gt 0 ]; then
                speedup=$(awk "BEGIN {printf \"%.2f\", $ts_time / $rust_time}")
            else
                speedup="N/A"
            fi

            printf "%-20s %-15s %-15s %-15s\n" "$test" "${ts_time}ms" "${rust_time}ms" "${speedup}x"
        fi
    done

    echo ""
    echo "========================================"
    echo "  测试完成!"
    echo "========================================"
}

# 运行主函数
main
