Implementation:AUTOMATIC1111 Stable diffusion webui Startup Performance Timer
| Knowledge Sources | |
|---|---|
| Domains | Performance Monitoring, Startup Profiling |
| Last Updated | 2025-05-15 00:00 GMT |
Overview
Provides a lightweight timing utility with the Timer and TimerSubcategory classes for measuring and reporting elapsed time across categorized code sections, primarily used for startup profiling.
Description
The Timer class records elapsed time between consecutive record() calls, accumulating results into named categories stored in a dictionary. Each call to record(category) captures the time since the last call and adds it to the named category. The elapsed() method returns and resets the internal stopwatch. The summary() method produces a human-readable string showing total time and a breakdown of categories that took at least 0.1 seconds. The dump() method serializes timing data as a dictionary, and reset() reinitializes the timer.
The TimerSubcategory class implements a context manager that creates hierarchical timing categories. When entering a subcategory, it prepends the category name as a prefix to subsequent records, enabling nested timing hierarchies with "/" as a separator. It tracks its own elapsed time and records the subcategory duration on exit.
The module also creates a global startup_timer instance that is used throughout the application to profile startup phases. An optional --log-startup command-line flag enables real-time printing of timing records during startup. The startup_record variable stores the final dumped startup timing data.
Usage
Use this module to measure and report execution time for application startup phases, model loading, UI creation, and any other operations where timing visibility is needed.
Code Reference
Source Location
- Repository: AUTOMATIC1111_Stable_diffusion_webui
- File: modules/timer.py
- Lines: 1-91
Signature
class TimerSubcategory:
def __init__(self, timer: Timer, category: str)
def __enter__(self) -> None
def __exit__(self, exc_type, exc_val, exc_tb) -> None
class Timer:
def __init__(self, print_log: bool = False)
def elapsed(self) -> float
def add_time_to_record(self, category: str, amount: float) -> None
def record(self, category: str, extra_time: float = 0, disable_log: bool = False) -> None
def subcategory(self, name: str) -> TimerSubcategory
def summary(self) -> str
def dump(self) -> dict
def reset(self) -> None
startup_timer: Timer
startup_record: dict
Import
from modules import timer
# Access the global startup timer
startup_timer = timer.startup_timer
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| print_log | bool | No | Whether to print timing records to stdout; defaults to False. |
| category | str | Yes | Name for the timing category in record() and subcategory(). |
| extra_time | float | No | Additional time to add to a record; defaults to 0. |
| name | str | Yes | Subcategory name for subcategory(). |
Outputs
| Name | Type | Description |
|---|---|---|
| elapsed | float | Seconds elapsed since the last elapsed() or record() call. |
| summary | str | Human-readable string showing total time and category breakdown. |
| dump | dict | Dictionary with "total" (float) and "records" (dict of category to seconds). |
Usage Examples
from modules.timer import Timer
t = Timer(print_log=True)
# Time a sequence of operations
import time
time.sleep(0.5)
t.record("load model")
time.sleep(0.2)
t.record("create ui")
# Use subcategories
with t.subcategory("extensions"):
time.sleep(0.1)
t.record("builtin")
time.sleep(0.1)
t.record("custom")
print(t.summary()) # e.g., "0.9s (load model: 0.5s, create ui: 0.2s)"
print(t.dump())