api suggestions for the cProfile module

I use cProfile a lot, and would like to suggest three backwards-compatible improvements to the API. 1: When using cProfile on a specific piece of code I often use the enable() and disable() methods. It occurred to me that this would be an obvious place to use a context manager. 2: Enhance the `print_stats` method on Profile to accept more options currently available only through the pstats.Stats class. For example, strip_dirs could be a boolean argument, and limit could accept an int. This would reduce the number of cases you'd need to use the more complex API. 3: I often forget which string keys are available for sorting. It would be nice to add an enum for these so a user could have their linter and IDE check that value pre-runtime. Since it would subclass `str` and `Enum` it would still work with all currently existing code. The current documentation contains the following code: import cProfile, pstats, io pr = cProfile.Profile() pr.enable() # ... do something ... pr.disable() s = io.StringIO() sortby = 'cumulative' ps = pstats.Stats(pr, stream=s).sort_stats(sortby) ps.print_stats() print(s.getvalue()) While the code below doesn't exactly match the functionality above (eg. not using StringIO), I envision the context manager working like this, along with some adjustments on how to get the stats from the profiler: import cProfile, pstats with cProfile.Profile() as pr: # ... do something ... pr.print_stats(sort=pstats.Sort.cumulative, limit=10, strip_dirs=True) As you can see, the code is shorter and somewhat more self-documenting. The best thing about these suggestions is that as far as I can tell they would be backwards-compatible API additions. What do you think? Thank you in advance for your time! /Thane

On 01/10/2017 08:36 AM, Thane Brimhall wrote:
Does anyone have thoughts on this topic? I assume the silence is because this suggestion is too trivial to matter.
Sometimes it's just a matter of timing. :)
Absolutely.
I don't have much experience with cProfile, but this seems reasonable.
Absolutely! :)
The `pr.print_stats... line should not be inside the `with` block unless you want to profile that part as well. These suggestions seem fairly uncontroversial. Have you opened an issue on the issue tracker? The fun part of the patch will be the C code, but a Python proof-of-concept would be useful. -- ~Ethan~

Thanks for getting back to me on this! Yes timing can be a big factor. :) Turns out this gave me opportunity to look a little further back in the archives and someone suggested a very similar API change in November, so maybe more people than just me would want a feature like this. Good call on putting the print_stats outside of the context block. Kinda meta to profile the profiler... If the next step is to open an issue on the tracker, I'll do that. I can work on a Python proof-of-concept to attach there as well. Again, thanks for your feedback! /Thane On Tuesday, January 10, 2017 at 9:57:54 AM UTC-7, Ethan Furman wrote:

On 1/10/2017 11:36 AM, Thane Brimhall wrote:
Does anyone have thoughts on this topic? I assume the silence is because this suggestion is too trivial to matter.
1 and 3 don't really need discussion here. 2 perhaps. I would open 3 separate enhancement issues. As near as I can tell, profile and cProfile have the same API and to the extent it is true, this should be maintained.
-- Terry Jan Reedy

On Wed, Dec 21, 2016 at 1:50 AM, Thane Brimhall <thane.brimhall@gmail.com> wrote:
I think this makes sense. I did that in https://bugs.python.org/issue9285 but unfortunately I got stuck and the issue remained stagnant. Signaling it here just in case somebody has some insights on how to proceed.
I'm not sure about this. I agree the current API is not the nicest one. I use a wrapper on top of cProfile which does this: stats = pstats.Stats(file.name) if strip_dirs: stats.strip_dirs() if isinstance(sort, (tuple, list)): stats.sort_stats(*sort) else: stats.sort_stats(sort) stats.print_stats(lines) With your proposal we would have 2 ways of doing the same thing and I'm not entirely sure that is good. 3: I often forget which string keys are available for sorting. It would be
-- Giampaolo - http://grodola.blogspot.com

On 01/10/2017 08:36 AM, Thane Brimhall wrote:
Does anyone have thoughts on this topic? I assume the silence is because this suggestion is too trivial to matter.
Sometimes it's just a matter of timing. :)
Absolutely.
I don't have much experience with cProfile, but this seems reasonable.
Absolutely! :)
The `pr.print_stats... line should not be inside the `with` block unless you want to profile that part as well. These suggestions seem fairly uncontroversial. Have you opened an issue on the issue tracker? The fun part of the patch will be the C code, but a Python proof-of-concept would be useful. -- ~Ethan~

Thanks for getting back to me on this! Yes timing can be a big factor. :) Turns out this gave me opportunity to look a little further back in the archives and someone suggested a very similar API change in November, so maybe more people than just me would want a feature like this. Good call on putting the print_stats outside of the context block. Kinda meta to profile the profiler... If the next step is to open an issue on the tracker, I'll do that. I can work on a Python proof-of-concept to attach there as well. Again, thanks for your feedback! /Thane On Tuesday, January 10, 2017 at 9:57:54 AM UTC-7, Ethan Furman wrote:

On 1/10/2017 11:36 AM, Thane Brimhall wrote:
Does anyone have thoughts on this topic? I assume the silence is because this suggestion is too trivial to matter.
1 and 3 don't really need discussion here. 2 perhaps. I would open 3 separate enhancement issues. As near as I can tell, profile and cProfile have the same API and to the extent it is true, this should be maintained.
-- Terry Jan Reedy

On Wed, Dec 21, 2016 at 1:50 AM, Thane Brimhall <thane.brimhall@gmail.com> wrote:
I think this makes sense. I did that in https://bugs.python.org/issue9285 but unfortunately I got stuck and the issue remained stagnant. Signaling it here just in case somebody has some insights on how to proceed.
I'm not sure about this. I agree the current API is not the nicest one. I use a wrapper on top of cProfile which does this: stats = pstats.Stats(file.name) if strip_dirs: stats.strip_dirs() if isinstance(sort, (tuple, list)): stats.sort_stats(*sort) else: stats.sort_stats(sort) stats.print_stats(lines) With your proposal we would have 2 ways of doing the same thing and I'm not entirely sure that is good. 3: I often forget which string keys are available for sorting. It would be
-- Giampaolo - http://grodola.blogspot.com
participants (5)
-
Ethan Furman
-
Giampaolo Rodola'
-
Gregory P. Smith
-
Terry Reedy
-
Thane Brimhall