classification
Title: Add a profile decorator to profile and cProfile
Type: enhancement Stage: patch review
Components: Extension Modules, Library (Lib) Versions: Python 3.4
process
Status: open Resolution:
Dependencies: 8916 Superseder:
Assigned To: Nosy List: arigo, benjamin.peterson, bjorns, debatem1, eric.araujo, ezio.melotti, fdrake, georg.brandl, giampaolo.rodola, gvanrossum, nbastin, pitrou, rhettinger, tim.peters, tleeuwenburg@gmail.com, ubershmekel
Priority: normal Keywords: needs review, patch

Created on 2010-07-17 14:01 by giampaolo.rodola, last changed 2013-03-14 13:20 by giampaolo.rodola.

Files
File name Uploaded Description Edit
profile.patch giampaolo.rodola, 2010-07-17 14:01
profile.patch giampaolo.rodola, 2013-02-12 15:07 provide new runcall() and runblock() functions review
profile3.patch giampaolo.rodola, 2013-02-25 21:33 review
Messages (8)
msg110555 - (view) Author: Giampaolo Rodola' (giampaolo.rodola) * (Python committer) Date: 2010-07-17 14:01
A patch including tests is in attachment.
Example usage:

>>> from cProfile import profile
>>>
>>> @profile
... def factorial(n):
...     n = abs(int(n))
...     if n < 1: 
...             n = 1
...     x = 1
...     for i in range(1, n+1):
...             x = i * x
...     return x
... 
>>>
>>> factorial(5)
    Thu Jul 15 20:58:21 2010    /tmp/tmpIDejr5

             4 function calls in 0.000 CPU seconds

       Ordered by: internal time, call count

       ncalls  tottime  percall  cumtime  percall filename:lineno(function)
            1    0.000    0.000    0.000    0.000 profiler.py:120(factorial)
            1    0.000    0.000    0.000    0.000 {range}
            1    0.000    0.000    0.000    0.000 {abs}
            1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

120
>>>


Before including doc changes I'd like to hear whether this is actually desirable or not.
msg124464 - (view) Author: Lukas Lueg (ebfe) Date: 2010-12-21 22:08
+1
msg132576 - (view) Author: Tennessee Leeuwenburg (tleeuwenburg@gmail.com) Date: 2011-03-30 09:33
I have been working on a similar idea. I just wanted to raise an issue I ran into which might affect this code also. The decorated function ended up with a different function signature to the original inner function. This can be important sometimes. I had to use the decorator.decorator module to give the outer function the same signature as the inner function.
msg140269 - (view) Author: √Čric Araujo (eric.araujo) * (Python committer) Date: 2011-07-13 15:20
See #8916 for adding standard functionality similar to the decorator module.
msg181917 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2013-02-11 17:54
Brief comments:

- Please don't call it profile -- we already have a module by that name.

- Please make it so that both the decorator and context manager can specify a file where to dump the raw data -- basically it needs to have the same functionality as the functions run()/runctx()/runcall() (the latter TBD, see issue 17130).

- Please also make Profile object itself the context manager -- all you have to do is add __enter__() and __exit__() that call enable() and disable().  (But this doesn't completely replace the global function, which has more functionality -- it prints the profile or dumps the data).
msg181918 - (view) Author: Giampaolo Rodola' (giampaolo.rodola) * (Python committer) Date: 2013-02-11 17:55
Ok, will look into this soon.
msg181962 - (view) Author: Giampaolo Rodola' (giampaolo.rodola) * (Python committer) Date: 2013-02-12 15:07
A preliminary patch for cProfile.py is in attachment. Will make changes to profile.py later.
msg182986 - (view) Author: Giampaolo Rodola' (giampaolo.rodola) * (Python committer) Date: 2013-02-25 21:33
Ok, here's an updated patch modeled after:
http://hg.python.org/cpython/rev/422169310b7c

It works fine with cProfile.py but not with profile.py where I get this exception when I try to use the context manager (tests can be run in order to reproduce it):

  File "/home/giampaolo/svn/python/3.4-profile/Lib/profile.py", line 339, in trace_dispatch_return
    assert frame is self.cur[-2].f_back, ("Bad return", self.cur[-3])
AssertionError: ('Bad return', ('profile', 0, ''))

I have no clue what this error means.
I wasn't able to add a context manager for profile.Profile for the same reason.
Any clue?
History
Date User Action Args
2013-03-14 13:20:09giampaolo.rodolasetnosy: + tim.peters, fdrake, rhettinger, nbastin, benjamin.peterson
2013-02-25 21:33:51ebfesetnosy: - ebfe
2013-02-25 21:33:18giampaolo.rodolasetfiles: + profile3.patch
nosy: + arigo
messages: + msg182986

2013-02-25 15:59:34eric.araujosetversions: + Python 3.4, - Python 3.3
2013-02-25 15:59:30eric.araujosetnosy: + bjorns
2013-02-25 15:59:18eric.araujolinkissue17130 superseder
2013-02-12 15:07:18giampaolo.rodolasetfiles: + profile.patch

messages: + msg181962
2013-02-12 12:47:33giampaolo.rodolasetnosy: + georg.brandl
2013-02-11 17:55:25giampaolo.rodolasetmessages: + msg181918
2013-02-11 17:54:11gvanrossumsetnosy: + gvanrossum
messages: + msg181917
2012-01-25 09:45:03ubershmekelsetnosy: + ubershmekel
2011-07-13 15:57:25collinwintersetnosy: - collinwinter
2011-07-13 15:26:50ezio.melottisetnosy: + ezio.melotti
2011-07-13 15:20:28eric.araujosettitle: A decorator for cProfile and profile modules -> Add a profile decorator to profile and cProfile
nosy: + eric.araujo

messages: + msg140269

dependencies: + Move PEP 362 (function signature objects) into inspect
components: + Extension Modules, - Benchmarks
2011-03-30 09:33:06tleeuwenburg@gmail.comsetnosy: + tleeuwenburg@gmail.com
messages: + msg132576
2010-12-21 22:08:38ebfesetnosy: + ebfe
messages: + msg124464
2010-12-14 19:05:55r.david.murraysetassignee: collinwinter ->
stage: patch review
type: enhancement
versions: + Python 3.3, - Python 3.2
2010-07-18 01:12:40debatem1setnosy: + debatem1
2010-07-17 14:01:10giampaolo.rodolacreate