In this article we will cover following points:
- Profile single function
- Profile complete python script
- How to read .prof file
- Profile web2py application
To install cprofile on ubuntu use following command:
1 |
sudo pip install cprofilev |
1) Profile single function
To profile function, import cProfile
1 |
import cProfile |
Then call function using cProfile.run()
1 |
cProfile.run('test()', 'test.profile') |
This will write profiling data in test.profile .
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import cProfile def test_func(): numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9] addition = 0 for num in numbers: addition += num return addition if __name__ == '__main__': cProfile.run('test_func()', 'test.profile') |
Later in this post we will see how to read .profile file
2) Profile python script
Run python script using following command :
1 |
python -m cProfile -o test.profile test.py |
This will profile complete script and write profiling data in test.profile.
3) How to read .profile file?
Using pstats module:
.profile file is in binary format. So to examine data from .profile file use the interactive mode of the pstats module by running it as a script with the profile data file as argument.
1 |
python -m pstats test.profile |
Now you will go in interactive profile statistics browser.
1 2 |
Welcome to the profile statistics browser. test.profile{b15f91eef52e7c6d40fd3fe45227e26332e4c02be027e4e72c6ecf7adacefb97} |
Now use stats command to display stats
1 2 |
Welcome to the profile statistics browser. test.profile{b15f91eef52e7c6d40fd3fe45227e26332e4c02be027e4e72c6ecf7adacefb97} stats |
You can sort stats using following sort keys:
cumulative — cumulative time
module — file name
ncalls — call count
pcalls — primitive call count
file — file name
line — line number
name — function name
calls — call count
stdname — standard name
nfl — name/file/line
filename — file name
cumtime — cumulative time
time — internal time
tottime — internal time
For example:
1 2 |
Welcome to the profile statistics browser. test.profile{b15f91eef52e7c6d40fd3fe45227e26332e4c02be027e4e72c6ecf7adacefb97} stats tottime |
Above command will sort records using tottime ( internal time)
To show particular number of records use command “stats count”
For example:
1 2 |
Welcome to the profile statistics browser. test.profile{b15f91eef52e7c6d40fd3fe45227e26332e4c02be027e4e72c6ecf7adacefb97} stats 10 |
This will show only 10 records.
Other method to read .profile file is using command “cprofilev -f test_func.profile”
1 |
cprofilev -f test_func.profile |
Run above command in terminal and view stats in browser on http://127.0.0.1:4000
Advantage of this method is you can easily sort records ,just by one click on column name.
4. Profile web2py application
Create a empty text file in a directory and then pass this text file as argument to web2py. All profiling data will be stored in this file.
For example :
Create lcm.txt in ‘/home/gaurav/temp/lcm/” directory. Now run web2py server using following command with command line argument -F (profiler filename)
1 |
python web2py.py -p 8000 -a g0987 -F /home/gaurav/temp/lcm/lcm.txt |
Note: For some web2py version , we have to provide directory using -f (not -F) option and data will be written in that directory in the form of .profile files. We can read these files using above mentioned methods.
Now open application from browser and open pages you want to profile.Then stop web2py server. This will write profiling data to lcm.txt. Data in text file will be in readable format , no need of pstats module to read data.
Recent Comments