diff options
| author | Stephane Wirtel <stephane@tinyerp.com> | 2009-01-26 19:52:11 +0100 |
|---|---|---|
| committer | Stephane Wirtel <stephane@tinyerp.com> | 2009-01-26 19:52:11 +0100 |
| commit | c85a9033c64d7bb9fac40ffd53afbf537b3ad001 (patch) | |
| tree | a19eba812223495e717f2fd64d719c818f888f9c /bin/tools/misc.py | |
| parent | 535d1ced05bab147b49befb3f1f7b9a26ebe3b30 (diff) | |
[IMP] Add a decorator to run a profiler on a method/function
bzr revid: stephane@tinyerp.com-20090126185211-27m4agx5b43ctsba
Diffstat (limited to 'bin/tools/misc.py')
| -rw-r--r-- | bin/tools/misc.py | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/bin/tools/misc.py b/bin/tools/misc.py index e47e36b80d1..25972b4a67a 100644 --- a/bin/tools/misc.py +++ b/bin/tools/misc.py @@ -828,6 +828,28 @@ def logged(f): return wrapper +class profile(object): + def __init__(self, fname=None): + self.fname = fname + + def __call__(self, f): + from tools.func import wraps + + @wraps(f) + def wrapper(*args, **kwargs): + class profile_wrapper(object): + def __init__(self): + self.result = None + def __call__(self): + self.result = f(*args, **kwargs) + pw = profile_wrapper() + import cProfile + fname = self.fname or ("%s.cprof" % (f.func_name,)) + cProfile.runctx('pw()', globals(), locals(), filename=fname) + return pw.result + + return wrapper + def debug(what): """ This method allow you to debug your code without print |
