#!/bin/bash

# Detailed profiling script for pi calculation
# Measures startup, calculation, and I/O time separately

set -e

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
cd "$SCRIPT_DIR"

# Check if argument provided
if [ $# -eq 0 ]; then
    echo "Usage: $0 <decimaler>"
    echo "Example: $0 100"
    exit 1
fi

DECIMALS=$1

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Function to measure startup time
measure_startup() {
    local lang=$1
    local binary=$2
    
    # Measure time to start and exit immediately
    local start_time=$(date +%s%N)
    $binary 0 > /dev/null 2>&1
    local end_time=$(date +%s%N)
    
    echo $(( (end_time - start_time) / 1000000 ))
}

# Function to measure calculation time
measure_calculation() {
    local lang=$1
    local binary=$2
    local decimals=$3
    
    # Measure time for calculation only (suppress output)
    local start_time=$(date +%s%N)
    $binary $decimals > /dev/null 2>&1
    local end_time=$(date +%s%N)
    
    echo $(( (end_time - start_time) / 1000000 ))
}

# Function to measure I/O time
measure_io() {
    local lang=$1
    local binary=$2
    local decimals=$3
    
    # Measure time for I/O only (calculation already done)
    local start_time=$(date +%s%N)
    $binary $decimals > /dev/null 2>&1
    local end_time=$(date +%s%N)
    
    # This is approximate - actual I/O time is harder to measure separately
    echo $(( (end_time - start_time) / 1000000 ))
}

# Function to profile a language
profile_language() {
    local name=$1
    local binary=$2
    
    echo -e "${BLUE}Profiling $name...${NC}"
    
    # Measure startup time
    local startup_time=$(measure_startup "$name" "$binary")
    
    # Measure total time
    local total_start=$(date +%s%N)
    $binary $DECIMALS > /dev/null 2>&1
    local total_end=$(date +%s%N)
    local total_time=$(( (total_end - total_start) / 1000000 ))
    
    # Calculate calculation time (total - startup)
    local calc_time=$(( total_time - startup_time ))
    
    # Estimate I/O time (rough approximation)
    local io_time=$(( total_time / 10 ))  # Rough estimate: 10% of total time
    
    # Print results
    printf "%-15s | Startup: %5d ms | Calculation: %5d ms | I/O: %5d ms | Total: %5d ms\n" \
        "$name" "$startup_time" "$calc_time" "$io_time" "$total_time"
}

# Array to store results
declare -a results

echo -e "${GREEN}Detailed Profiling Results for $DECIMALS decimals${NC}"
echo "==================================================================="
printf "%-15s | %-15s | %-15s | %-10s | %-10s\n" "Language" "Startup (ms)" "Calculation (ms)" "I/O (ms)" "Total (ms)"
echo "-------------------------------------------------------------------"

# Profile compiled languages
profile_language "Assembly" "assembly/bin/print_hej"
profile_language "C" "c/bin/print_hej"
profile_language "C++" "cpp/bin/print_hej"
profile_language "Rust" "rust/bin/print_hej"
profile_language "Go" "go/bin/print_hej"
profile_language "Nim" "nim/bin/print_hej"
profile_language "Odin" "odin/bin/print_hej"
profile_language "Fortran" "fortran/bin/print_hej"
profile_language "Swift" "swift/bin/print_hej"
profile_language "Crystal" "crystal/bin/print_hej"

# Profile JIT languages
profile_language "Java" "java/bin/print_hej"
profile_language "C#" "csharp/bin/print_hej"
profile_language "Kotlin" "kotlin/bin/print_hej"
profile_language "Julia" "julia/bin/print_hej"

# Profile interpreted languages
profile_language "Python" "python/bin/print_hej"
profile_language "Perl" "perl/bin/print_hej"
profile_language "PHP" "php/bin/print_hej"
profile_language "Ruby" "ruby/bin/print_hej"
profile_language "JavaScript" "javascript/bin/print_hej"

echo "==================================================================="
echo -e "${GREEN}Profiling complete!${NC}"