#!/bin/bash
# Benchmark runner for Lux vs other languages

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR/.."

echo "=== Lux Language Benchmarks ==="
echo "Date: $(date)"
echo ""

# Build Lux compiler in release mode
echo "Building Lux compiler..."
cargo build --release 2>/dev/null

# Function to time a command using /usr/bin/time or bash time
time_cmd() {
    local name="$1"
    shift
    # Use bash's time with TIMEFORMAT
    TIMEFORMAT="%R"
    local elapsed=$( { time "$@" > /dev/null 2>&1; } 2>&1 )
    printf "  %-20s %8.3f s\n" "$name" "$elapsed"
}

# Fibonacci benchmark
echo "=== Fibonacci (fib(35)) ==="

# Lux compiled
echo "Compiling Lux (native)..."
cargo run --release -- compile benchmarks/fib.lux -o /tmp/fib_lux 2>/dev/null

time_cmd "Lux (native)" /tmp/fib_lux

# Node.js
time_cmd "Node.js" node benchmarks/fib.js

# Rust (compile + run)
echo "Compiling Rust..."
rustc -O benchmarks/fib.rs -o /tmp/fib_rust 2>/dev/null
time_cmd "Rust (native)" /tmp/fib_rust

echo ""

# List operations benchmark
echo "=== List Operations (10k elements) ==="

cargo run --release -- compile benchmarks/list_ops.lux -o /tmp/list_ops_lux 2>/dev/null
time_cmd "Lux (native)" /tmp/list_ops_lux

time_cmd "Node.js" node benchmarks/list_ops.js

rustc -O benchmarks/list_ops.rs -o /tmp/list_ops_rust 2>/dev/null
time_cmd "Rust (native)" /tmp/list_ops_rust

echo ""

# Primes benchmark
echo "=== Prime Counting (up to 10000) ==="

cargo run --release -- compile benchmarks/primes.lux -o /tmp/primes_lux 2>/dev/null
time_cmd "Lux (native)" /tmp/primes_lux

time_cmd "Node.js" node benchmarks/primes.js

rustc -O benchmarks/primes.rs -o /tmp/primes_rust 2>/dev/null
time_cmd "Rust (native)" /tmp/primes_rust

echo ""
echo "=== Summary ==="
echo "Lux compiles to native code via C, comparable to other AOT-compiled languages."
echo "Performance depends on optimization level and runtime overhead."
