#!/usr/bin/env python3
"""
Benchmark script for UPath.exists() cold performance on GCS.

Usage examples:
    # Basic benchmark
    python benchmark.py gs://my-bucket/my-directory/
    
    # With debug logging to see gcsfs operations
    python benchmark.py gs://my-bucket/my-directory/ --debug
    
    # With custom log level
    python benchmark.py gs://my-bucket/my-directory/ --log-level INFO

Requirements:
    pip install universal-pathlib gcsfs google-cloud-storage
"""

import time
import argparse
import logging

from upath import UPath
import fsspec.utils


def setup_logging(debug: bool = False, log_level: str = None):
    """Setup logging for fsspec and gcsfs.
    
    This configures logging for:
    - gcsfs: Main GCS filesystem operations
    - gcsfs.credentials: Authentication and credential handling
    - fsspec: Core filesystem spec operations
    
    Args:
        debug: Enable debug logging for all components
        log_level: Custom log level (DEBUG, INFO, WARNING, ERROR)
    """
    if log_level:
        level = log_level.upper()
    elif debug:
        level = "DEBUG"
    else:
        level = "WARNING"
    
    # Setup logging for gcsfs and fsspec components
    fsspec.utils.setup_logging(logger_name="gcsfs", level=level)
    fsspec.utils.setup_logging(logger_name="gcsfs.credentials", level=level)
    fsspec.utils.setup_logging(logger_name="fsspec", level=level)
    
    # Setup root logger
    log_format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
    if level == "DEBUG":
        logging.basicConfig(level=logging.DEBUG, format=log_format)
        print(f"Debug logging enabled (level: {level})")
        print("You'll see detailed GCS operations, authentication, and caching behavior\n")
    elif level == "INFO":
        logging.basicConfig(level=logging.INFO, format=log_format)
        print(f"Info logging enabled\n")
    else:
        logging.basicConfig(level=getattr(logging, level, logging.WARNING))


def benchmark_exists(gcs_path: str) -> tuple[float, bool]:
    """
    Benchmark a single UPath.exists() call for cold performance.
    
    Args:
        gcs_path: Full GCS path (e.g., gs://bucket/directory/)
        
    Returns:
        Tuple of (elapsed_time, exists_result)
    """
    # Construct UPath for GCS
    path = UPath(gcs_path)
    
    print(f"Benchmarking: {path}")
    print("-" * 50)
    
    # Measure cold performance - single call
    start = time.perf_counter()
    exists = path.exists()
    end = time.perf_counter()
    
    elapsed = end - start
    
    return elapsed, exists


def main():
    parser = argparse.ArgumentParser(
        description='Benchmark UPath.exists() cold performance for GCS paths'
    )
    parser.add_argument(
        'path',
        help='GCS path (e.g., gs://bucket/directory/)'
    )
    parser.add_argument(
        '--debug',
        action='store_true',
        help='Enable debug logging for gcsfs and fsspec'
    )
    parser.add_argument(
        '--log-level',
        choices=['DEBUG', 'INFO', 'WARNING', 'ERROR'],
        help='Set logging level (overrides --debug)'
    )
    
    args = parser.parse_args()
    
    # Setup logging
    setup_logging(args.debug, args.log_level)
    
    try:
        # Run benchmark
        elapsed, exists = benchmark_exists(args.path)
        
        # Print results
        print("\n" + "=" * 50)
        print("RESULTS")
        print("=" * 50)
        print(f"Path: {args.path}")
        print(f"Exists: {exists}")
        print(f"Time: {elapsed:.6f} seconds")
        print(f"Time: {elapsed*1000:.2f} milliseconds")
        
    except Exception as e:
        print(f"Error during benchmark: {e}")
        raise


if __name__ == "__main__":
    main()
