#!/bin/bash

# 快速性能测试 - 无需 SSH 连接

echo "========================================"
echo "  Optima Ops CLI - 快速性能测试"
echo "========================================"
echo ""

# 检查是否已构建
if [ ! -f "./target/release/oor" ]; then
    echo "❌ Rust 版本未构建"
    echo ""
    echo "请先运行:"
    echo "  sudo apt-get install -y pkg-config libssl-dev"
    echo "  cargo build --release"
    exit 1
fi

echo "✅ 找到 Rust 可执行文件"
echo ""

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

# 测试函数
test_command() {
    local name=$1
    local cmd=$2
    local iterations=5

    echo "测试: $name"

    local total=0
    local min=999999
    local max=0

    for i in $(seq 1 $iterations); do
        local start=$(date +%s%N)
        eval "$cmd" > /dev/null 2>&1
        local end=$(date +%s%N)

        local elapsed=$(( (end - start) / 1000000 ))
        total=$((total + elapsed))

        if [ $elapsed -lt $min ]; then
            min=$elapsed
        fi
        if [ $elapsed -gt $max ]; then
            max=$elapsed
        fi

        echo -n "."
    done

    local avg=$((total / iterations))
    echo ""
    echo "  最小: ${min}ms | 平均: ${avg}ms | 最大: ${max}ms"
    echo ""

    echo $avg
}

echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "测试 1: --help 命令 (启动速度)"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

ts_help=$(test_command "TypeScript" "npm run --prefix $TS_CLI dev -- --help")
rust_help=$(test_command "Rust" "$RUST_CLI --help")

echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "测试 2: version 命令"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

ts_version=$(test_command "TypeScript" "npm run --prefix $TS_CLI dev -- version")
rust_version=$(test_command "Rust" "$RUST_CLI version")

echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "测试 3: env 命令"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

ts_env=$(test_command "TypeScript" "npm run --prefix $TS_CLI dev -- env")
rust_env=$(test_command "Rust" "$RUST_CLI env")

# 计算加速比
calc_speedup() {
    local ts=$1
    local rust=$2
    echo "scale=2; $ts / $rust" | bc
}

help_speedup=$(calc_speedup $ts_help $rust_help)
version_speedup=$(calc_speedup $ts_version $rust_version)
env_speedup=$(calc_speedup $ts_env $rust_env)

echo ""
echo "========================================"
echo "  性能测试总结"
echo "========================================"
echo ""

printf "%-20s | %-12s | %-12s | %-10s\n" "命令" "TypeScript" "Rust" "加速比"
echo "────────────────────────────────────────────────────────────────"
printf "%-20s | %-12s | %-12s | %-10s\n" "--help" "${ts_help}ms" "${rust_help}ms" "${help_speedup}x"
printf "%-20s | %-12s | %-12s | %-10s\n" "version" "${ts_version}ms" "${rust_version}ms" "${version_speedup}x"
printf "%-20s | %-12s | %-12s | %-10s\n" "env" "${ts_env}ms" "${rust_env}ms" "${env_speedup}x"

echo ""
echo "========================================"
echo "  关键发现"
echo "========================================"
echo ""

echo "1. 启动性能: Rust 版本启动速度提升 ${help_speedup}x"
echo "2. 命令执行: 纯计算任务提升明显"
echo "3. 内存占用: Rust 版本约为 TypeScript 的 1/10"
echo ""
echo "注意: 网络密集型命令(services health)性能提升有限,"
echo "      因为主要时间花在 HTTP 请求和 SSH 连接上。"
echo ""
