-
Notifications
You must be signed in to change notification settings - Fork 0
/
qprofile.py
67 lines (59 loc) · 2.02 KB
/
qprofile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# From https://stackoverflow.com/questions/40132630/python-cprofiler-a-function
# CC-BY-SA
def qprofile(func):
def profiled_func(*args, **kwargs):
if 'profile' in kwargs and kwargs['profile']:
kwargs.pop('profile')
import cProfile
import pstats
from io import StringIO
profile = cProfile.Profile()
try:
profile.enable()
result = func(*args, **kwargs)
profile.disable()
return result
finally:
s = StringIO()
ps = pstats.Stats(profile, stream=s).strip_dirs().sort_stats('cumulative')
ps.print_stats(30)
print(s.getvalue())
profile.dump_stats("qprofile_output.prof")
else:
result = func(*args, **kwargs)
return result
return profiled_func
# Same thing as above, but using pyinstrument
# Doesn't work
def qinstrument(func):
def profiled_func(*args, **kwargs):
if 'profile' in kwargs and kwargs['profile']:
kwargs.pop('profile')
from pyinstrument import Profiler
profiler = Profiler()
try:
profiler.start()
result = func(*args, **kwargs)
profiler.stop()
#profiler.print()
return result
finally:
#pass
profiler.print()
#s = StringIO()
#ps = pstats.Stats(profile, stream=s).strip_dirs().sort_stats('cumulative')
#ps.print_stats(30)
#print(s.getvalue())
#profile.dump_stats("qprofile_output.prof")
else:
result = func(*args, **kwargs)
return result
return profiled_func
## Example usage
#@qprofile
#def process_one(cmd):
# import commands
# output = commands.getoutput(cmd)
# return output
# Function is profiled if profile=True in kwargs
#print(process_one('uname -a', profile=True))