-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Address major performance issue with recreating requests sessions ove…
…r and over
- Loading branch information
Showing
3 changed files
with
91 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import cProfile | ||
import functools | ||
import os | ||
import pstats | ||
import threading | ||
from datetime import datetime | ||
from pstats import SortKey | ||
|
||
# Keep track of lock stats | ||
lock_stats = {"acquire_count": 0, "wait_times": [], "acquire_locations": []} | ||
|
||
# Patch threading.Lock to track usage | ||
original_lock = threading.Lock | ||
|
||
|
||
def instrumented_lock(): | ||
lock = original_lock() | ||
original_acquire = lock.acquire | ||
|
||
@functools.wraps(original_acquire) | ||
def traced_acquire(*args, **kwargs): | ||
import traceback | ||
|
||
lock_stats["acquire_count"] += 1 | ||
# Get call location | ||
stack = traceback.extract_stack() | ||
# Get the caller's frame (excluding this function) | ||
caller = stack[-2] | ||
lock_stats["acquire_locations"].append(f"{caller.filename}:{caller.lineno}") | ||
return original_acquire(*args, **kwargs) | ||
|
||
lock.acquire = traced_acquire | ||
return lock | ||
|
||
|
||
# Apply the patch | ||
threading.Lock = instrumented_lock | ||
|
||
|
||
def profile_method(output_dir="profiles"): | ||
""" | ||
Decorator to profile pipenv method execution. | ||
""" | ||
|
||
def decorator(func): | ||
@functools.wraps(func) | ||
def wrapper(*args, **kwargs): | ||
# Create output directory if it doesn't exist | ||
os.makedirs(output_dir, exist_ok=True) | ||
|
||
# Create profile filename with timestamp | ||
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") | ||
profile_name = f"{func.__name__}_{timestamp}" | ||
profile_path = os.path.join(output_dir, f"{profile_name}.prof") | ||
|
||
# Setup profiler | ||
profiler = cProfile.Profile() | ||
|
||
# Start profiling | ||
profiler.enable() | ||
|
||
try: | ||
# Run the actual pipenv command | ||
result = func(*args, **kwargs) | ||
|
||
return result | ||
finally: | ||
# Stop profiling | ||
profiler.disable() | ||
|
||
# Save stats | ||
stats = pstats.Stats(profiler) | ||
stats.sort_stats(SortKey.CUMULATIVE) | ||
stats.dump_stats(profile_path) | ||
print(f"\nProfile saved to: {profile_path}") | ||
stats.print_stats(20) | ||
# Print lock statistics | ||
print("\nLock Statistics:") | ||
print(f"Total lock acquisitions: {lock_stats['acquire_count']}") | ||
print("\nTop 10 lock acquisition locations:") | ||
from collections import Counter | ||
|
||
locations = Counter(lock_stats["acquire_locations"]) | ||
for loc, count in locations.most_common(10): | ||
print(f"{count:5d} times: {loc}") | ||
|
||
return wrapper | ||
|
||
return decorator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters