Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Measuring performance in Python

I'm writing a web-application in Python, I haven't decided if I want to use Flask, web.py or something else yet, and I want to be able to do profile on the live application.

There seems to be very little information on how you go about implementing the instrumentation to do performance measurement, short of doing a lot of print datetime.now() everywhere.

What is the best way of going about instrumenting your Python application to allow good measurements to be made. I guess I'm looking for something similar to the Stackoverflow teams mvc-mini-profiler.

like image 670
Simon Avatar asked Jun 21 '11 17:06

Simon


People also ask

Which module S of python can be used to measure the performance of your application code?

In Python, we have a by default module for benchmarking which is called timeit. With the help of the timeit module, we can measure the performance of small bit of Python code within our main program.


2 Answers

You could simply run cProfile tool that comes with Python:

python -m cProfile script.py

Of course, you would have to create the script.py file that would execute the parts of the code that you want to test. If you had some unit tests, you could also use that.

Or you couse use:

import cProfile 
cProfile.run('foo()')

to profile it from foo entry point.

like image 187
rafalotufo Avatar answered Oct 15 '22 21:10

rafalotufo


Amir Salihefendic wrote a short (150 LOC) RequestProfiler, which is described in this blog post:

  • http://amix.dk/blog/post/19359

I haven't tried it, but since it is a WSGI middleware, it should be somewhat pluggable.

like image 44
miku Avatar answered Oct 15 '22 19:10

miku