Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
bpo-34861 Make cProfile default output more useful#9655
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
Open
boxed
wants to merge
1
commit into
python:mainChoose a base branch
from
boxed:nicer-cprofile-command-line
base:main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+128
−4
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -26,9 +26,12 @@ | ||
| import marshal | ||
| import re | ||
| from collections import Counter | ||
| from enum import StrEnum, _simple_enum | ||
| from functools import cmp_to_key | ||
| from dataclasses import dataclass | ||
| from os.path import join | ||
| from pathlib import Path | ||
| __all__ = ["Stats", "SortKey", "FunctionProfile", "StatsProfile"] | ||
| @@ -276,16 +279,19 @@ def reverse_order(self): | ||
| return self | ||
| def strip_dirs(self): | ||
| return self._strip_directory_data(lambda fullpath_linenum_funcname: func_strip_path(fullpath_linenum_funcname)) | ||
| def _strip_directory_data(self, strip_function): | ||
| oldstats = self.stats | ||
| self.stats = newstats = {} | ||
| max_name_len = 0 | ||
| for func, (cc, nc, tt, ct, callers) in oldstats.items(): | ||
| newfunc = func_strip_path(func) | ||
| newfunc = strip_function(func) | ||
| if len(func_std_string(newfunc)) > max_name_len: | ||
| max_name_len = len(func_std_string(newfunc)) | ||
| newcallers = {} | ||
| for func2, caller in callers.items(): | ||
| newcallers[func_strip_path(func2)] = caller | ||
| newcallers[strip_function(func2)] = caller | ||
| if newfunc in newstats: | ||
| newstats[newfunc] = add_func_stats( | ||
| @@ -296,14 +302,30 @@ def strip_dirs(self): | ||
| old_top = self.top_level | ||
| self.top_level = new_top = set() | ||
| for func in old_top: | ||
| new_top.add(func_strip_path(func)) | ||
| new_top.add(strip_function(func)) | ||
| self.max_name_len = max_name_len | ||
| self.fcn_list = None | ||
| self.all_callees = None | ||
| return self | ||
| def strip_non_unique_dirs(self): | ||
| full_paths = set() | ||
| for (full_path, _, _), (_, _, _, _, callers) in self.stats.items(): | ||
| full_paths.add(full_path) | ||
| for (full_path_caller, _, _), _ in callers.items(): | ||
| full_paths.add(full_path_caller) | ||
| minimal_path_by_full_path = _build_minimal_path_by_full_path(full_paths) | ||
| def strip_function(fullpath_linenum_funcname): | ||
| fullpath, linenum, funcname = fullpath_linenum_funcname | ||
| return minimal_path_by_full_path[fullpath], linenum, funcname | ||
| return self._strip_directory_data(strip_function) | ||
| def calc_callees(self): | ||
| if self.all_callees: | ||
| return | ||
| @@ -774,4 +796,41 @@ def postcmd(self, stop, line): | ||
| except KeyboardInterrupt: | ||
| pass | ||
| def _build_minimal_path_by_full_path(paths): | ||
| if not paths: | ||
| return paths | ||
| completed = { | ||
| full_path: None | ||
| for full_path in paths | ||
| } | ||
| split_path_by_full = {full_path: Path(full_path).parts for full_path in paths} | ||
| max_step = max(len(x) for x in split_path_by_full.values()) | ||
| step = 1 | ||
| while step <= max_step: | ||
| short_path_by_full = { | ||
| full_path: split_path_by_full[full_path][-step:] | ||
| for full_path, value in completed.items() | ||
| if value is None | ||
| } | ||
| full_path_by_short = {v: k for k, v in short_path_by_full.items()} | ||
| for short_path, count in Counter(short_path_by_full.values()).most_common(): | ||
| if count == 1: | ||
| joined_short_path = join(*short_path) | ||
| # __init__.py is handled specially because it's a very common | ||
| # file name which gives no clue what file is meant | ||
| if joined_short_path == '__init__.py': | ||
boxed marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| continue | ||
| completed[full_path_by_short[short_path]] = joined_short_path | ||
| step += 1 | ||
| return { | ||
| full_path: short_path if short_path is not None else full_path | ||
| for full_path, short_path in completed.items() | ||
| } | ||
| # That's all, folks. | ||
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
2 changes: 2 additions & 0 deletions
2 Misc/NEWS.d/next/Library/2020-10-18-13-20-00.bpo-34861.8JanqQ.rst
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,2 @@ | ||
| cProfile command line output now defaults to cumulative time | ||
| cProfile command line gives you unique filenames in the output (plus an extra path level of path if the filename is __init__.py) |
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.