Profiler Module¶
Core profiler implementation with multi-track support and runtime control.
Profiler Class¶
stichotrope.profiler.Profiler
¶
Main profiler class with multi-track support and runtime enable/disable.
Example
profiler = Profiler("MyApp")
@profiler.track(0, "process_data") def process_data(data): return transform(data)
def complex_function(): with profiler.block(1, "database_query"): result = query_database() return result
results = profiler.get_results()
Source code in stichotrope/profiler.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 | |
Functions¶
__init__(name='Profiler')
¶
Initialize a new profiler instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Human-readable name for this profiler |
'Profiler'
|
Source code in stichotrope/profiler.py
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | |
block(track_idx, name)
¶
Context manager for profiling code blocks.
Example
with profiler.block(1, "database_query"): result = query_database()
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
track_idx
|
int
|
Track index for this block |
required |
name
|
str
|
Block name (required) |
required |
Yields:
| Type | Description |
|---|---|
None
|
None |
Source code in stichotrope/profiler.py
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 | |
clear()
¶
Clear all profiling data from all threads.
Source code in stichotrope/profiler.py
320 321 322 323 324 325 326 327 328 329 | |
export_csv(filename)
¶
Export profiling results to CSV file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Output CSV filename |
required |
Source code in stichotrope/profiler.py
493 494 495 496 497 498 499 500 501 502 503 504 | |
export_json(filename, indent=2)
¶
Export profiling results to JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Output JSON filename |
required |
indent
|
int
|
JSON indentation level |
2
|
Source code in stichotrope/profiler.py
506 507 508 509 510 511 512 513 514 515 516 517 518 | |
get_results()
¶
Get profiling results aggregated from all threads.
Returns:
| Type | Description |
|---|---|
ProfilerResults
|
ProfilerResults containing all tracks and blocks from all threads |
Source code in stichotrope/profiler.py
311 312 313 314 315 316 317 318 | |
is_started()
¶
Check if profiler is started.
Source code in stichotrope/profiler.py
105 106 107 | |
is_track_enabled(track_idx)
¶
Check if a specific track is enabled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
track_idx
|
int
|
Track index |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if track is enabled (default: True) |
Source code in stichotrope/profiler.py
164 165 166 167 168 169 170 171 172 173 174 175 | |
print_results()
¶
Print profiling results to console in a formatted table.
Source code in stichotrope/profiler.py
520 521 522 523 524 525 | |
set_track_enabled(track_idx, enabled)
¶
Enable or disable a specific track.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
track_idx
|
int
|
Track index |
required |
enabled
|
bool
|
True to enable, False to disable |
required |
Source code in stichotrope/profiler.py
153 154 155 156 157 158 159 160 161 162 | |
set_track_name(track_idx, name)
¶
Set a human-readable name for a track.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
track_idx
|
int
|
Track index |
required |
name
|
str
|
Track name |
required |
Source code in stichotrope/profiler.py
177 178 179 180 181 182 183 184 185 186 187 | |
start()
¶
Start profiling (resume data collection).
Source code in stichotrope/profiler.py
97 98 99 | |
stop()
¶
Stop profiling (pause data collection).
Source code in stichotrope/profiler.py
101 102 103 | |
track(track_idx, name=None)
¶
Decorator for profiling functions.
Example
@profiler.track(0, "process_data") def process_data(data): return transform(data)
Auto-detect function name¶
@profiler.track(0) def compute(): return result
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
track_idx
|
int
|
Track index for this function |
required |
name
|
Optional[str]
|
Optional name (defaults to function.name) |
None
|
Returns:
| Type | Description |
|---|---|
Callable
|
Decorator function |
Source code in stichotrope/profiler.py
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 | |
Global Functions¶
set_global_enabled¶
stichotrope.profiler.set_global_enabled(enabled)
¶
Enable or disable profiling globally across all profiler instances.
When disabled, decorators return identity functions (zero overhead).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
enabled
|
bool
|
True to enable profiling, False to disable |
required |
Source code in stichotrope/profiler.py
31 32 33 34 35 36 37 38 39 40 41 | |
is_global_enabled¶
stichotrope.profiler.is_global_enabled()
¶
Check if profiling is globally enabled.
Source code in stichotrope/profiler.py
44 45 46 | |
Usage Examples¶
Basic Profiler Usage¶
from stichotrope import Profiler
# Create a profiler instance
profiler = Profiler("MyApplication")
# Profile a function with decorator
@profiler.track(0, "process_data")
def process_data(data):
return transform(data)
# Profile a code block with context manager
def complex_function():
with profiler.block(1, "database_query"):
result = query_database()
return result
# Get results
results = profiler.get_results()
profiler.print_results()
Runtime Control¶
from stichotrope import Profiler, set_global_enabled
profiler = Profiler("MyApp")
# Per-profiler control
profiler.stop() # Pause profiling
profiler.start() # Resume profiling
# Per-track control
profiler.set_track_enabled(0, False) # Disable track 0
profiler.set_track_enabled(0, True) # Re-enable track 0
# Global control (affects all profilers)
set_global_enabled(False) # Disable all profiling (zero overhead)
set_global_enabled(True) # Re-enable profiling
Multi-Track Organization¶
from stichotrope import Profiler
profiler = Profiler("WebServer")
# Track 0: Request handling
@profiler.track(0, "handle_request")
def handle_request(request):
return process_request(request)
# Track 1: Database operations
@profiler.track(1, "db_query")
def query_database(query):
return execute_query(query)
# Track 2: Cache operations
@profiler.track(2, "cache_lookup")
def check_cache(key):
return cache.get(key)
# Results are organized by track
results = profiler.get_results()
for track in results.tracks:
print(f"Track {track.track_idx}: {len(track.blocks)} blocks")
Nested Profiling¶
from stichotrope import Profiler
profiler = Profiler("DataPipeline")
def process_pipeline(data):
# Outer block
with profiler.block(0, "full_pipeline"):
# Inner blocks
with profiler.block(1, "load_data"):
loaded = load(data)
with profiler.block(1, "transform_data"):
transformed = transform(loaded)
with profiler.block(1, "save_data"):
save(transformed)
return transformed
Multi-Threaded Usage¶
from stichotrope import Profiler
import threading
profiler = Profiler("MultiThreadApp")
@profiler.track(0, "worker_task")
def worker_task(task_id):
# Profiling in thread
with profiler.block(1, "task_processing"):
process(task_id)
# Start multiple threads
threads = [
threading.Thread(target=worker_task, args=(i,))
for i in range(5)
]
for t in threads:
t.start()
for t in threads:
t.join()
# Retrieve results from all threads
all_results = profiler.get_all_thread_data()
for thread_id, thread_results in all_results.items():
print(f"Thread {thread_id}: {thread_results}")
Implementation Details¶
Call-Site Caching¶
The profiler uses call-site caching to minimize overhead. Each unique call site (file, line number, function name) is cached, so subsequent calls to the same profiled function have minimal overhead.
Thread Safety¶
The Profiler is thread-safe by design in v0.2.0+. Each thread maintains its own profiling data independently, with a thread-safe aggregation mechanism for retrieving cross-thread results.
Per-thread operations (lock-free):
- track() decorator on functions
- block() context manager
- get_results() for current thread's data
Cross-thread operations (synchronized):
- get_all_thread_data() for aggregating results across threads
For multi-threaded applications, use get_all_thread_data() to retrieve and aggregate profiling results from all threads.
Performance Characteristics¶
- Overhead when enabled: ≤1% for blocks over 1ms. The raw overhead is typically 4µs so profiling anything within the same order of magnitude will result in significant timing distortions.
- Overhead when disabled: Zero overhead (decorators return identity functions)
For detailed performance analysis and benchmarking methodology, see Performance Documentation.
See Also¶
- Export Module - Exporting and displaying results
- Types Module - Data structure definitions
- Getting Started - Usage guide