-
Notifications
You must be signed in to change notification settings - Fork 37
Implement cProfile-based profiling for performance analysis #271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
b555c6c
feat(cli): implement debug mode with coredumpy and viztracer
Davidyz 8d68912
chore(cli): exclude debugging.py from coverage
Davidyz 375add5
ci(cli): add debug group to pdm lock
Davidyz c5c24f8
Merge branch 'main' into cli/debug_mode_my
outp1 d851ec1
Merge branch 'main' into cli/debug_mode_my
outp1 1dd9ff9
feat(debugging): Implement cProfile-based profiling for performance a…
outp1 d3bc7d8
chore(dependencies): Remove viztracer from debug dependencies
outp1 9e9bcae
fix(cli): lazy import
Davidyz 0bc07d7
feat(debugging): add log directory creation and crash debugging support
outp1 4bbff7c
chore(build): revert makefile and ci/cd outdated changes
outp1 7e92612
chore(dependencies): Add coredumpy to development dependencies
outp1 05c6b6a
fix(debugging): Add noqa comment to suppress linting warning
outp1 41a8b11
docs(cli): remove debug dependency from cli help
outp1 f491c0b
fix(cli): make sure to enable `coredumpy` when available.
Davidyz 7bb3a66
tests(cli): Replace `MagicMock` with `Config` so that `debug` won't b…
Davidyz 1dde000
docs(cli): Add profiling and post-mortem debugging instructions
Davidyz a56f3b2
ci: Update git auto commit action and ignore main branch
Davidyz 5e30eb9
Auto generate docs
Davidyz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,65 @@ | ||
import atexit | ||
import cProfile | ||
import logging | ||
import os | ||
import pstats | ||
from datetime import datetime | ||
|
||
__LOG_DIR = os.path.expanduser("~/.local/share/vectorcode/logs/") | ||
|
||
logger = logging.getLogger(name=__name__) | ||
|
||
__profiler: cProfile.Profile | None = None | ||
|
||
|
||
def _ensure_log_dir(): | ||
"""Ensure the log directory exists""" | ||
os.makedirs(__LOG_DIR, exist_ok=True) | ||
|
||
|
||
def finish(): | ||
"""Clean up profiling and save results""" | ||
if __profiler is not None: | ||
try: | ||
__profiler.disable() | ||
stats_file = os.path.join( | ||
__LOG_DIR, | ||
f"cprofile-{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.stats", | ||
) | ||
__profiler.dump_stats(stats_file) | ||
print(f"cProfile stats saved to: {stats_file}") | ||
|
||
# Print summary stats | ||
stats = pstats.Stats(__profiler) | ||
stats.sort_stats("cumulative") | ||
stats.print_stats(20) | ||
except Exception as e: | ||
logger.warning(f"Failed to save cProfile output: {e}") | ||
|
||
|
||
def enable(): | ||
"""Enable cProfile-based profiling and crash debugging""" | ||
global __profiler | ||
|
||
try: | ||
_ensure_log_dir() | ||
|
||
# Initialize cProfile for comprehensive profiling | ||
__profiler = cProfile.Profile() | ||
__profiler.enable() | ||
atexit.register(finish) | ||
logger.info("cProfile profiling enabled successfully") | ||
|
||
try: | ||
import coredumpy # noqa: F401 | ||
|
||
logger.info("coredumpy crash debugging enabled successfully") | ||
coredumpy.patch_except(directory=__LOG_DIR) | ||
except Exception as e: | ||
logger.warning( | ||
f"Crash debugging will not be available. Failed to import coredumpy: {e}" | ||
) | ||
|
||
except Exception as e: | ||
logger.error(f"Failed to initialize cProfile: {e}") | ||
logger.warning("Profiling will not be available for this session") |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.