I'm trying to follow the instructions here: http://docs.python.org/2/library/profile.html#module-cProfile
Specifically, this part:
import cProfile, pstats, io
pr = cProfile.Profile()
pr.enable()
... do something ...
pr.disable()
s = io.StringIO()
ps = pstats.Stats(pr, stream=s)
ps.print_results()
I've already determined that print_results is not a real method of the Stats class, nor does it seem to really exist anywhere. Here is my current code:
import cProfile, pstats, io
def foo(request):
pr = cProfile.Profile()
pr.enable()
pass
pr.disable()
s = io.StringIO()
ps = pstats.Stats(pr, stream = s)
f = open('/profstats', 'a')
ps.print_stats()
f.write(s.getvalue())
s.close()
f.close()
Current result is: TypeError at /inspection-summary/ unicode argument expected, got 'str'
(Output looks like this because I am using Django to call the code in question).
So does anyone know how I can get the profiler to actually, well, work? I just want it to profile like it's supposed to, then print the results to a file so I can view the results later after execution. I can get dump_stats to work, but the file it produces is garbage.