-
-
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.
Check in developer helpers to improve pipenv
- Loading branch information
Showing
1 changed file
with
61 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import cProfile | ||
import functools | ||
import os | ||
import pstats | ||
from datetime import datetime | ||
from pstats import SortKey | ||
|
||
|
||
def profile_method(output_dir="profiles"): | ||
""" | ||
Decorator to profile pipenv method execution with focus on file reads. | ||
""" | ||
|
||
def decorator(func): | ||
@functools.wraps(func) | ||
def wrapper(*args, **kwargs): | ||
os.makedirs(output_dir, exist_ok=True) | ||
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") | ||
|
||
profiler = cProfile.Profile() | ||
profiler.enable() | ||
|
||
try: | ||
result = func(*args, **kwargs) | ||
return result | ||
finally: | ||
profiler.disable() | ||
|
||
# Save and analyze stats | ||
stats = pstats.Stats(profiler) | ||
stats.sort_stats(SortKey.CUMULATIVE) | ||
stats.dump_stats(profile_path) | ||
print(f"\nProfile saved to: {profile_path}") | ||
|
||
# Analyze file reads specifically | ||
print("\nAnalyzing file read operations:") | ||
print("-" * 50) | ||
|
||
# Get all entries involving file read operations | ||
read_stats = stats.stats | ||
for (file, line, name), (_, _, tt, _, callers) in read_stats.items(): | ||
if "read" in str(name): | ||
# Print the call stack for this read operation | ||
print(f"\nFile read at: {file}:{line}") | ||
print(f"Function: {name}") | ||
print(f"Time: {tt:.6f}s") | ||
print("Called by:") | ||
for caller in callers: | ||
caller_file, caller_line, caller_name = caller | ||
print(f" {caller_name} in {caller_file}:{caller_line}") | ||
print("-" * 30) | ||
|
||
# Print overall stats | ||
print("\nTop 20 overall calls:") | ||
stats.print_stats(20) | ||
|
||
return wrapper | ||
|
||
return decorator |