"""Benchmark the runtime optimizations in PR #12.

Compares:
  - ConsoleOptions.__eq__ (explicit short-circuit vs all(getattr(...)))
  - ConsoleOptions.update() (identity check vs isinstance)
  - _emoji_replace() (inline cache vs _get_emoji() call)

Usage:
  python3.13 bench_runtime.py
"""

import os
import sys
import timeit

sys.path.insert(0, os.path.expanduser("~/rich"))


def bench(label, stmt, setup, number=500_000):
    times = timeit.repeat(stmt, setup, number=number, repeat=5)
    best = min(times)
    per_call_ns = best / number * 1e9
    print(
        f"  {label}: {best * 1000:.1f}ms total, {per_call_ns:.0f}ns/call ({number:,} iterations, best of 5)"
    )
    return best


print(f"Python {sys.version}")
print(f"Rich path: {os.path.expanduser('~/rich')}")
print()

# --- 1. ConsoleOptions.__eq__ ---
print("=== ConsoleOptions.__eq__ ===")
eq_setup = """\
import sys, os
sys.path.insert(0, os.path.expanduser("~/rich"))
from rich.console import Console
c = Console()
opts_a = c.options
opts_b = c.options.copy()
"""
bench("__eq__ (equal objects)", "opts_a == opts_b", eq_setup)
bench("__eq__ (same object)", "opts_a == opts_a", eq_setup)

eq_setup_diff = (
    eq_setup
    + """\
from rich.console import ConsoleDimensions
opts_c = opts_b.copy()
opts_c.size = ConsoleDimensions(999, 999)
"""
)
bench("__eq__ (differ at size)", "opts_a == opts_c", eq_setup_diff)
print()

# --- 2. ConsoleOptions.update() ---
print("=== ConsoleOptions.update() ===")
update_setup = """\
import sys, os
sys.path.insert(0, os.path.expanduser("~/rich"))
from rich.console import Console
c = Console()
opts = c.options
"""
bench("update(width=80)", "opts.update(width=80)", update_setup)
bench("update() no changes", "opts.update()", update_setup)
bench(
    "update(width=80, no_wrap=True, highlight=False)",
    "opts.update(width=80, no_wrap=True, highlight=False)",
    update_setup,
)
print()

# --- 3. _emoji_replace ---
print("=== _emoji_replace() ===")
emoji_setup = """\
import sys, os
sys.path.insert(0, os.path.expanduser("~/rich"))
from rich._emoji_replace import _emoji_replace
"""
bench(
    "_emoji_replace (with emoji)",
    '_emoji_replace("Hello :wave: world :smile:")',
    emoji_setup,
    number=200_000,
)
bench(
    "_emoji_replace (no emoji)",
    '_emoji_replace("Hello world, no emojis here")',
    emoji_setup,
    number=200_000,
)
print()

print("Done.")
