timing Examples

Basic Examples
Context Manager Examples
Decorator Examples
Advanced Examples

Full API Performance Monitor Script

from suitkaise import timing
import threading

overall = timing.Sktimer(max_times=1000)

@timing.timethis()
@timing.timethis(overall)
def handle_users():
    sorted(range(50_000))

@timing.timethis()
@timing.timethis(overall)
def handle_search():
    {str(i): i for i in range(100_000)}

@timing.timethis()
@timing.timethis(overall)
def handle_health():
    pass

def worker(num_requests):
    endpoints = [handle_users, handle_search, handle_health]
    for i in range(num_requests):
        endpoints[i % len(endpoints)]()

# 4 threads, 100 requests each — thread-safe, automatic aggregation
threads = [threading.Thread(target=worker, args=(100,)) for _ in range(4)]
for t in threads:
    t.start()
for t in threads:
    t.join()

# overall stats
print(f"Total requests: {overall.num_times}")
print(f"Overall mean: {overall.mean*1000:.1f}ms")
print(f"Overall p95:  {overall.percentile(95)*1000:.1f}ms")

# per-endpoint breakdown — stacked decorators give you both levels for free
for fn in [handle_users, handle_search, handle_health]:
    t = fn.timer
    print(f"  {fn.__name__}: n={t.num_times}, "
          f"mean={t.mean*1000:.1f}ms, p95={t.percentile(95)*1000:.1f}ms")